您的位置:首页 > 其它

Longest Substring With Repeating Characters

2015-07-21 09:16 204 查看
public class Solution {
public int lengthOfLongestSubstring(String s) {

HashSet<Character> set = new HashSet<Character>();
char[] ch = s.toCharArray();
int left = 0, right = 0;
int result = 0;
while (right < ch.length) {
if (!set.contains(ch[right])) {
set.add(ch[right]);
result = Math.max(result, (right - left + 1));
} else {
while (ch[left] != ch[right]) {
set.remove(ch[left]);
left++;
}
left++;
}
right++;
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息