当前位置: 首页 > 知识库问答 >
问题:

在while循环中写入++i与在while循环中递增i的区别

司马昕
2023-03-14
                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;
    }

共有1个答案

帅颖逸
2023-03-14

在第一个示例中,Lindex会递增,即使条件的计算结果为false。换句话说,如果while循环体执行n次,Lindex将增加n+1次次。

在第二个示例中,Lindex只随主体的其余部分递增。因此,如果while循环体执行n次,Lindex将增加n次。

这里有一个非常简单的例子,说明两者的区别:

java prettyprint-override">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
 类似资料:
  • 我对编码完全陌生,正在使用Python作为一个学校项目开发一个基于文本的游戏。用户必须通过键入“北”、“东”、“南”或“西”进入房间。如果方向无效,则应弹出错误消息,提示用户输入新方向。如果用户键入退出,游戏应该结束。 这个项目有一百万个问题,因为我发现我在编码方面很糟糕,但我想弄清楚的是,如果出现提示,如何让我的程序退出游戏。这是我的代码(它不是完整的代码,只是我到目前为止所拥有的代码。我试图一

  • 我用For创建了一个嵌套循环,这是程序代码和输出,然后我尝试了同时循环,得到了不同的结果 对于 虽然 请引导我。。谢谢

  • 只要给定条件为真,Perl编程语言中的while循环语句就会重复执行目标语句。 语法 (Syntax) Perl编程语言中while循环的语法是 - while(condition) { statement(s); } 这里的statement(s)可以是单个陈述或一个陈述块。 condition可以是任何表达。 当条件为真时,循环迭代。 当条件变为假时,程序控制将立即传递到循环之后的行。

  • 编写程序时,您可能会遇到需要反复执行操作的情况。 在这种情况下,您需要编写循环语句以减少行数。 JavaScript支持所有必要的循环,以减轻编程压力。 while循环 JavaScript中最基本的循环是while循环,将在本章中讨论。 while循环的目的是只要expression为真,就重复执行语句或代码块。 表达式变为false,循环终止。 流程图 while loop流程图如下 - 语法

  • 只要给定条件为真,Objective-C编程语言中的while循环语句就会重复执行目标语句。 语法 (Syntax) Objective-C编程语言中while循环的语法是 - while(condition) { statement(s); } 这里, statement(s)可以是单个语句或语句块。 condition可以是任何表达式,true是任何非零值。 当条件为真时,循环迭代。

  • While循环一次又一次地执行相同的代码,直到满足停止条件。 语法 (Syntax) 在R中创建while循环的基本语法是 - while (test_expression) { statement } 流程图 (Flow Diagram) 这里while循环的关键点是循环可能永远不会运行。 当测试条件并且结果为假时,将跳过循环体并且将执行while循环之后的第一个语句。 例子 (Exam