我想知道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'中的值。 –
謝謝!這就是我相信它會做的。 –