Skip to content

Commit cf0f74d

Browse files
author
rchen102
committed
review
1 parent a21142d commit cf0f74d

11 files changed

+164
-48
lines changed

Java/leetcode 1021.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Solution1: stack T: O(n) S: O(n)
2+
class Solution {
3+
public String removeOuterParentheses(String S) {
4+
Stack<Character> stack = new Stack<>();
5+
StringBuilder res = new StringBuilder();
6+
for (char ch : S.toCharArray()) {
7+
if (ch == '(') {
8+
if (!stack.isEmpty()) res.append(ch);
9+
stack.push(ch);
10+
}
11+
if (ch == ')') {
12+
stack.pop();
13+
if (!stack.isEmpty()) res.append(ch);
14+
}
15+
}
16+
return res.toString();
17+
}
18+
}
19+
20+
// Solution2: 省略stack T: O(n) S: O(1) (not including result)
21+
class Solution {
22+
public String removeOuterParentheses(String S) {
23+
int num = 0; // num of stack
24+
StringBuilder res = new StringBuilder();
25+
for (char ch : S.toCharArray()) {
26+
if (ch == '(') {
27+
if (num != 0) res.append(ch);
28+
num++;
29+
}
30+
if (ch == ')') {
31+
num--;
32+
if (num != 0) res.append(ch);
33+
}
34+
}
35+
return res.toString();
36+
}
37+
}

Java/leetcode 1249.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Solution1: stack + placeholder T: O(n) S: O(n)
2+
class Solution {
3+
public String minRemoveToMakeValid(String s) {
4+
Stack<Integer> stack = new Stack<>();
5+
StringBuilder res = new StringBuilder(s);
6+
for (int i = 0; i < s.length(); i++) {
7+
char ch = s.charAt(i);
8+
if (ch == '(') stack.push(i);
9+
if (ch == ')') {
10+
if (!stack.isEmpty()) stack.pop();
11+
else res.setCharAt(i, '$');
12+
}
13+
}
14+
while (!stack.isEmpty()) {
15+
int idx = stack.pop();
16+
res.setCharAt(idx, '$');
17+
}
18+
return res.toString().replaceAll("\\$", "");
19+
}
20+
}

Java/leetcode 142.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ public class Solution {
1414
public ListNode detectCycle(ListNode head) {
1515
ListNode slow = head;
1616
ListNode fast = head;
17-
int flag = 0;
17+
boolean hasCycle = false;
1818
while (fast != null && fast.next != null) {
1919
slow = slow.next;
2020
fast = fast.next.next;
2121
if (slow == fast) {
22-
flag = 1;
22+
hasCycle = true;
2323
break;
2424
}
2525
}
26-
if (flag == 0) return null;
27-
fast = head;
28-
while (slow != head) {
26+
if (!hasCycle) return null;
27+
slow = head;
28+
while (slow != fast) {
2929
slow = slow.next;
30-
head = head.next;
31-
}
30+
fast = fast.next;
31+
}
3232
return slow;
3333
}
3434
}

Java/leetcode 147.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* }
88
*/
99

