-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution03.java
More file actions
35 lines (32 loc) · 913 Bytes
/
Solution03.java
File metadata and controls
35 lines (32 loc) · 913 Bytes
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
import java.util.HashMap;
import java.util.Map;
/**
* Created by Alex on 2016/5/19.
*/
public class Solution03 {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
char[] input = s.toCharArray();
int index = 0;
int result = 0;
int currentLength = 0;
while(index != input.length-1){
if(!map.containsKey(input[index])){
map.put(input[index], index);
index++;
currentLength++;
}else {
if(currentLength > result){
result = currentLength;
}
index = map.get(input[index]) + 1;
map.clear();
currentLength = 0;
}
}
if(currentLength > result){
result = currentLength;
}
return result;
}
}