Skip to content

Commit 53e69ef

Browse files
committed
Update
1 parent 63d1ff3 commit 53e69ef

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.mvn/*
55
target
66
*.class
7+
*.iml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
package com.fibers.algorithm.leetcode._003;
22

3+
import java.util.HashMap;
4+
35
public class Solution {
6+
public int lengthOfLongestSubstring(String s) {
7+
if (s.length() == 0) {
8+
return 0;
9+
}
10+
HashMap<Character, Integer> map = new HashMap<>();
11+
int max = 0;
12+
for (int i = 0, j = 0; i < s.length(); ++i) {
13+
if (map.containsKey(s.charAt(i))) {
14+
j = Math.max(j, map.get(s.charAt(i)) + 1);
15+
}
16+
map.put(s.charAt(i), i);
17+
max = Math.max(max, i - j + 1);
18+
}
19+
return max;
20+
}
421
}

0 commit comments

Comments
 (0)