10-
// Solution1: recursive
10+
// Solution1: recursive 不完全符合题意
1111
// T: O(n^2) S: O(n)
1212
class Solution {
1313
public ListNode insertionSortList(ListNode head) {
@@ -27,6 +27,35 @@ public ListNode insertionSortList(ListNode head) {
2727
}
2828

2929
// Solution2: iterative T: O(n^2) S: O(1)
30+
class Solution {
31+
public ListNode insertionSortList(ListNode head) {
32+
if (head == null || head.next == null) return head;
33+
ListNode newHead = head;
34+
ListNode cur = newHead.next;
35+
newHead.next = null;
36+
while (cur != null) {
37+
ListNode next = cur.next;
38+
cur.next = null; // clean tail, do not forget
39+
newHead = insertNode(newHead, cur);
40+
cur = next;
41+
}
42+
return newHead;
43+
}
44+
45+
private ListNode insertNode(ListNode head, ListNode node) {
46+
ListNode dummy = new ListNode(-1);
47+
dummy.next = head;
48+
ListNode prev = dummy;
49+
while (prev.next != null && prev.next.val < node.val) {
50+
prev = prev.next;
51+
}
52+
node.next = prev.next;
53+
prev.next = node;
54+
return dummy.next;
55+
}
56+
}
57+
58+
3059
class Solution {
3160
public ListNode insertionSortList(ListNode head) {
3261
ListNode dummy = new ListNode(-1);

Java/leetcode 203.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ public ListNode removeElements(ListNode head, int val) {
2121
}
2222

2323
// Solution2: iterative T: O(n) S: O(1)
24+
// 更简洁,完全不需要清理尾巴
25+
class Solution {
26+
public ListNode removeElements(ListNode head, int val) {
27+
ListNode dummy = new ListNode(-1);
28+
dummy.next = head;
29+
ListNode prev = dummy;
30+
while (prev.next != null) {
31+
if (prev.next.val == val) prev.next = prev.next.next;
32+
else prev = prev.next;
33+
}
34+
return dummy.next;
35+
}
36+
}
37+
2438
class Solution {
2539
public ListNode removeElements(ListNode head, int val) {
2640
ListNode dummy = new ListNode(-1);

Java/leetcode 206.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@
1111
class Solution {
1212
public ListNode reverseList(ListNode head) {
1313
if (head == null || head.next == null) return head;
14-
ListNode tail = reverseList(head.next);
15-
ListNode prev = tail;
14+
ListNode newHead = reverseList(head.next);
15+
ListNode prev = newHead;
1616
while (prev.next != null) prev = prev.next;
1717
prev.next = head;
18-
head.next = null; // 清理尾巴,不然死循环
19-
return tail;
18+
head.next = null; // 清理尾巴
19+
return newHead;
2020
}
2121
}
2222

2323
// Solution2: iterative T: O(n) S: O(1)
2424
class Solution {
2525
public ListNode reverseList(ListNode head) {
2626
ListNode dummy = new ListNode(-1);
27+
ListNode prev = dummy;
2728
while (head != null) {
28-
ListNode tmp = dummy.next;
29-
dummy.next = head;
30-
head = head.next; // points to next node
31-
dummy.next.next = tmp;
29+
ListNode next = head.next;
30+
head.next = prev.next;
31+
prev.next = head;
32+
head = next;
3233
}
3334
return dummy.next;
3435
}

Java/leetcode 234.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,33 @@
1010
// T: O(n) S: O(1)
1111
class Solution {
1212
public boolean isPalindrome(ListNode head) {
13-
if (head == null) return true;
13+
ListNode mid = middleNode(head);
14+
mid = reverseNode(mid);
15+
while (mid != null) {
16+
if (mid.val != head.val) return false;
17+
mid = mid.next;
18+
head = head.next;
19+
}
20+
return true;
21+
}
22+
23+
private ListNode middleNode(ListNode head) {
1424
ListNode slow = head, fast = head;
15-
// Find middle node
1625
while (fast != null && fast.next != null) {
1726
slow = slow.next;
1827
fast = fast.next.next;
1928
}
20-
ListNode newHead1, newHead2;
21-
newHead1 = head;
22-
if (fast == null) newHead2 = slow; // even number
23-
else newHead2 = slow.next; // odd number
24-
25-
newHead2 = reverse(newHead2);
26-
while (newHead2 != null) {
27-
if (newHead1.val != newHead2.val) return false;
28-
newHead1 = newHead1.next;
29-
newHead2 = newHead2.next;
30-
}
31-
return true;
29+
return slow;
3230
}
33-
34-
public ListNode reverse(ListNode head) {
31+
32+
private ListNode reverseNode(ListNode head) {
3533
ListNode dummy = new ListNode(-1);
34+
ListNode prev = dummy;
3635
while (head != null) {
37-
ListNode tmp = head.next;
38-
head.next = dummy.next;
39-
dummy.next = head;
40-
head = tmp;
36+
ListNode next = head.next;
37+
head.next = prev.next;
38+
prev.next = head;
39+
head = next;
4140
}
4241
return dummy.next;
4342
}

Java/leetcode 24.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public ListNode swapPairs(ListNode head) {
2424
ListNode dummy = new ListNode(-1);
2525
dummy.next = head;
2626
ListNode prev = dummy;
27-
ListNode cur = head;
28-
while (cur != null && cur.next != null) {
29-
prev.next = cur.next; // cur.next is new head, the one to be swapped
30-
cur.next = cur.next.next; // connect to next candidate for cur
31-
prev.next.next = cur; // complete swap
32-
prev = cur; // update prev and cur
33-
cur = cur.next;
27+
while (prev.next != null && prev.next.next != null) {
28+
ListNode left = prev.next; // first node to be swapped
29+
ListNode right = prev.next.next; // second nodeto be swapped
30+
prev.next = right;
31+
left.next = right.next;
32+
right.next = left;
33+
prev = left;
3434
}
3535
return dummy.next;
3636
}

Java/leetcode 83.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public ListNode deleteDuplicates(ListNode head) {
1313
ListNode dummy = new ListNode(-1);
1414
dummy.next = head;
1515
ListNode cur = head;
16-
while (cur.next != null) {
16+
while (cur != null && cur.next != null) {
1717
if (cur.val == cur.next.val) cur.next = cur.next.next;
1818
else cur = cur.next;
1919
}

Java/leetcode 86.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public ListNode partition(ListNode head, int x) {
2020
ListNode dummy = new ListNode(-1);
2121
dummy.next = tail;
2222
ListNode prev = dummy;
23-
while (prev.next != null) {
24-
if (prev.next.val >= x) break;
25-
prev = prev.next;
26-
}
23+
while (prev.next != null && prev.next.val < x) prev = prev.next;
2724
head.next = prev.next;
2825
prev.next = head;
2926
return dummy.next;

Java/leetcode 921.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Solution1: T: O(n) S: O(1)
2+
class Solution {
3+
public int minAddToMakeValid(String S) {
4+
int stack_size = 0;
5+
int miss_match = 0;
6+
for (char ch : S.toCharArray()) {
7+
if (ch == '(') {
8+
stack_size++; // push
9+
}
10+
else if (ch == ')' && stack_size > 0) {
11+
stack_size--; // pop
12+
}
13+
else {
14+
miss_match++;
15+
}
16+
}
17+
return stack_size + miss_match;
18+
}
19+
}

0 commit comments

Comments
 (0)