简体   繁体   English

为什么我的代码只有在我注释掉“break;”时才有效?

[英]Why does my code only work when I comment out "break;"?

I am trying to solve a LeetCode question ( 26. Remove Duplicates from Sorted Array ) and my code was not working until I commented out "break;".我正在尝试解决 LeetCode 问题( 26. Remove Duplicates from Sorted Array ),直到我注释掉“break;”,我的代码才起作用。 Could someone explain why this is so?有人可以解释为什么会这样吗? Here's my code:这是我的代码:

class Solution {
    public int removeDuplicates(int[] nums) {
        int read = 0;
        int write = 0;
        
        for (int i = 0; i < nums.length; i++) {
            int found = 0;
            for (int j = 0; j < nums.length; j++) {
                if ((nums[i] == nums[j]) && (i != j) && (j < i))
                    found = 1;
                   // break;
                }
            
            if (found == 0) {
                nums[write] = nums[read];
                write++;
            }
            
            read++;
            
        }
        return write;
    }
}

Indent your code correctly, and you'll see that the corresponding part reads as:正确缩进你的代码,你会看到相应的部分写成:

            int found = 0;
            for (int j = 0; j < nums.length; j++) {
                if ((nums[i] == nums[j]) && (i != j) && (j < i))
                    found = 1;
                break;
            }

You can see, that the break is not only executed if the condition holds true but in every iteration, so also in the first one.您可以看到, break不仅在条件成立时执行,而且在每次迭代中都会执行,第一次也是如此。 The loop terminates as soon as you've inspected the case j == 0 which is incorrect, of course.一旦您检查了不正确的案例j == 0 ,循环就会立即终止。

As you want to terminate the loop only if the condition holds true, just put the two lines after the if-statement into a code block:由于您只想在条件成立时终止循环,只需将 if 语句后的两行放入代码块中:

            int found = 0;
            for (int j = 0; j < nums.length; j++) {
                if ((nums[i] == nums[j]) && (i != j) && (j < i)) {
                    found = 1;
                    break;
                } 
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此照明代码仅对单个照明有效,而在添加多个照明时会中断? - Why does this lighting code only work for single lights and break when I add multiple lights? 货币转换器-为什么我的for循环开关无法正常工作? 当我运行我的代码时,只有第一个for循环有效 - currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works 如果突破内部if,为什么我的消息打印两次? - Why is my message printing twice when I break out of the inner if? 我需要帮助弄清楚为什么清除按钮代码不起作用 - I need help figuring out why my clear button code does not work 为什么@SuppressWarnings破坏了我的代码? - Why does @SuppressWarnings break my code? 使用if语句时,为什么我的代码没有给出正确的答案? - Why does my code not put out the right answer when I use the if statement? Java:为什么我的for循环又循环一次,然后在我第一次尝试中断之后又中断呢? - Java: Why does my for loop cycle through one more time and then break after I try to break out of it the first time? 为什么我的break语句脱离整个程序? - why does my break statement breaks out of the entire program? 当字符位于数组的第二行时,为什么我的代码不起作用? - Why does my code not work when a character is in the second row of the array? 为什么多态性不能像我期望的那样起作用? - Why polymorphism does not work as I'd expect in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM