Java でコンパイルし、append、addAll、multAll の 3 つの API を実装して素晴らしいシーケンスを実現してください。Fancy クラスを実装してください。 Fancy() 空のシーケンス オブジェクトを初期化します。void append(v

Java でコンパイルする場合は、append、addAll、multAll の 3 つの API を実装して、素晴らしいシーケンスを実現してください。

Fancy クラスを実装してください。

Fancy() は空のシーケンス オブジェクトを初期化します。
void append(val) 整数 val をシーケンスの末尾に追加します。
void addAll(inc) シーケンス内のすべての既存の値に inc を追加します。
void multAll(m) シーケンス内のすべての既存の値を整数 m で乗算します。
int getIndex(idx) 添字 idx の値を取得し (添字は 0 から始まります)、結果を 109 + 7 で剰余します。添字がシーケンスの長さ以上の場合は -1 を返します。
例:

入力:
[“Fancy”, “append”, “addAll”, “append”, “multAll”, “getIndex”, “addAll”, “append”, “multAll”, “getIndex”, “getIndex”, “getIndex” ]
[[]、[2]、[3]、[7]、[2]、[0]、[3]、[10]、[2]、[0]、[1]、[2]]
输出:
[ヌル、ヌル、ヌル、ヌル、ヌル、10、ヌル、ヌル、ヌル、26、34、20]

説明:
Fancy fancy = new Fancy();
fancy.append(2); // ファンシーシーケンス: [2]
fancy.addAll(3); // ファンシーシーケンス: [2+3] -> [5]
fancy.append (7); // 派手なシーケンス: [5, 7]
fancy.multAll(2); // 派手なシーケンス: [5 2, 7 2] -> [10, 14]
fancy.getIndex(0); // return 10
fancy.addAll(3); // ファンシーシーケンス: [10+3, 14+3] -> [13, 17]
fancy.append(10); // ファンシーシーケンス: [13, 17, 10]
fancy. multAll(2); // ファンタスティック シーケンス: [13 2, 17 2, 10*2] -> [26, 34, 20]
fancy.getIndex(0); // 26 を返します
fancy.getIndex(1); // 34 を返します
fancy.getIndex(2); // 20 を返します

public class Fancy { private int[] 配列; プライベート int の長さ。public Fancy() { this.array = new int[0]; this.length = 0; public void append(int val) { int[] temp = new int[this.length + 1] ; for (int i = 0; i < this.length; i++) { temp[i] = this.array[i]; temp [ this.length] = val; this.length += 1; this.array = temp; public void addAll(int inc) { for (int i = 0; i < this.length; i++) { this.array [i] += inc; public void multAll(int m) { for (int i = 0; i < this.length; i++) { this.array[i] *= m; } } }

























public int getIndex(int idx) { if (idx >= this.length) { return -1; } } return (this.array[idx] % 109 + 7) % 109; } }





おすすめ

転載: blog.csdn.net/zezeaichirou/article/details/129256566