Leetcode(Longest Substring Without Repeating Characters)
Posted hugeng007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode(Longest Substring Without Repeating Characters)相关的知识,希望对你有一定的参考价值。
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;
}
}
以上是关于Leetcode(Longest Substring Without Repeating Characters)的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode409. Longest Palindrome
[Leetcode]Longest Palindromic Substring
leetcode longest consecutive sequence