while循环退出条件
问题描述:
我想知道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 + 1值存储在其中,并使用它来访问角色。就像这样:
z = i+1;
set.add(s.charAt(z));
使用后增量或预增的变化总是增加变量的值,只有不同,当他们增加量超过变量的值。
+0
谢谢!现在我似乎已经明白了。 –
+0
如果答案对您有帮助,请接受。干杯:) –
'i ++'会改变存储在'i'中的值。 –
谢谢!这就是我相信它会做的。 –