请你实现三个 API append,addAll 和 multAll 来实现奇妙序列。 请实现 Fancy 类 : Fancy() 初始化一个空序列对象。 void append(val) 将整数 val 添加在序列末尾。 void addAll(inc) 将所有序列中的现有数值都增加 inc 。 void multAll(m) 将序列中的所有现有数值都乘以整数 m 。 int getIndex(idx) 得到下标为 idx 处的数值(下标从 0 开始),并将结果对 109 + 7 取余。如果下标大于等于序列的长度,请返回 -1 。
时间: 2023-02-20 12:55:04 浏览: 73
以下是 Python 代码实现:
```
class Fancy:
def __init__(self):
self.seq = []
self.add = 0
self.mult = 1
def append(self, val):
self.seq.append((val + self.add) * self.mult)
def addAll(self, inc):
self.add += inc
def multAll(self, m):
self.add = self.add * m
self.mult = self.mult * m
def getIndex(self, idx):
if idx >= len(self.seq):
return -1
return (self.seq[idx] - self.add) // self.mult % (10**9 + 7)
```
相关问题
请你用javascript实现三个 API append,addAll 和 multAll 来实现奇妙序列。 请实现 Fancy 类 : Fancy() 初始化一个空序列对象。 void append(val) 将整数 val 添加在序列末尾。 void addAll(inc) 将所有序列中的现有数值都增加 inc 。 void multAll(m) 将序列中的所有现有数值都乘以整数 m 。 int getIndex(idx) 得到下标为 idx 处的数值(下标从 0 开始),并将结果对 109 + 7 取余。如果下标大于等于序列的长度,请返回 -1 。
以下是用 JavaScript 实现的 Fancy 类的示例代码:
```
class Fancy {
constructor() {
this.sequence = [];
this.increment = 0;
this.multiplier = 1;
}
append(val) {
this.sequence.push(val);
}
addAll(inc) {
this.increment += inc;
}
multAll(m) {
this.multiplier *= m;
this.increment *= m;
}
getIndex(idx) {
if (idx >= this.sequence.length) return -1;
return ((this.sequence[idx] + this.increment) * this.multiplier) % 1000000007;
}
}
```
这个类定义了三个方法:
- `append` 方法用于向序列末尾添加一个整数。
- `addAll` 方法用于将所有序列中的现有数值都增加给定的数。
- `multAll` 方法用于将所有序列中的现有数值都乘以给定的数。
- `getIndex` 方法用于获取序列中下标为 idx 的数值,并将结果对 1000000007 取模。如果下标大于等于序列的长度,则返回 -1。
示例:
```
const fancy = new Fancy();
fancy.append(1);
fancy.append(2);
fancy.append(3);
fancy.addAll(1);
fancy.multAll(2);
console.log(fancy.getIndex(0)); // 输出 4
console.log(fancy.getIndex(1)); // 输出 6
console.log(fancy.getIndex(2)); // 输出 8
console.log(fancy.getIndex(3)); // 输出 -1
```
示例: 输入: ["Fancy", "append", "addAll", "append", "multAll", "getIndex", "addAll", "append", "multAll", "getIndex", "getIndex", "getIndex"] [[], [2], [3], [7], [2], [0], [3], [10], [2], [0], [1], [2]] 输出: [null, null, null, null, null, 10, null, null, null, 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); // 返回 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
给定一个操作列表和操作参数列表,请实现一个 "Fancy" 类,执行这些操作并返回结果。
示例代码如下:
```
class Fancy {
List<Integer> list;
int add = 0, mul = 1;
public Fancy() {
list = new ArrayList<>();
}
public void append(int val) {
list.add(val * mul + add);
}
public void addAll(int inc) {
add += inc;
}
public void multAll(int m) {
mul *= m;
add *= m;
}
public int getIndex(int idx) {
if (idx >= list.size()) return -1;
return (list.get(idx) - add) / mul;
}
}
```
使用示例:
```
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); // 返回 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
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)