[英]Data type conversion problem for leetcode pratice "Moving Average from Data Stream"
When I was running the code for the leetcode question ""Moving Average from Data Stream"".当我运行 leetcode 问题““来自数据流的移动平均线””的代码时。 I got a partly different output with the expected:我得到了一个与预期部分不同的 output: Here is my code:这是我的代码:
class MovingAverage {
int front = 0, rear = 0; Double sum = 0.00000;
int[] data;
public MovingAverage(int size) {
data = new int[size];
Arrays.fill(data, 0);
}
public double next(int val) {
if(data.length == 1){
data[rear] = val;
return val;
}else{
if(rear < data.length){
data[rear] = val;
sum += data[rear];
rear++;
return sum/rear;
}else{
sum -= data[front];
rear = (rear+1) % data.length - 1;
front = (front+1) % data.length ;
data[rear] = val;
sum += data[rear];
rear++;
return sum/data.length;
}
}
}
}
The requirement for code is:对代码的要求是:
When I run the tast cases of the example provided, everything is ok.当我运行所提供示例的测试用例时,一切正常。 But when I run this edge case, thing is different: ["MovingAverage","next","next","next","next","next","next","next","next","next","next"] [[5],[12009],[1965],[-940],[-8516],[-16446],[7870],[25545],[-21028],[18430],[-23464]]但是当我运行这个边缘案例时,事情就不同了:["MovingAverage","next","next","next","next","next","next","next","next","下一个","下一个"] [[5],[12009],[1965],[-940],[-8516],[-16446],[7870],[25545],[-21028],[18430 ],[-23464]]
Anyone can help me pls?任何人都可以帮助我吗? Is my code wrong or I didn't process the data conversion well?是我的代码有误还是我没有很好地处理数据转换?
This logical check is incorrect:此逻辑检查不正确:
if (rear < data.length)
In the corresponding else
block, when rear = data.length
, you set rear = (rear+1) % data.length - 1
, so rear < data.length
becomes true in the next iteration, and you go back to the if
block again.在相应的else
块中,当rear = data.length
时,你设置rear = (rear+1) % data.length - 1
,所以rear < data.length
在下一次迭代中变为 true,你 go 回到if
块再次。 However, the logic to dequeue and update values is in the else
block (which is where you should end up once data
is filled completely).但是,出列和更新值的逻辑在else
块中(一旦data
完全填充,您应该在该块结束)。 This is why you're getting an incorrect answer;这就是您得到错误答案的原因; if (rear < data.length)
isn't correct. if (rear < data.length)
不正确。
Replacing that condition with something like if (numberOfElementsTillNow < data.length)
should fix this issue.用if (numberOfElementsTillNow < data.length)
类的内容替换该条件应该可以解决此问题。
I revised my code, and the answer accepted:我修改了我的代码,并接受了答案:
class MovingAverage {
int front = 0, rear = 0; int sum = 0;
int[] data;
public MovingAverage(int size) {
data = new int[size];
Arrays.fill(data, 0);
}
public double next(int val) {
if(data.length == 1){
data[rear] = val;
return (double)val;
}else{
if(rear < data.length){
data[rear] = val;
sum += (int)data[rear];
rear++;
return sum * 1.0/rear;
}else{
sum -= (int)data[front % data.length];
data[rear % data.length] = val;
sum += (int)data[rear % data.length];
rear++;
front++;
return sum * 1.0/data.length;
}
}
}
} }
This issue can be closed.这个问题可以关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.