Skip to content

Commit 575f2df

Browse files
2 parents 6b4cd7c + 7a591d2 commit 575f2df

31 files changed

+512
-343
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Companies | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1047.java) | O(n) | O(1) | |Easy||
3031
|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1037.java) | O(1) | O(1) | |Easy|Math|
3132
|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1033.java) | O(1) | O(1) | |Easy|Math|
3233
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | O(R*C) | O(1) | |Easy|

src/main/java/com/fishercoder/solutions/_102.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ public List<List<Integer>> levelOrder(TreeNode root) {
3737
if (root == null) {
3838
return result;
3939
}
40-
Queue<TreeNode> q = new LinkedList();
41-
q.offer(root);
42-
while (!q.isEmpty()) {
40+
Queue<TreeNode> queue = new LinkedList();
41+
queue.offer(root);
42+
while (!queue.isEmpty()) {
4343
List<Integer> thisLevel = new ArrayList();
44-
int qSize = q.size();
45-
for (int i = 0; i < qSize; i++) {
46-
TreeNode curr = q.poll();
44+
int size = queue.size();
45+
for (int i = 0; i < size; i++) {
46+
TreeNode curr = queue.poll();
4747
thisLevel.add(curr.val);
4848
if (curr.left != null) {
49-
q.offer(curr.left);
49+
queue.offer(curr.left);
5050
}
5151
if (curr.right != null) {
52-
q.offer(curr.right);
52+
queue.offer(curr.right);
5353
}
5454
}
5555
result.add(thisLevel);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1047. Remove All Adjacent Duplicates In String
5+
*
6+
* Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
7+
*
8+
* We repeatedly make duplicate removals on S until we no longer can.
9+
*
10+
* Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
11+
*
12+
*
13+
*
14+
* Example 1:
15+
*
16+
* Input: "abbaca"
17+
* Output: "ca"
18+
* Explanation:
19+
* For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.
20+
* The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
21+
*
22+
*
23+
* Note:
24+
*
25+
* 1 <= S.length <= 20000
26+
* S consists only of English lowercase letters.*/
27+
public class _1047 {
28+
public static class Solution1 {
29+
public String removeDuplicates(String S) {
30+
StringBuilder sb = new StringBuilder(S);
31+
for (int i = 0; i < S.length() - 1; i++) {
32+
if (S.charAt(i) == S.charAt(i + 1)) {
33+
return removeDuplicates(S.substring(0, i) + S.substring(i + 2));
34+
}
35+
}
36+
return sb.toString();
37+
}
38+
}
39+
}

src/main/java/com/fishercoder/solutions/_138.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
public class _138 {
1515
public static class Solution1 {
16-
public RandomListNode copyRandomList(RandomListNode head) {
16+
public Node copyRandomList(Node head) {
1717
/**Key is the original nodes, value is the new nodes we're deep copying to.*/
18-
Map<RandomListNode, RandomListNode> map = new HashMap();
19-
RandomListNode node = head;
18+
Map<Node, Node> map = new HashMap();
19+
Node node = head;
2020

2121
//loop for the first time: copy the node themselves with only labels
2222
while (node != null) {
23-
map.put(node, new RandomListNode(node.label));
23+
map.put(node, new Node(node.val));
2424
node = node.next;
2525
}
2626

@@ -36,14 +36,14 @@ public RandomListNode copyRandomList(RandomListNode head) {
3636
}
3737

3838
// Definition for singly-linked list with a random pointer.
39-
class RandomListNode {
40-
int label;
39+
class Node {
40+
int val;
4141

42-
RandomListNode next;
43-
RandomListNode random;
42+
Node next;
43+
Node random;
4444

45-
RandomListNode(int x) {
46-
this.label = x;
45+
Node(int x) {
46+
this.val = x;
4747
}
4848
}
4949
}

src/main/java/com/fishercoder/solutions/_146.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class Solution1 {
3737
public class LRUCache {
3838
/**
3939
* The shortest implementation is to use LinkedHashMap:
40-
* specify a size of the linkedHashMap;
40+
* specify a size of the LinkedHashMap;
4141
* override the removeEldestEntry method when its size exceeds max size:
4242
* https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#removeEldestEntry-java.util.Map.Entry-
4343
* in the constructor, set the last boolean variable to be true: it means the ordering mode,

src/main/java/com/fishercoder/solutions/_15.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public List<List<Integer>> threeSum(int[] nums) {
3838
if (sum == 0) {
3939
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
4040

41-
while (left + 1 < right && nums[left] == nums[left + 1]) {
41+
while (left < right && nums[left] == nums[left + 1]) {
4242
left++;
4343
}
4444

45-
while (right - 1 > left && nums[right] == nums[right - 1]) {
45+
while (right > left && nums[right] == nums[right - 1]) {
4646
right--;
4747
}
4848
left++;

src/main/java/com/fishercoder/solutions/_160.java

+45
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,49 @@
1919
B: b1 → b2 → b3
2020
begin to intersect at node c1.
2121
22+
Example 1:
23+
A: 4 → 1
24+
25+
8 → 4 → 5
26+
27+
B: 5 → 0 → 1
28+
29+
30+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
31+
Output: Reference of the node with value = 8
32+
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
33+
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5].
34+
There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
35+
36+
37+
Example 2:
38+
A: 0 -> 9 → 1
39+
40+
2 → 4
41+
42+
B: 3
43+
44+
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
45+
Output: Reference of the node with value = 2
46+
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
47+
From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4].
48+
There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
49+
50+
51+
Example 3:
52+
A: 2 → 6 -> 4
53+
54+
B: 1 -> 5
55+
56+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
57+
Output: null
58+
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5].
59+
Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
60+
Explanation: The two lists do not intersect, so return null.
61+
2262
2363
Notes:
64+
2465
If the two linked lists have no intersection at all, return null.
2566
The linked lists must retain their original structure after the function returns.
2667
You may assume there are no cycles anywhere in the entire linked structure.
@@ -66,6 +107,8 @@ private int findLen(ListNode head) {
66107

67108
public static class Solution2 {
68109
/**
110+
* Most optimal solution:
111+
*
69112
* O(m+n) time
70113
* O(1) space
71114
* credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len*/
@@ -77,7 +120,9 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
77120
ListNode a = headA;
78121
ListNode b = headB;
79122

123+
/**if a and b have different lengths, then it will stop the loop after second iteration*/
80124
while (a != b) {
125+
/**for the first iteration, it'll just reset the pointer to the head of another linkedlist*/
81126
a = a == null ? headB : a.next;
82127
b = b == null ? headA : b.next;
83128
}

src/main/java/com/fishercoder/solutions/_189.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
public class _189 {
3333

3434
public static class Solution1 {
35+
/**
36+
* O(n) space
37+
* O(n) time
38+
* */
3539
public void rotate(int[] nums, int k) {
3640
int len = nums.length;
3741
int[] tmp = new int[len];
@@ -45,9 +49,26 @@ public void rotate(int[] nums, int k) {
4549
}
4650

4751
public static class Solution2 {
52+
/**
53+
* O(1) space
54+
* O(n) time
55+
* */
56+
public void rotate(int[] nums, int k) {
57+
int tmp;
58+
for (int i = 0; i < k; i++) {
59+
tmp = nums[nums.length - 1];
60+
for (int j = nums.length - 1; j > 0; j--) {
61+
nums[j] = nums[j - 1];
62+
}
63+
nums[0] = tmp;
64+
}
65+
}
66+
}
67+
68+
public static class Solution3 {
4869
/**
4970
* My original idea and got AC'ed.
50-
* One thing to notice is that when k > nums.length, we'll continue to rotate_naive the array, it just becomes k -= nums.length
71+
* One thing to notice is that when k > nums.length, we'll continue to rotate the array, it just becomes k -= nums.length
5172
*/
5273
public static void rotate(int[] nums, int k) {
5374
if (k == 0 || k == nums.length) {
@@ -77,18 +98,4 @@ public static void rotate(int[] nums, int k) {
7798
}
7899
}
79100
}
80-
81-
public static class Solution3 {
82-
public void rotate(int[] nums, int k) {
83-
int tmp;
84-
for (int i = 0; i < k; i++) {
85-
tmp = nums[nums.length - 1];
86-
for (int j = nums.length - 1; j > 0; j--) {
87-
nums[j] = nums[j - 1];
88-
}
89-
nums[0] = tmp;
90-
}
91-
}
92-
}
93-
94101
}

src/main/java/com/fishercoder/solutions/_199.java

+41-18
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,52 @@
2424

2525
public class _199 {
2626

27-
public List<Integer> rightSideView(TreeNode root) {
28-
List<Integer> result = new ArrayList<>();
29-
if (root == null) {
27+
public static class Solution1 {
28+
/**credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA)*/
29+
public List<Integer> rightSideView(TreeNode root) {
30+
List<Integer> result = new ArrayList<>();
31+
rightView(root, result, 0);
3032
return result;
3133
}
32-
Queue<TreeNode> q = new LinkedList<>();
33-
q.offer(root);
34-
while (!q.isEmpty()) {
35-
int size = q.size();
36-
for (int i = 0; i < size; i++) {
37-
TreeNode curr = q.poll();
38-
if (i == size - 1) {
39-
result.add(curr.val);
40-
}
41-
if (curr.left != null) {
42-
q.offer(curr.left);
43-
}
44-
if (curr.right != null) {
45-
q.offer(curr.right);
34+
35+
void rightView(TreeNode curr, List<Integer> result, int currDepth) {
36+
if (curr == null) {
37+
return;
38+
}
39+
if (currDepth == result.size()) {
40+
result.add(curr.val);
41+
}
42+
rightView(curr.right, result, currDepth + 1);
43+
rightView(curr.left, result, currDepth + 1);
44+
}
45+
}
46+
47+
public static class Solution2 {
48+
/**BFS the tree*/
49+
public List<Integer> rightSideView(TreeNode root) {
50+
List<Integer> result = new ArrayList<>();
51+
if (root == null) {
52+
return result;
53+
}
54+
Queue<TreeNode> q = new LinkedList<>();
55+
q.offer(root);
56+
while (!q.isEmpty()) {
57+
int size = q.size();
58+
for (int i = 0; i < size; i++) {
59+
TreeNode curr = q.poll();
60+
if (i == size - 1) {
61+
result.add(curr.val);
62+
}
63+
if (curr.left != null) {
64+
q.offer(curr.left);
65+
}
66+
if (curr.right != null) {
67+
q.offer(curr.right);
68+
}
4669
}
4770
}
71+
return result;
4872
}
49-
return result;
5073
}
5174

5275
}

src/main/java/com/fishercoder/solutions/_2.java

-32
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3030
l2 = l2.next;
3131
}
3232
tmp.next = new ListNode(sum % 10);
33-
;
3433
tmp = tmp.next;
3534
}
3635
if (sum / 10 == 1) {
@@ -40,35 +39,4 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
4039
}
4140
}
4241

43-
public static class Solution2 {
44-
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
45-
ListNode pre = new ListNode(-1);
46-
ListNode head = new ListNode(0);
47-
pre.next = head;
48-
int carry = 0;
49-
while (l1 != null || l2 != null) {
50-
int val = carry;
51-
if (l1 != null) {
52-
val += l1.val;
53-
l1 = l1.next;
54-
}
55-
if (l2 != null) {
56-
val += l2.val;
57-
l2 = l2.next;
58-
}
59-
if (val >= 10) {
60-
val %= 10;
61-
carry = 1;
62-
} else {
63-
carry = 0;
64-
}
65-
head.next = new ListNode(val);
66-
head = head.next;
67-
}
68-
if (carry != 0) {
69-
head.next = new ListNode(carry);
70-
}
71-
return pre.next.next;
72-
}
73-
}
7442
}

0 commit comments

Comments
 (0)