Skip to content

Commit e12335d

Browse files
author
rchen102
committed
Review: 4
1 parent 4d06263 commit e12335d

File tree

4 files changed

+43
-53
lines changed

4 files changed

+43
-53
lines changed

Java/leetcode 187.java

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,8 @@ public List<String> findRepeatedDnaSequences(String s) {
1414
}
1515
}
1616

17-
//Solution2: bit manipulation and HashSet T: O(n) S: O(n)
18-
class Solution {
19-
public List<String> findRepeatedDnaSequences(String s) {
20-
List<String> list = new ArrayList<>();
21-
if(s.length() < 10) return list;
22-
23-
Set<Integer> set = new HashSet<>();
24-
Set<Integer> repeated = new HashSet<>();
25-
26-
//coding of a 10-letter sequence into a 4-bytes number
27-
char[] map = new char[26];
28-
//A:00
29-
map['C'-'A'] = 1; //C:01
30-
map['G'-'A'] = 2; //G:10
31-
map['T'-'A'] = 3; //T:11
32-
33-
for(int i = 0; i < s.length() - 9; i++) {
34-
int v = 0;
35-
for(int j = i; j < i + 10; j++) {
36-
v <<= 2;
37-
v |= map[s.charAt(j) - 'A'];
38-
}
39-
if(!set.add(v) && repeated.add(v)) {
40-
list.add(s.substring(i, i + 10));
41-
}
42-
}
43-
return list;
44-
}
45-
}
46-
47-
//Solution2: follow up
17+
// Solution2: bit manipulation and HashSet
18+
// T: O(n) S: O(n)
4819
class Solution {
4920
public List<String> findRepeatedDnaSequences(String s) {
5021
List<String> list = new ArrayList<>();
@@ -64,7 +35,7 @@ public List<String> findRepeatedDnaSequences(String s) {
6435
for(int i = 0; i < s.length(); i++) {
6536
v <<= 2;
6637
v |= map[s.charAt(i) - 'A'];
67-
v &= 0xfffff;
38+
v &= (1 << 20) - 1;
6839
if(i < 9)
6940
continue;
7041
if(!set.add(v) && repeated.add(v)) {

Java/leetcode 205.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public boolean isIsomorphic(String s, String t) {
2121
}
2222
}
2323

24-
//Own Solution: HashMap T: O(n) S: O(512)
24+
//Own Solution: HashMap
25+
//T: O(n) S: O(512)
2526
class Solution {
2627
public boolean isIsomorphic(String s, String t) {
2728
Map<Character, Integer> mapS = new HashMap<>();

Java/leetcode 215.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,34 @@ private void swap(int[] nums, int i, int j) {
5555
}
5656
}
5757

58-
// Solution2: priorityqueue T: O(nlogk) S: O(k)
58+
// Solution2: Heap(PriorityQueue)
59+
// T: O(nlogk) S: O(k)
5960
class Solution {
6061
public int findKthLargest(int[] nums, int k) {
62+
if (nums == null || nums.length == 0 ) return -1;
6163
PriorityQueue<Integer> pq = new PriorityQueue<>();
62-
for(int num : nums) {
64+
for (int num : nums) {
65+
if (pq.size() >= k) {
66+
if (pq.peek() < num) {
67+
pq.poll();
68+
pq.offer(num);
69+
}
70+
}
71+
else pq.offer(num);
72+
}
73+
return pq.poll();
74+
}
75+
76+
// 简化,复杂度稍高,会增加不必要的插入、删除操作
77+
public int findKthLargest(int[] nums, int k) {
78+
if (nums == null || nums.length == 0 ) return -1;
79+
PriorityQueue<Integer> pq = new PriorityQueue<>();
80+
for (int num : nums) {
6381
pq.offer(num);
64-
if(pq.size() > k) {
82+
if (pq.size() > k) {
6583
pq.poll();
6684
}
6785
}
68-
return pq.peek();
86+
return pq.poll();
6987
}
7088
}

Java/leetcode 49.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
// Solution1: HashMap + sort
2-
// T: O(n * klogk) S: O(n)
1+
// Solution1: HashMap + encoding function
2+
// T: O(n * k) S: O(n)
33
class Solution {
44
public List<List<String>> groupAnagrams(String[] strs) {
55
if (strs == null || strs.length == 0) return null;
66
Map<String, List<String>> map = new HashMap<>();
77
for (String str : strs) {
8-
char[] chs = str.toCharArray();
9-
Arrays.sort(chs);
10-
String tag = String.valueOf(chs);
8+
String tag = this.encode(str);
119

1210
if (!map.containsKey(tag)) {
1311
map.put(tag, new ArrayList<>());
@@ -16,16 +14,26 @@ public List<List<String>> groupAnagrams(String[] strs) {
1614
}
1715
return new ArrayList<>(map.values());
1816
}
17+
18+
private String encode(String str) {
19+
char[] code = new char[26]; // ASCLL = 0 by default
20+
for (char ch : str.toCharArray()) {
21+
code[ch-'a'] += 1;
22+
}
23+
return String.valueOf(code);
24+
}
1925
}
2026

21-
// Solution2: HashMap + encoding function
22-
// T: O(n * k) S: O(n)
27+
// Solution2: HashMap + sort
28+
// T: O(n * klogk) S: O(n)
2329
class Solution {
2430
public List<List<String>> groupAnagrams(String[] strs) {
2531
if (strs == null || strs.length == 0) return null;
2632
Map<String, List<String>> map = new HashMap<>();
2733
for (String str : strs) {
28-
String tag = this.encode(str);
34+
char[] chs = str.toCharArray();
35+
Arrays.sort(chs);
36+
String tag = String.valueOf(chs);
2937

3038
if (!map.containsKey(tag)) {
3139
map.put(tag, new ArrayList<>());
@@ -34,12 +42,4 @@ public List<List<String>> groupAnagrams(String[] strs) {
3442
}
3543
return new ArrayList<>(map.values());
3644
}
37-
38-
private String encode(String str) {
39-
char[] code = new char[26]; // ASCLL = 0 by default
40-
for (char ch : str.toCharArray()) {
41-
code[ch-'a'] += 1;
42-
}
43-
return String.valueOf(code);
44-
}
4545
}

0 commit comments

Comments
 (0)