在 while 循环条件下写 ++i 和在 while 循环内递增 i 之间的区别
Difference between writing ++i in the condition of a while loop and incrementing i inside the while loop
我正在编写此问题底部所示的代码。
对于while循环部分,我以为下面两段代码是一样的
第一个
while (lIndex < rIndex && height[++lIndex] <= left) {
ans += (left - height[lIndex]);
}
第二个
while (lIndex < rIndex && height[lIndex + 1] <= left) {
ans += (left - height[lIndex + 1]);
lIndex++;
}
但是,当我 运行 系统上的第二个时,
存在超时错误。
有人可以解释这个问题的原因吗?
感谢阅读我的问题。
原代码:
public int trap(int[] height) {
if (height.length < 3) return 0;
int ans = 0;
int lIndex = 0;
int rIndex = height.length - 1;
// Find the first wall on each side
while (lIndex < rIndex && height[lIndex] <= height[lIndex + 1]) lIndex++;
while (lIndex < rIndex && height[rIndex] <= height[rIndex - 1]) rIndex--;
while (lIndex < rIndex) {
int left = height[lIndex];
int right = height[rIndex];
if (left <= right) {
while (lIndex < rIndex && height[++lIndex] <= left) {
ans += (left - height[lIndex]);
}
}
else {
while (lIndex < rIndex && height[--rIndex] <= right) {
ans += right - height[rIndex];
}
}
}
return ans;
}
在第一个示例中,即使条件评估最终为假,lIndex
也会递增。换句话说,如果while
循环体被执行了n
次,lIndex
会增加n + 1
次。
在第二个示例中,lIndex
仅随正文的其余部分递增。所以如果while
循环体被执行了n
次,lIndex
会递增n
次。
这是一个非常简单的例子,显示了两者的区别:
public class Test {
public static void main(String[] args) {
int i = 0;
while (++i < 3) {
System.out.println("First loop iteration");
}
System.out.println("Value of i afterwards: " + i);
int j = 0;
while (j + 1 < 3) {
System.out.println("Second loop iteration");
j++;
}
System.out.println("Value of j afterwards: " + j);
}
}
输出:
First loop iteration
First loop iteration
Value of i afterwards: 3
Second loop iteration
Second loop iteration
Value of j afterwards: 2
所以两个循环都执行了两次主体,但它们以不同的计数器值结束。
我正在编写此问题底部所示的代码。
对于while循环部分,我以为下面两段代码是一样的
第一个
while (lIndex < rIndex && height[++lIndex] <= left) {
ans += (left - height[lIndex]);
}
第二个
while (lIndex < rIndex && height[lIndex + 1] <= left) {
ans += (left - height[lIndex + 1]);
lIndex++;
}
但是,当我 运行 系统上的第二个时, 存在超时错误。 有人可以解释这个问题的原因吗? 感谢阅读我的问题。
原代码:
public int trap(int[] height) {
if (height.length < 3) return 0;
int ans = 0;
int lIndex = 0;
int rIndex = height.length - 1;
// Find the first wall on each side
while (lIndex < rIndex && height[lIndex] <= height[lIndex + 1]) lIndex++;
while (lIndex < rIndex && height[rIndex] <= height[rIndex - 1]) rIndex--;
while (lIndex < rIndex) {
int left = height[lIndex];
int right = height[rIndex];
if (left <= right) {
while (lIndex < rIndex && height[++lIndex] <= left) {
ans += (left - height[lIndex]);
}
}
else {
while (lIndex < rIndex && height[--rIndex] <= right) {
ans += right - height[rIndex];
}
}
}
return ans;
}
在第一个示例中,即使条件评估最终为假,lIndex
也会递增。换句话说,如果while
循环体被执行了n
次,lIndex
会增加n + 1
次。
在第二个示例中,lIndex
仅随正文的其余部分递增。所以如果while
循环体被执行了n
次,lIndex
会递增n
次。
这是一个非常简单的例子,显示了两者的区别:
public class Test {
public static void main(String[] args) {
int i = 0;
while (++i < 3) {
System.out.println("First loop iteration");
}
System.out.println("Value of i afterwards: " + i);
int j = 0;
while (j + 1 < 3) {
System.out.println("Second loop iteration");
j++;
}
System.out.println("Value of j afterwards: " + j);
}
}
输出:
First loop iteration
First loop iteration
Value of i afterwards: 3
Second loop iteration
Second loop iteration
Value of j afterwards: 2
所以两个循环都执行了两次主体,但它们以不同的计数器值结束。