Skip to content

Commit f73d6c8

Browse files
author
haotf
committed
无重复字符的最长子串
1 parent 530f379 commit f73d6c8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Diff for: 3.无重复字符的最长子串.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
/*
7+
* @lc app=leetcode.cn id=3 lang=java
8+
*
9+
* [3] 无重复字符的最长子串
10+
*/
11+
12+
// @lc code=start
13+
class Solution {
14+
public int lengthOfLongestSubstring(String s) {
15+
16+
Map<Character, Integer> map = new HashMap<Character, Integer>();
17+
18+
int max = 0;
19+
int left = 0;
20+
int right = 0;
21+
22+
while (right < s.length()) {
23+
char r = s.charAt(right);
24+
map.put(r, map.getOrDefault(r, 0) + 1);
25+
right++;
26+
while (map.get(r) > 1) {
27+
char l = s.charAt(left);
28+
map.put(l, map.get(l) - 1);
29+
left++;
30+
}
31+
max = Math.max(max, right - left);
32+
}
33+
34+
return max;
35+
}
36+
}
37+
// @lc code=end

0 commit comments

Comments
 (0)