Problem
Given a string, find the length of the longest substring without repeating characters.
Examples
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
很奇怪我为什么没写过这道题...
Solution
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) return 0;
Map<Character, Integer> dict = new HashMap<>();
int start = 0, res = 0;
for (int i = 0; i < s.length(); i++) {
Character ch = s.charAt(i);
Integer pre = dict.get(ch);
if (pre != null && pre >= start) {
res = Math.max(res, i - start);
start = pre + 1;
}
dict.put(ch, i);
}
return Math.max(s.length()-start, res);
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。