我想知道while循环如何退出?因为除了在s.charAt(j ++)和s.charAt(i ++)上寻找j或第i个位置上的char,i和j不会增加。这是否也会增加j和i?在我看来,它应该只给你在j ++或i ++的字符代码,th位置不是这样吗?while循环退出条件
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) {
// try to extend the range [i, j]
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;
}
}使用I ++递增i的值
'i ++'会改变存储在'i'中的值。 –
谢谢!这就是我相信它会做的。 –