[LeetCode] Longest Substring Without Repeating Characters
文章目录
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);
}
}

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论