Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我找到了关于Leetcode的3个问题的解决方案_Java_Scope_Increment - Fatal编程技术网

Java 我找到了关于Leetcode的3个问题的解决方案

Java 我找到了关于Leetcode的3个问题的解决方案,java,scope,increment,Java,Scope,Increment,下面的代码是我为3找到的。Leetcode上没有重复字符的最长子字符串: public class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(); Set<Character> set = new HashSet<>(); int ans = 0, i = 0, j = 0; while (i < n && j

下面的代码是我为3找到的。Leetcode上没有重复字符的最长子字符串:

public class Solution {
public int lengthOfLongestSubstring(String s) {
    int n = s.length();
    Set<Character> set = new HashSet<>();
    int ans = 0, i = 0, j = 0;
    while (i < n && j < n) {
        if (!set.contains(s.charAt(j))){
            set.add(s.charAt(j++));
            ans = Math.max(ans, j - i);
        }
        else {
            set.remove(s.charAt(i++));
        }
    }
    return ans;
}
}

我不明白的是,当我们进入while循环的第一次迭代时,I=0,j=0。现在,如果输入了if语句,那么现在是'j++',因此j=1。现在j=1,在if语句中。比方说,现在我在while循环的第二次迭代中,所以I=1,j=2??在while循环的第二次迭代中,j=2??

,因此i=1,j=2?不,我是0,j是1