Skip to content

Commit 83388b7

Browse files
committed
modify code on class
1 parent 197ee16 commit 83388b7

8 files changed

+129
-30
lines changed

src/topinterviewquestions/Problem_0130_SurroundedRegions.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public static void solve2(char[][] board) {
8383
if (board[i][j] == 'F') {
8484
board[i][j] = 'O';
8585
}
86-
8786
}
8887
}
8988
}

src/topinterviewquestions/Problem_0138_CopyListWithRandomPointer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public static Node copyRandomList(Node head) {
2020
}
2121
Node cur = head;
2222
Node next = null;
23+
// 1 -> 2 -> 3 -> null
24+
// 1 -> 1' -> 2 -> 2' -> 3 -> 3'
2325
while (cur != null) {
2426
next = cur.next;
2527
cur.next = new Node(cur.val);
@@ -28,6 +30,8 @@ public static Node copyRandomList(Node head) {
2830
}
2931
cur = head;
3032
Node copy = null;
33+
// 1 1' 2 2' 3 3'
34+
// 依次设置 1' 2' 3' random指针
3135
while (cur != null) {
3236
next = cur.next.next;
3337
copy = cur.next;
@@ -36,6 +40,8 @@ public static Node copyRandomList(Node head) {
3640
}
3741
Node res = head.next;
3842
cur = head;
43+
// 老 新 混在一起,next方向上,random正确
44+
// next方向上,把新老链表分离
3945
while (cur != null) {
4046
next = cur.next.next;
4147
copy = cur.next;

src/topinterviewquestions/Problem_0139_WordBreak.java

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
package topinterviewquestions;
22

3+
import java.util.HashSet;
34
import java.util.List;
45

56
public class Problem_0139_WordBreak {
67

7-
public static class Node {
8-
public boolean end;
9-
public Node[] nexts;
10-
11-
public Node() {
12-
end = false;
13-
nexts = new Node[26];
14-
}
15-
}
16-
178
public static boolean wordBreak(String s, List<String> wordDict) {
189
Node root = new Node();
1910
for (String str : wordDict) {
@@ -50,4 +41,94 @@ public static boolean wordBreak(String s, List<String> wordDict) {
5041
return dp[0];
5142
}
5243

44+
public static boolean wordBreak2(String s, List<String> wordDict) {
45+
return process(s, 0, new HashSet<>(wordDict)) != 0;
46+
}
47+
48+
// s[0......index-1]这一段,已经分解过了,不用在操心
49+
// s[index.....] 这一段字符串,能够被分解的方法数,返回
50+
public static int process(String s, int index, HashSet<String> wordDict) {
51+
if (index == s.length()) {
52+
return 1;
53+
}
54+
// index没到最后
55+
// index...index
56+
// index...index+1
57+
// index....index+2
58+
// index....end
59+
int ways = 0;
60+
for (int end = index; end < s.length(); end++) {
61+
// s[index....end]
62+
String pre = s.substring(index, end + 1);
63+
if (wordDict.contains(pre)) {
64+
ways += process(s, end + 1, wordDict);
65+
}
66+
}
67+
return ways;
68+
}
69+
70+
public static boolean wordBreak3(String s, List<String> wordDict) {
71+
HashSet<String> set = new HashSet<>(wordDict);
72+
int N = s.length();
73+
int[] dp = new int[N + 1];
74+
// dp[i] = process(s, i, set)的返回值
75+
dp[N] = 1;
76+
for (int index = N - 1; index >= 0; index--) {
77+
int ways = 0;
78+
for (int end = index; end < s.length(); end++) {
79+
// s[index....end]
80+
String pre = s.substring(index, end + 1);
81+
if (set.contains(pre)) {
82+
ways += dp[end + 1];
83+
}
84+
}
85+
dp[index] = ways;
86+
}
87+
return dp[0] != 0;
88+
}
89+
90+
public static class Node {
91+
public boolean end;
92+
public Node[] nexts;
93+
94+
public Node() {
95+
end = false;
96+
nexts = new Node[26];
97+
}
98+
}
99+
100+
public static boolean wordBreak4(String s, List<String> wordDict) {
101+
Node root = new Node();
102+
for (String str : wordDict) {
103+
char[] chs = str.toCharArray();
104+
Node node = root;
105+
int index = 0;
106+
for (int i = 0; i < chs.length; i++) {
107+
index = chs[i] - 'a';
108+
if (node.nexts[index] == null) {
109+
node.nexts[index] = new Node();
110+
}
111+
node = node.nexts[index];
112+
}
113+
node.end = true;
114+
}
115+
char[] str = s.toCharArray();
116+
int N = str.length;
117+
int[] dp = new int[N + 1];
118+
dp[N] = 1;
119+
for (int i = N - 1; i >= 0; i--) {
120+
Node cur = root;
121+
for (int end = i; end < N; end++) {
122+
cur = cur.nexts[str[end] - 'a'];
123+
if (cur == null) {
124+
break;
125+
}
126+
if (cur.end) {
127+
dp[i] += dp[end + 1];
128+
}
129+
}
130+
}
131+
return dp[0] != 0;
132+
}
133+
53134
}

src/topinterviewquestions/Problem_0146_LRUCache.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,35 @@ public void addNode(Node<K, V> newNode) {
6060
}
6161

6262
public void moveNodeToTail(Node<K, V> node) {
63-
if (this.tail == node) {
63+
if (tail == node) {
6464
return;
6565
}
66-
if (this.head == node) {
67-
this.head = node.next;
68-
this.head.last = null;
66+
// node 不是尾巴
67+
if (head == node) {
68+
head = node.next;
69+
head.last = null;
6970
} else {
7071
node.last.next = node.next;
7172
node.next.last = node.last;
7273
}
73-
node.last = this.tail;
74+
node.last = tail;
7475
node.next = null;
75-
this.tail.next = node;
76-
this.tail = node;
76+
tail.next = node;
77+
tail = node;
7778
}
7879

7980
public Node<K, V> removeHead() {
80-
if (this.head == null) {
81+
if (head == null) {
8182
return null;
8283
}
83-
Node<K, V> res = this.head;
84-
if (this.head == this.tail) { // 链表中只有一个节点的时候
85-
this.head = null;
86-
this.tail = null;
84+
Node<K, V> res = head;
85+
if (head == tail) { // 链表中只有一个节点的时候
86+
head = null;
87+
tail = null;
8788
} else {
88-
this.head = res.next;
89+
head = res.next;
8990
res.next = null;
90-
this.head.last = null;
91+
head.last = null;
9192
}
9293
return res;
9394
}
@@ -123,12 +124,12 @@ public void set(K key, V value) {
123124
node.value = value;
124125
nodeList.moveNodeToTail(node);
125126
} else {
127+
if (keyNodeMap.size() == capacity) {
128+
removeMostUnusedCache();
129+
}
126130
Node<K, V> newNode = new Node<K, V>(key, value);
127131
keyNodeMap.put(key, newNode);
128132
nodeList.addNode(newNode);
129-
if (keyNodeMap.size() == capacity + 1) {
130-
removeMostUnusedCache();
131-
}
132133
}
133134
}
134135

src/topinterviewquestions/Problem_0149_MaxPointsOnALine.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ public static int maxPoints(int[][] points) {
1212
if (points.length <= 2) {
1313
return points.length;
1414
}
15+
// Map<String, Integer> "3_5" 6
16+
// 3 / 5 4
17+
// 3 / 7 10
18+
// 3 / 17 11
19+
// 5 / 7 9
20+
// 5 / 9 3
21+
// 3 : ( 5 , 4 7, 10, 17 , 11 )
22+
// 5 : ( 7 , 9 9, 3 )
1523
Map<Integer, Map<Integer, Integer>> map = new HashMap<Integer, Map<Integer, Integer>>();
1624
int result = 0;
1725
for (int i = 0; i < points.length; i++) {
1826
map.clear();
1927
int samePosition = 1;
2028
int sameX = 0;
2129
int sameY = 0;
22-
int line = 0;
30+
int line = 0; // 哪个斜率压中的点最多,把最多的点的数量,赋值给line
2331
for (int j = i + 1; j < points.length; j++) {
2432
int x = points[j][0] - points[i][0];
2533
int y = points[j][1] - points[i][1];

src/topinterviewquestions/Problem_0152_MaximumProductSubarray.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public static int maxProduct(int[] nums) {
66
int ans = nums[0];
77
int min = nums[0];
88
int max = nums[0];
9+
// [0]
910
for (int i = 1; i < nums.length; i++) {
1011
int curmin = Math.min(nums[i], Math.min(min * nums[i], max * nums[i]));
1112
int curmax = Math.max(nums[i], Math.max(min * nums[i], max * nums[i]));

src/topinterviewquestions/Problem_0160_IntersectionOfTwoLinkedLists.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ public static ListNode getIntersectionNode(ListNode head1, ListNode head2) {
1818
n++;
1919
cur1 = cur1.next;
2020
}
21+
// cur1 end1
2122
while (cur2.next != null) {
2223
n--;
2324
cur2 = cur2.next;
2425
}
26+
// cur2 end2
2527
if (cur1 != cur2) {
2628
return null;
2729
}
28-
cur1 = n > 0 ? head1 : head2;
30+
cur1 = n > 0 ? head1 : head2; // 谁是长链表,谁把头节点,给cur1赋值
2931
cur2 = cur1 == head1 ? head2 : head1;
3032
n = Math.abs(n);
3133
while (n != 0) {

src/topinterviewquestions/Problem_0190_ReverseBits.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Problem_0190_ReverseBits {
3838

3939

4040
public static int reverseBits(int n) {
41+
// n
4142
n = (n >>> 16) | (n << 16);
4243
n = ((n & 0xff00ff00) >>> 8) | ((n & 0x00ff00ff) << 8);
4344
n = ((n & 0xf0f0f0f0) >>> 4) | ((n & 0x0f0f0f0f) << 4);

0 commit comments

Comments
 (0)