Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 23eae9f

Browse files
refactor 3
1 parent 8a1ddee commit 23eae9f

File tree

2 files changed

+92
-19
lines changed

2 files changed

+92
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ Your ideas/fixes/algorithms are more than welcome!
600600
|6|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_6.java) | O(n) | O(n) | Easy |
601601
|5|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5.java) | O(n^2) | O(1) | Medium|
602602
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_4.java) | ? | ? | Hard | Divide and Conquer
603-
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_3.java) | O(n) | O(1) | Medium | HashMap, Sliding Window
603+
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_3.java) | O(n) | O(k) | Medium | HashMap, Sliding Window
604604
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_2.java) | O(max(m,n)) | O(1) | Medium | LinkedList
605605
|1|[Two Sum](https://leetcode.com/problems/two-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1.java)| O(n)| O(n) | Easy| HashMap
606606

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,111 @@
11
package com.fishercoder.solutions;
22

33
import java.util.HashMap;
4+
import java.util.HashSet;
45
import java.util.Map;
6+
import java.util.Set;
57

6-
/**Given a string, find the length of the longest substring without repeating characters.
8+
/**
9+
* 3. Longest Substring Without Repeating Characters
10+
* Given a string, find the length of the longest substring without repeating characters.
711
812
Examples:
913
1014
Given "abcabcbb", the answer is "abc", which the length is 3.
1115
1216
Given "bbbbb", the answer is "b", with the length of 1.
1317
14-
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.*/
18+
Given "pwwkew", the answer is "wke", with the length of 3.
19+
20+
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
21+
*/
22+
1523
public class _3 {
1624

17-
public static int lengthOfLongestSubstring(String s) {
18-
int result = 0;
19-
Map<Character, Integer> map = new HashMap();
20-
for (int i = 0, j = i; j < s.length(); ){
21-
if (!map.containsKey(s.charAt(j)) || (map.containsKey(s.charAt(j)) && map.get(s.charAt(j)) == 0)){
22-
map.put(s.charAt(j), 1);
23-
result = Math.max(j-i+1, result);
24-
j++;
25-
} else {
26-
map.put(s.charAt(i), map.get(s.charAt(i)) - 1);
27-
i++;
25+
public static class Solution1 {
26+
public int lengthOfLongestSubstring(String s) {
27+
int result = 0;
28+
Map<Character, Integer> map = new HashMap();
29+
for (int i = 0, j = i; j < s.length(); ) {
30+
if (!map.containsKey(s.charAt(j)) || (map.containsKey(s.charAt(j)) && map.get(s.charAt(j)) == 0)) {
31+
map.put(s.charAt(j), 1);
32+
result = Math.max(j - i + 1, result);
33+
j++;
34+
} else {
35+
map.put(s.charAt(i), map.get(s.charAt(i)) - 1);
36+
i++;
37+
}
38+
}
39+
return result;
40+
}
41+
}
42+
43+
public static class Solution2 {
44+
/**
45+
* Sliding Window
46+
* O(n) time
47+
* O(min(m,n)) or O(k) space
48+
*/
49+
public int lengthOfLongestSubstring(String s) {
50+
int n = s.length();
51+
Set<Character> set = new HashSet<>();
52+
int result = 0;
53+
int i = 0;
54+
int j = 0;
55+
while (i < n && j < n) {
56+
/**Try to extend the range i, j*/
57+
if (!set.contains(s.charAt(j))) {
58+
set.add(s.charAt(j++));
59+
result = Math.max(result, j - i);
60+
} else {
61+
set.remove(s.charAt(i++));
62+
}
63+
}
64+
return result;
65+
}
66+
}
67+
68+
public static class Solution3 {
69+
/**Sliding Window
70+
* O(n) time
71+
* O(n) space
72+
*/
73+
public int lengthOfLongestSubstring(String s) {
74+
if (s.length() == 0) {
75+
return 0;
76+
}
77+
int max = 0;
78+
Map<Character, Integer> map = new HashMap<>();
79+
/**Try to extend the range (i, j)*/
80+
for (int i = 0, j = 0; i < s.length(); i++) {
81+
if (map.containsKey(s.charAt(i))) {
82+
j = Math.max(j, map.get(s.charAt(i)) + 1);
83+
}
84+
map.put(s.charAt(i), i);
85+
max = Math.max(max, i + 1 - j);
2886
}
87+
return max;
2988
}
30-
return result;
3189
}
3290

33-
public static void main(String...args){
34-
// String s = "abcabcbb";
35-
String s = "pwwkew";
36-
System.out.println(lengthOfLongestSubstring(s));
91+
public static class Solution4 {
92+
/**Sliding Window Optimized
93+
* O(n) time
94+
* O(n) space
95+
*/
96+
public int lengthOfLongestSubstring(String s) {
97+
if (s.length() == 0) {
98+
return 0;
99+
}
100+
int max = 0;
101+
int[] index = new int[128];
102+
/**Try to extend the range (i, j)*/
103+
for (int i = 0, j = 0; j < s.length(); j++) {
104+
i = Math.max(index[s.charAt(j)], i);
105+
max = Math.max(max, j -i + 1);
106+
index[s.charAt(j)] = j+1;
107+
}
108+
return max;
109+
}
37110
}
38111
}

0 commit comments

Comments
 (0)