Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 66802d0

Browse files
committedSep 5, 2024·
Corrected line endings from CRLF to LF
1 parent 0a0ec57 commit 66802d0

File tree

16 files changed

+532
-533
lines changed

16 files changed

+532
-533
lines changed
 
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.TreeNode;
4-
5-
public class _100 {
6-
public static class Solution1 {
7-
public boolean isSameTree(TreeNode p, TreeNode q) {
8-
if (p == null || q == null) {
9-
return p == q;
10-
}
11-
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
12-
}
13-
}
14-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
public class _100 {
6+
public static class Solution1 {
7+
public boolean isSameTree(TreeNode p, TreeNode q) {
8+
if (p == null || q == null) {
9+
return p == q;
10+
}
11+
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
12+
}
13+
}
14+
}
Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.TreeNode;
4-
5-
public class _101 {
6-
public static class Solution1 {
7-
public boolean isSymmetric(TreeNode root) {
8-
if (root == null) {
9-
return true;
10-
}
11-
return isSymmetric(root.left, root.right);
12-
}
13-
14-
private boolean isSymmetric(TreeNode left, TreeNode right) {
15-
if (left == null || right == null) {
16-
return left == right;
17-
}
18-
return left.val == right.val
19-
&& isSymmetric(left.left, right.right)
20-
&& isSymmetric(left.right, right.left);
21-
}
22-
}
23-
24-
public static class Solution2 {
25-
/*
26-
* The same as the above solution, just a bit more verbose.
27-
*/
28-
public boolean isSymmetric(TreeNode root) {
29-
if (root == null) {
30-
return true;
31-
}
32-
return isSymmetric(root.left, root.right);
33-
}
34-
35-
private boolean isSymmetric(TreeNode left, TreeNode right) {
36-
if (left == null && right == null) {
37-
return true;
38-
} else if (left == null || right == null) {
39-
return false;
40-
}
41-
if (left.val == right.val) {
42-
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
43-
} else {
44-
return false;
45-
}
46-
}
47-
}
48-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
public class _101 {
6+
public static class Solution1 {
7+
public boolean isSymmetric(TreeNode root) {
8+
if (root == null) {
9+
return true;
10+
}
11+
return isSymmetric(root.left, root.right);
12+
}
13+
14+
private boolean isSymmetric(TreeNode left, TreeNode right) {
15+
if (left == null || right == null) {
16+
return left == right;
17+
}
18+
return left.val == right.val
19+
&& isSymmetric(left.left, right.right)
20+
&& isSymmetric(left.right, right.left);
21+
}
22+
}
23+
24+
public static class Solution2 {
25+
/*
26+
* The same as the above solution, just a bit more verbose.
27+
*/
28+
public boolean isSymmetric(TreeNode root) {
29+
if (root == null) {
30+
return true;
31+
}
32+
return isSymmetric(root.left, root.right);
33+
}
34+
35+
private boolean isSymmetric(TreeNode left, TreeNode right) {
36+
if (left == null && right == null) {
37+
return true;
38+
} else if (left == null || right == null) {
39+
return false;
40+
}
41+
if (left.val == right.val) {
42+
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
43+
} else {
44+
return false;
45+
}
46+
}
47+
}
48+
}
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.TreeNode;
4-
import java.util.ArrayList;
5-
import java.util.LinkedList;
6-
import java.util.List;
7-
import java.util.Queue;
8-
9-
public class _102 {
10-
11-
public static class Solution1 {
12-
public List<List<Integer>> levelOrder(TreeNode root) {
13-
List<List<Integer>> result = new ArrayList<>();
14-
if (root == null) {
15-
return result;
16-
}
17-
Queue<TreeNode> queue = new LinkedList();
18-
queue.offer(root);
19-
while (!queue.isEmpty()) {
20-
List<Integer> thisLevel = new ArrayList();
21-
int size = queue.size();
22-
for (int i = 0; i < size; i++) {
23-
TreeNode curr = queue.poll();
24-
thisLevel.add(curr.val);
25-
if (curr.left != null) {
26-
queue.offer(curr.left);
27-
}
28-
if (curr.right != null) {
29-
queue.offer(curr.right);
30-
}
31-
}
32-
result.add(thisLevel);
33-
}
34-
return result;
35-
}
36-
}
37-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Queue;
8+
9+
public class _102 {
10+
11+
public static class Solution1 {
12+
public List<List<Integer>> levelOrder(TreeNode root) {
13+
List<List<Integer>> result = new ArrayList<>();
14+
if (root == null) {
15+
return result;
16+
}
17+
Queue<TreeNode> queue = new LinkedList();
18+
queue.offer(root);
19+
while (!queue.isEmpty()) {
20+
List<Integer> thisLevel = new ArrayList();
21+
int size = queue.size();
22+
for (int i = 0; i < size; i++) {
23+
TreeNode curr = queue.poll();
24+
thisLevel.add(curr.val);
25+
if (curr.left != null) {
26+
queue.offer(curr.left);
27+
}
28+
if (curr.right != null) {
29+
queue.offer(curr.right);
30+
}
31+
}
32+
result.add(thisLevel);
33+
}
34+
return result;
35+
}
36+
}
37+
}
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.TreeNode;
4-
import java.util.ArrayList;
5-
import java.util.Collections;
6-
import java.util.LinkedList;
7-
import java.util.List;
8-
import java.util.Queue;
9-
10-
public class _107 {
11-
public static class Solution1 {
12-
public List<List<Integer>> levelOrderBottom(TreeNode root) {
13-
List<List<Integer>> result = new ArrayList();
14-
if (root == null) {
15-
return result;
16-
}
17-
Queue<TreeNode> q = new LinkedList();
18-
q.offer(root);
19-
while (!q.isEmpty()) {
20-
List<Integer> thisLevel = new ArrayList<>();
21-
int qSize = q.size();
22-
for (int i = 0; i < qSize; i++) {
23-
TreeNode curr = q.poll();
24-
thisLevel.add(curr.val);
25-
if (curr.left != null) {
26-
q.offer(curr.left);
27-
}
28-
if (curr.right != null) {
29-
q.offer(curr.right);
30-
}
31-
}
32-
result.add(thisLevel);
33-
}
34-
Collections.reverse(result);
35-
return result;
36-
}
37-
}
38-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
public class _107 {
11+
public static class Solution1 {
12+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
13+
List<List<Integer>> result = new ArrayList();
14+
if (root == null) {
15+
return result;
16+
}
17+
Queue<TreeNode> q = new LinkedList();
18+
q.offer(root);
19+
while (!q.isEmpty()) {
20+
List<Integer> thisLevel = new ArrayList<>();
21+
int qSize = q.size();
22+
for (int i = 0; i < qSize; i++) {
23+
TreeNode curr = q.poll();
24+
thisLevel.add(curr.val);
25+
if (curr.left != null) {
26+
q.offer(curr.left);
27+
}
28+
if (curr.right != null) {
29+
q.offer(curr.right);
30+
}
31+
}
32+
result.add(thisLevel);
33+
}
34+
Collections.reverse(result);
35+
return result;
36+
}
37+
}
38+
}
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.ListNode;
4-
import java.util.HashSet;
5-
import java.util.Set;
6-
7-
public class _141 {
8-
9-
public static class Solution1 {
10-
public boolean hasCycle(ListNode head) {
11-
Set<ListNode> set = new HashSet();
12-
while (head != null) {
13-
if (!set.add(head)) {
14-
return true;
15-
}
16-
head = head.next;
17-
}
18-
return false;
19-
}
20-
}
21-
22-
public static class Solution2 {
23-
public boolean hasCycle(ListNode head) {
24-
ListNode slow = head;
25-
ListNode fast = head;
26-
while (fast != null && fast.next != null) {
27-
fast = fast.next.next;
28-
slow = slow.next;
29-
if (fast == slow) {
30-
return true;
31-
}
32-
}
33-
return false;
34-
}
35-
}
36-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class _141 {
8+
9+
public static class Solution1 {
10+
public boolean hasCycle(ListNode head) {
11+
Set<ListNode> set = new HashSet();
12+
while (head != null) {
13+
if (!set.add(head)) {
14+
return true;
15+
}
16+
head = head.next;
17+
}
18+
return false;
19+
}
20+
}
21+
22+
public static class Solution2 {
23+
public boolean hasCycle(ListNode head) {
24+
ListNode slow = head;
25+
ListNode fast = head;
26+
while (fast != null && fast.next != null) {
27+
fast = fast.next.next;
28+
slow = slow.next;
29+
if (fast == slow) {
30+
return true;
31+
}
32+
}
33+
return false;
34+
}
35+
}
36+
}
Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.TreeNode;
4-
import java.util.ArrayList;
5-
import java.util.LinkedList;
6-
import java.util.List;
7-
import java.util.Queue;
8-
9-
public class _199 {
10-
11-
public static class Solution1 {
12-
/*
13-
* credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA)
14-
*/
15-
public List<Integer> rightSideView(TreeNode root) {
16-
List<Integer> result = new ArrayList<>();
17-
rightView(root, result, 0);
18-
return result;
19-
}
20-
21-
void rightView(TreeNode curr, List<Integer> result, int currDepth) {
22-
if (curr == null) {
23-
return;
24-
}
25-
if (currDepth == result.size()) {
26-
result.add(curr.val);
27-
}
28-
// go through right side first as we want right side view, so as soon as we find it,
29-
// then we won't use the one from left side
30-
rightView(curr.right, result, currDepth + 1);
31-
rightView(curr.left, result, currDepth + 1);
32-
}
33-
}
34-
35-
public static class Solution2 {
36-
/*
37-
* BFS the tree
38-
*/
39-
public List<Integer> rightSideView(TreeNode root) {
40-
List<Integer> result = new ArrayList<>();
41-
if (root == null) {
42-
return result;
43-
}
44-
Queue<TreeNode> q = new LinkedList<>();
45-
q.offer(root);
46-
while (!q.isEmpty()) {
47-
int size = q.size();
48-
for (int i = 0; i < size; i++) {
49-
TreeNode curr = q.poll();
50-
if (i == size - 1) {
51-
result.add(curr.val);
52-
}
53-
if (curr.left != null) {
54-
q.offer(curr.left);
55-
}
56-
if (curr.right != null) {
57-
q.offer(curr.right);
58-
}
59-
}
60-
}
61-
return result;
62-
}
63-
}
64-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Queue;
8+
9+
public class _199 {
10+
11+
public static class Solution1 {
12+
/*
13+
* credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA)
14+
*/
15+
public List<Integer> rightSideView(TreeNode root) {
16+
List<Integer> result = new ArrayList<>();
17+
rightView(root, result, 0);
18+
return result;
19+
}
20+
21+
void rightView(TreeNode curr, List<Integer> result, int currDepth) {
22+
if (curr == null) {
23+
return;
24+
}
25+
if (currDepth == result.size()) {
26+
result.add(curr.val);
27+
}
28+
// go through right side first as we want right side view, so as soon as we find it,
29+
// then we won't use the one from left side
30+
rightView(curr.right, result, currDepth + 1);
31+
rightView(curr.left, result, currDepth + 1);
32+
}
33+
}
34+
35+
public static class Solution2 {
36+
/*
37+
* BFS the tree
38+
*/
39+
public List<Integer> rightSideView(TreeNode root) {
40+
List<Integer> result = new ArrayList<>();
41+
if (root == null) {
42+
return result;
43+
}
44+
Queue<TreeNode> q = new LinkedList<>();
45+
q.offer(root);
46+
while (!q.isEmpty()) {
47+
int size = q.size();
48+
for (int i = 0; i < size; i++) {
49+
TreeNode curr = q.poll();
50+
if (i == size - 1) {
51+
result.add(curr.val);
52+
}
53+
if (curr.left != null) {
54+
q.offer(curr.left);
55+
}
56+
if (curr.right != null) {
57+
q.offer(curr.right);
58+
}
59+
}
60+
}
61+
return result;
62+
}
63+
}
64+
}
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.ListNode;
4-
5-
public class _203 {
6-
public static class Solution1 {
7-
/*
8-
* This is a very good question to test your understanding of pointers/memory/addresses, although it's marked as EASY.
9-
* All the three nodes: dummy, curr and prev are indispensable.
10-
* <p>
11-
* 1. Eventually, we should return dummy.next as the final result.
12-
* 2. we assign head to curr, dummy to prev
13-
* 3. and then we use prev and curr to traverse through the list and do the work
14-
* 4. each time, we only move one node forward, so we don't need another while loop inside the while loop
15-
* 5. KEY: if(curr.val == val), then curr is the node we want to remove, so, we'll assign curr.next to prev.next, thus, prev won't have that node
16-
* else, we'll keep moving prev forward, so, just do prev = prev.next
17-
* but, for both cases, we'll also move curr forward, so we put curr = curr.next in the outside.
18-
*/
19-
public ListNode removeElements(ListNode head, int val) {
20-
ListNode dummy = new ListNode(-1);
21-
dummy.next = head;
22-
ListNode curr = head;
23-
ListNode prev = dummy;
24-
while (curr != null) {
25-
if (curr.val == val) {
26-
prev.next = curr.next;
27-
} else {
28-
prev = prev.next;
29-
}
30-
curr = curr.next;
31-
}
32-
return dummy.next;
33-
}
34-
}
35-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
public class _203 {
6+
public static class Solution1 {
7+
/*
8+
* This is a very good question to test your understanding of pointers/memory/addresses, although it's marked as EASY.
9+
* All the three nodes: dummy, curr and prev are indispensable.
10+
* <p>
11+
* 1. Eventually, we should return dummy.next as the final result.
12+
* 2. we assign head to curr, dummy to prev
13+
* 3. and then we use prev and curr to traverse through the list and do the work
14+
* 4. each time, we only move one node forward, so we don't need another while loop inside the while loop
15+
* 5. KEY: if(curr.val == val), then curr is the node we want to remove, so, we'll assign curr.next to prev.next, thus, prev won't have that node
16+
* else, we'll keep moving prev forward, so, just do prev = prev.next
17+
* but, for both cases, we'll also move curr forward, so we put curr = curr.next in the outside.
18+
*/
19+
public ListNode removeElements(ListNode head, int val) {
20+
ListNode dummy = new ListNode(-1);
21+
dummy.next = head;
22+
ListNode curr = head;
23+
ListNode prev = dummy;
24+
while (curr != null) {
25+
if (curr.val == val) {
26+
prev.next = curr.next;
27+
} else {
28+
prev = prev.next;
29+
}
30+
curr = curr.next;
31+
}
32+
return dummy.next;
33+
}
34+
}
35+
}
Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.TreeNode;
4-
import java.util.LinkedList;
5-
import java.util.Queue;
6-
7-
public class _226 {
8-
9-
public static class Solution1 {
10-
/*
11-
* An iterative solution
12-
*/
13-
public TreeNode invertTree(TreeNode root) {
14-
if (root == null || (root.left == null && root.right == null)) {
15-
return root;
16-
}
17-
Queue<TreeNode> q = new LinkedList();
18-
q.offer(root);
19-
while (!q.isEmpty()) {
20-
TreeNode curr = q.poll();
21-
TreeNode temp = curr.left;
22-
curr.left = curr.right;
23-
curr.right = temp;
24-
if (curr.left != null) {
25-
q.offer(curr.left);
26-
}
27-
if (curr.right != null) {
28-
q.offer(curr.right);
29-
}
30-
}
31-
return root;
32-
}
33-
}
34-
35-
public static class Solution2 {
36-
/*
37-
* A recursive solution
38-
*/
39-
public TreeNode invertTree(TreeNode root) {
40-
if (root == null || (root.left == null && root.right == null)) {
41-
return root;
42-
}
43-
TreeNode temp = root.left;
44-
root.left = root.right;
45-
root.right = temp;
46-
invertTree(root.left);
47-
invertTree(root.right);
48-
return root;
49-
}
50-
}
51-
52-
public static class Solution3 {
53-
/*
54-
* A more concise version
55-
*/
56-
public TreeNode invertTree(TreeNode root) {
57-
if (root == null) {
58-
return null;
59-
}
60-
TreeNode temp = root.left;
61-
root.left = invertTree(root.right);
62-
root.right = invertTree(temp);
63-
return root;
64-
}
65-
}
66-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
public class _226 {
8+
9+
public static class Solution1 {
10+
/*
11+
* An iterative solution
12+
*/
13+
public TreeNode invertTree(TreeNode root) {
14+
if (root == null || (root.left == null && root.right == null)) {
15+
return root;
16+
}
17+
Queue<TreeNode> q = new LinkedList();
18+
q.offer(root);
19+
while (!q.isEmpty()) {
20+
TreeNode curr = q.poll();
21+
TreeNode temp = curr.left;
22+
curr.left = curr.right;
23+
curr.right = temp;
24+
if (curr.left != null) {
25+
q.offer(curr.left);
26+
}
27+
if (curr.right != null) {
28+
q.offer(curr.right);
29+
}
30+
}
31+
return root;
32+
}
33+
}
34+
35+
public static class Solution2 {
36+
/*
37+
* A recursive solution
38+
*/
39+
public TreeNode invertTree(TreeNode root) {
40+
if (root == null || (root.left == null && root.right == null)) {
41+
return root;
42+
}
43+
TreeNode temp = root.left;
44+
root.left = root.right;
45+
root.right = temp;
46+
invertTree(root.left);
47+
invertTree(root.right);
48+
return root;
49+
}
50+
}
51+
52+
public static class Solution3 {
53+
/*
54+
* A more concise version
55+
*/
56+
public TreeNode invertTree(TreeNode root) {
57+
if (root == null) {
58+
return null;
59+
}
60+
TreeNode temp = root.left;
61+
root.left = invertTree(root.right);
62+
root.right = invertTree(temp);
63+
return root;
64+
}
65+
}
66+
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import com.fishercoder.common.classes.ListNode;
4-
5-
public class _237 {
6-
7-
public static class Solution1 {
8-
public void deleteNode(ListNode node) {
9-
node.val = node.next.val;
10-
node.next = node.next.next;
11-
}
12-
}
13-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
public class _237 {
6+
7+
public static class Solution1 {
8+
public void deleteNode(ListNode node) {
9+
node.val = node.next.val;
10+
node.next = node.next.next;
11+
}
12+
}
13+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
public class _27 {
4-
5-
public static class Solution1 {
6-
public int removeElement(int[] nums, int val) {
7-
int i = 0;
8-
for (int j = 0; j < nums.length; j++) {
9-
if (nums[j] != val) {
10-
nums[i++] = nums[j];
11-
}
12-
}
13-
return i;
14-
}
15-
}
16-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
public class _27 {
4+
5+
public static class Solution1 {
6+
public int removeElement(int[] nums, int val) {
7+
int i = 0;
8+
for (int j = 0; j < nums.length; j++) {
9+
if (nums[j] != val) {
10+
nums[i++] = nums[j];
11+
}
12+
}
13+
return i;
14+
}
15+
}
16+
}
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import java.util.Arrays;
4-
import java.util.HashSet;
5-
import java.util.Set;
6-
7-
public class _345 {
8-
public static class Solution1 {
9-
public String reverseVowels(String s) {
10-
StringBuilder sb = new StringBuilder(s);
11-
Set<Character> vowels =
12-
new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
13-
// use two pointers approach would be the fastest
14-
int i = 0;
15-
int j = s.length() - 1;
16-
while (i < j) {
17-
char left = s.charAt(i);
18-
char right = s.charAt(j);
19-
while (i < j && !vowels.contains(left)) {
20-
i++;
21-
left = s.charAt(i);
22-
}
23-
while (i < j && !vowels.contains(right)) {
24-
j--;
25-
right = s.charAt(j);
26-
}
27-
char temp = left;
28-
sb.setCharAt(i, right);
29-
sb.setCharAt(j, temp);
30-
i++;
31-
j--;
32-
}
33-
return sb.toString();
34-
}
35-
}
36-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class _345 {
8+
public static class Solution1 {
9+
public String reverseVowels(String s) {
10+
StringBuilder sb = new StringBuilder(s);
11+
Set<Character> vowels =
12+
new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
13+
// use two pointers approach would be the fastest
14+
int i = 0;
15+
int j = s.length() - 1;
16+
while (i < j) {
17+
char left = s.charAt(i);
18+
char right = s.charAt(j);
19+
while (i < j && !vowels.contains(left)) {
20+
i++;
21+
left = s.charAt(i);
22+
}
23+
while (i < j && !vowels.contains(right)) {
24+
j--;
25+
right = s.charAt(j);
26+
}
27+
char temp = left;
28+
sb.setCharAt(i, right);
29+
sb.setCharAt(j, temp);
30+
i++;
31+
j--;
32+
}
33+
return sb.toString();
34+
}
35+
}
36+
}
Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
import java.util.*;
4-
import java.util.Map.Entry;
5-
6-
public class _347 {
7-
8-
public static class Solution1 {
9-
/*
10-
* Bucket sort:
11-
* Use buckets to hold numbers of the same frequency, some buckets might be empty while the rest might have more than one element.
12-
* This editorial explains it well enough: https://leetcode.com/problems/top-k-frequent-elements/editorial/ starting from 08'55".
13-
* <p>
14-
* This is the most optimal solution.
15-
* Time: O(n)
16-
* Space: O(n)
17-
*/
18-
public int[] topKFrequent(int[] nums, int k) {
19-
Map<Integer, Integer> map = new HashMap();
20-
for (int num : nums) {
21-
map.put(num, map.getOrDefault(num, 0) + 1);
22-
}
23-
// use nums.length + 1, so that we can directly use the frequency as the index for this
24-
// array
25-
// how this buckets look like is: buckets[1] holds numbers that have frequency one,
26-
// buckets[2] holds numbers that have frequency two, etc.
27-
// so, the numbers that have the highest frequencies are on the right-most side.
28-
List[] bucket = new ArrayList[nums.length + 1];
29-
for (Entry<Integer, Integer> entry : map.entrySet()) {
30-
int freq = entry.getValue();
31-
if (bucket[freq] == null) {
32-
bucket[freq] = new ArrayList<Integer>();
33-
}
34-
bucket[freq].add(entry.getKey());
35-
}
36-
int[] result = new int[k];
37-
for (int i = bucket.length - 1, l = 0; i >= 0 && l < k; i--) {
38-
if (bucket[i] != null) {
39-
for (int j = 0; j < bucket[i].size(); j++) {
40-
result[l++] = (int) bucket[i].get(j);
41-
}
42-
}
43-
}
44-
return result;
45-
}
46-
}
47-
48-
public static class Solution2 {
49-
/*
50-
* Use hashtable and heap.
51-
* Time: O(nlogn)
52-
* Space: O(n)
53-
*/
54-
public int[] topKFrequent(int[] nums, int k) {
55-
// construct the frequency map first, and then iterate through the map
56-
// and put them into the heap, this is O(n)
57-
Map<Integer, Integer> map = new HashMap();
58-
for (int num : nums) {
59-
map.put(num, map.getOrDefault(num, 0) + 1);
60-
}
61-
62-
// build heap, this is O(nlogn)
63-
Queue<Entry<Integer, Integer>> heap =
64-
new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
65-
for (Entry<Integer, Integer> entry : map.entrySet()) {
66-
heap.offer(entry);
67-
}
68-
69-
List<Integer> result = new ArrayList();
70-
while (k-- > 0) {
71-
result.add(heap.poll().getKey());
72-
}
73-
int[] arr = new int[result.size()];
74-
for (int i = 0; i < arr.length; i++) {
75-
arr[i] = result.get(i);
76-
}
77-
return arr;
78-
}
79-
}
80-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import java.util.*;
4+
import java.util.Map.Entry;
5+
6+
public class _347 {
7+
8+
public static class Solution1 {
9+
/*
10+
* Bucket sort:
11+
* Use buckets to hold numbers of the same frequency, some buckets might be empty while the rest might have more than one element.
12+
* This editorial explains it well enough: https://leetcode.com/problems/top-k-frequent-elements/editorial/ starting from 08'55".
13+
* <p>
14+
* This is the most optimal solution.
15+
* Time: O(n)
16+
* Space: O(n)
17+
*/
18+
public int[] topKFrequent(int[] nums, int k) {
19+
Map<Integer, Integer> map = new HashMap();
20+
for (int num : nums) {
21+
map.put(num, map.getOrDefault(num, 0) + 1);
22+
}
23+
// use nums.length + 1, so that we can directly use the frequency as the index for this
24+
// array
25+
// how this buckets look like is: buckets[1] holds numbers that have frequency one,
26+
// buckets[2] holds numbers that have frequency two, etc.
27+
// so, the numbers that have the highest frequencies are on the right-most side.
28+
List[] bucket = new ArrayList[nums.length + 1];
29+
for (Entry<Integer, Integer> entry : map.entrySet()) {
30+
int freq = entry.getValue();
31+
if (bucket[freq] == null) {
32+
bucket[freq] = new ArrayList<Integer>();
33+
}
34+
bucket[freq].add(entry.getKey());
35+
}
36+
int[] result = new int[k];
37+
for (int i = bucket.length - 1, l = 0; i >= 0 && l < k; i--) {
38+
if (bucket[i] != null) {
39+
for (int j = 0; j < bucket[i].size(); j++) {
40+
result[l++] = (int) bucket[i].get(j);
41+
}
42+
}
43+
}
44+
return result;
45+
}
46+
}
47+
48+
public static class Solution2 {
49+
/*
50+
* Use hashtable and heap.
51+
* Time: O(nlogn)
52+
* Space: O(n)
53+
*/
54+
public int[] topKFrequent(int[] nums, int k) {
55+
// construct the frequency map first, and then iterate through the map
56+
// and put them into the heap, this is O(n)
57+
Map<Integer, Integer> map = new HashMap();
58+
for (int num : nums) {
59+
map.put(num, map.getOrDefault(num, 0) + 1);
60+
}
61+
62+
// build heap, this is O(nlogn)
63+
Queue<Entry<Integer, Integer>> heap =
64+
new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
65+
for (Entry<Integer, Integer> entry : map.entrySet()) {
66+
heap.offer(entry);
67+
}
68+
69+
List<Integer> result = new ArrayList();
70+
while (k-- > 0) {
71+
result.add(heap.poll().getKey());
72+
}
73+
int[] arr = new int[result.size()];
74+
for (int i = 0; i < arr.length; i++) {
75+
arr[i] = result.get(i);
76+
}
77+
return arr;
78+
}
79+
}
80+
}
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
package com.fishercoder.solutions.firstthousand;
2-
3-
public class _8 {
4-
5-
public static class Solution1 {
6-
/*
7-
* four corner cases:
8-
* 1. discards all leading zeroes
9-
* 2. sign of the number
10-
* 3. overflow
11-
* 4. invalid input
12-
*/
13-
public int myAtoi(String s) {
14-
int pointer = 0;
15-
int result = 0;
16-
while (pointer < s.length() && Character.isWhitespace(s.charAt(pointer))) {
17-
pointer++;
18-
}
19-
if (pointer == s.length()) {
20-
return 0;
21-
}
22-
boolean negativeFlag = (s.charAt(pointer) == '-');
23-
if (s.charAt(pointer) == '+' || s.charAt(pointer) == '-') {
24-
pointer++;
25-
}
26-
for (; pointer < s.length(); pointer++) {
27-
if (s.charAt(pointer) > '9' || s.charAt(pointer) < '0') {
28-
break;
29-
} else {
30-
int digit = s.charAt(pointer) - '0';
31-
if (!negativeFlag && result > (Integer.MAX_VALUE - digit) / 10) {
32-
return Integer.MAX_VALUE;
33-
} else if (negativeFlag && result < (Integer.MIN_VALUE + digit) / 10) {
34-
return Integer.MIN_VALUE;
35-
}
36-
result = result * 10 + (negativeFlag ? -digit : digit);
37-
}
38-
}
39-
return result;
40-
}
41-
}
42-
}
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
public class _8 {
4+
5+
public static class Solution1 {
6+
/*
7+
* four corner cases:
8+
* 1. discards all leading zeroes
9+
* 2. sign of the number
10+
* 3. overflow
11+
* 4. invalid input
12+
*/
13+
public int myAtoi(String s) {
14+
int pointer = 0;
15+
int result = 0;
16+
while (pointer < s.length() && Character.isWhitespace(s.charAt(pointer))) {
17+
pointer++;
18+
}
19+
if (pointer == s.length()) {
20+
return 0;
21+
}
22+
boolean negativeFlag = (s.charAt(pointer) == '-');
23+
if (s.charAt(pointer) == '+' || s.charAt(pointer) == '-') {
24+
pointer++;
25+
}
26+
for (; pointer < s.length(); pointer++) {
27+
if (s.charAt(pointer) > '9' || s.charAt(pointer) < '0') {
28+
break;
29+
} else {
30+
int digit = s.charAt(pointer) - '0';
31+
if (!negativeFlag && result > (Integer.MAX_VALUE - digit) / 10) {
32+
return Integer.MAX_VALUE;
33+
} else if (negativeFlag && result < (Integer.MIN_VALUE + digit) / 10) {
34+
return Integer.MIN_VALUE;
35+
}
36+
result = result * 10 + (negativeFlag ? -digit : digit);
37+
}
38+
}
39+
return result;
40+
}
41+
}
42+
}

‎src/main/java/com/fishercoder/solutions/thirdthousand/_2022.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class _2022 {
44
public static class Solution1 {
55
public int[][] construct2DArray(int[] original, int m, int n) {
66
if (m * n != original.length) {
7-
return new int[][]{};
7+
return new int[][] {};
88
}
99
int[][] ans = new int[m][n];
1010
int k = 0;

‎src/test/java/com/fishercoder/fourththousand/_3258Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.fishercoder.fourththousand;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
35
import com.fishercoder.solutions.fourththousand._3258;
46
import org.junit.jupiter.api.BeforeEach;
57
import org.junit.jupiter.api.Test;
68

7-
import static org.junit.jupiter.api.Assertions.assertEquals;
8-
99
public class _3258Test {
1010
private _3258.Solution1 solution1;
1111

@@ -23,4 +23,4 @@ public void test1() {
2323
public void test2() {
2424
assertEquals(25, solution1.countKConstraintSubstrings("1010101", 2));
2525
}
26-
}
26+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.fishercoder.secondthousand;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
35
import com.fishercoder.solutions.secondthousand._1894;
46
import org.junit.jupiter.api.BeforeEach;
57
import org.junit.jupiter.api.Test;
68

7-
import static org.junit.jupiter.api.Assertions.assertEquals;
8-
99
public class _1894Test {
1010
private _1894.Solution1 solution1;
1111
private static int[] chalk;
@@ -17,8 +17,7 @@ public void setup() {
1717

1818
@Test
1919
public void test1() {
20-
chalk = new int[]{3, 4, 1, 2};
20+
chalk = new int[] {3, 4, 1, 2};
2121
assertEquals(1, solution1.chalkReplacer(chalk, 25));
2222
}
23-
2423
}

0 commit comments

Comments
 (0)
Please sign in to comment.