Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
36 lines (27 loc) · 894 Bytes

File metadata and controls

36 lines (27 loc) · 894 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Algorithms.string;
import java.util.HashMap;
public class LengthOfLongestSubstring {
public int lengthOfLongestSubstring(String s) {
if (s == null) {
return 0;
}
int len = s.length();
// The start of the window.
int start = 0;
int max = 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int end = 0; end < len; end++) {
char c = s.charAt(end);
if (map.containsKey(c)) {
if (map.get(c) >= start) {
start = map.get(c) + 1;
}
}
//更新map里的char 的位置
map.put(c, end);
int subLen = end - start + 1;
max = Math.max(max, subLen);
}
return max;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.