Skip to content

Commit 8c87698

Browse files
committed
optimize solution of 3 & 49
1 parent b5e294c commit 8c87698

File tree

2 files changed

+14
-65
lines changed
  • codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table

2 files changed

+14
-65
lines changed

codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table/_3.java

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,75 +32,22 @@
3232
*/
3333
public class _3 {
3434
/**
35-
* Solution 1: Violence method
35+
* Slide window method of optimize
3636
*
37-
* @param s
38-
* @return
37+
* @param s target string
38+
* @return size
3939
*/
4040
public int lengthOfLongestSubstring(String s) {
41-
int n = s.length();
42-
int ans = 0;
43-
for (int i = 0; i < n; i++) {
44-
for (int j = i + 1; j <= n; j++) {
45-
if (allUnique(s, i, j)) {
46-
ans = Math.max(ans, j - i);
47-
}
48-
}
49-
}
50-
return ans;
51-
}
52-
53-
private boolean allUnique(String s, int start, int end) {
54-
Set<Character> aSet = new HashSet<>();
55-
for (int i = start; i < end; i++) {
56-
if (aSet.contains(s.charAt(i))) {
57-
return false;
58-
} else {
59-
aSet.add(s.charAt(i));
60-
}
61-
}
62-
return true;
63-
}
64-
65-
/**
66-
* Solution 2: Slide window method
67-
*
68-
* @param s
69-
* @return
70-
*/
71-
public int lengthOfLongestSubstring2(String s) {
72-
int n = s.length();
73-
Set<Character> set = new HashSet<>();
74-
int ans = 0, i = 0, j = 0;
75-
while (i < n && j < n) {
76-
// try to extend the range [i, j]
77-
if (!set.contains(s.charAt(j))) {
78-
set.add(s.charAt(j++));
79-
ans = Math.max(ans, j - i);
80-
} else {
81-
set.remove(s.charAt(i++));
82-
}
83-
}
84-
return ans;
85-
}
86-
87-
/**
88-
* Solution 3: Slide window method of optimize
89-
*
90-
* @param s
91-
* @return
92-
*/
93-
public int lengthOfLongestSubstring3(String s) {
94-
int n = s.length(), ans = 0;
41+
int length = s.length(), ans = 0;
9542
// current index of character
9643
Map<Character, Integer> map = new HashMap<>();
9744
// try to extend the range [i, j]
98-
for (int j = 0, i = 0; j < n; j++) {
99-
if (map.containsKey(s.charAt(j))) {
100-
i = Math.max(map.get(s.charAt(j)), i);
45+
for (int i = 0, j = 0; i < length; i++) {
46+
if (map.containsKey(s.charAt(i))) {
47+
j = Math.max(map.get(s.charAt(i)), j);
10148
}
102-
ans = Math.max(ans, j - i + 1);
103-
map.put(s.charAt(j), j + 1);
49+
ans = Math.max(ans, i - j + 1);
50+
map.put(s.charAt(i), i + 1);
10451
}
10552
return ans;
10653
}

codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table/_49.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@
2626
*/
2727
public class _49 {
2828
public List<List<String>> groupAnagrams(String[] strs) {
29-
if (strs == null || strs.length == 0) return new ArrayList<List<String>>();
29+
if (strs == null || strs.length == 0) return new ArrayList<>();
3030
Map<String, List<String>> map = new HashMap<String, List<String>>();
3131
for (String s : strs) {
3232
char[] ca = s.toCharArray();
3333
Arrays.sort(ca);
3434
String keyStr = String.valueOf(ca);
35-
if (!map.containsKey(keyStr)) map.put(keyStr, new ArrayList<String>());
35+
if (!map.containsKey(keyStr)) {
36+
map.put(keyStr, new ArrayList<>());
37+
}
3638
map.get(keyStr).add(s);
3739
}
38-
return new ArrayList<List<String>>(map.values());
40+
return new ArrayList<>(map.values());
3941
}
4042
}

0 commit comments

Comments
 (0)