Skip to content

Commit 6b4cd7c

Browse files
2 parents a75e677 + 6e270a6 commit 6b4cd7c

File tree

16 files changed

+103
-189
lines changed

16 files changed

+103
-189
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ public class _1 {
1818
public static class Solution1 {
1919
public int[] twoSum(int[] nums, int target) {
2020
Map<Integer, Integer> map = new HashMap();
21-
int[] result = new int[2];
2221
for (int i = 0; i < nums.length; i++) {
2322
if (map.containsKey(target - nums[i])) {
24-
result[0] = map.get(target - nums[i]);
25-
result[1] = i;
26-
break;
23+
return new int[]{map.get(target - nums[i]), i};
2724
} else {
2825
map.put(nums[i], i);
2926
}
3027
}
31-
return result;
28+
return new int[]{-1, -1};
3229
}
3330
}
3431

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static class Solution2 {
4343
/** O(k) space */
4444
public List<Integer> getRow(int rowIndex) {
4545
List<Integer> row = new ArrayList<>();
46-
for (int i = 0; i < rowIndex + 1; i++) {
46+
for (int i = 0; i <= rowIndex; i++) {
4747
row.add(0, 1);
4848
for (int j = 1; j < row.size() - 1; j++) {
4949
row.set(j, row.get(j) + row.get(j + 1));

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public int maxProfit(int[] prices) {
3333
if (prices == null || prices.length == 0) {
3434
return 0;
3535
}
36-
int buy = prices[0];
36+
int minBuy = prices[0];
3737
int maxProfit = 0;
3838
for (int i = 1; i < prices.length; i++) {
39-
if (prices[i] < buy) {
40-
buy = prices[i];
39+
if (prices[i] < minBuy) {
40+
minBuy = prices[i];
4141
} else {
42-
maxProfit = Math.max(maxProfit, prices[i] - buy);
42+
maxProfit = Math.max(maxProfit, prices[i] - minBuy);
4343
}
4444
}
4545
return maxProfit;

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,10 @@ public void push(int x) {
4848
}
4949

5050
public void pop() {
51-
if (min == stack.peek()) {
52-
stack.pop();
51+
/**if the value on the top of the stack happens to be the current minimum, we'll pop twice and change
52+
* the current min value to be the last min value */
53+
if (min == stack.pop()) {
5354
min = stack.pop();
54-
} else {
55-
stack.pop();
56-
}
57-
if (stack.isEmpty()) {
58-
min = Integer.MAX_VALUE;
5955
}
6056
}
6157

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ public int[] twoSum(int[] numbers, int target) {
3232
} else if (sum < target) {
3333
left++;
3434
} else {
35-
int[] res = new int[2];
36-
res[0] = left + 1;
37-
res[1] = right + 1;
38-
return res;
35+
return new int[]{left + 1, right + 1};
3936
}
4037
}
41-
return new int[] {-1, -1};
38+
return new int[]{-1, -1};
4239
}
4340
}
4441
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public static class Solution2 {
4141
* still, a null newHead proves to be very helpful.
4242
*/
4343
public ListNode reverseList(ListNode head) {
44-
ListNode newHead = null;
45-
return reverse(head, newHead);
44+
return reverse(head, null);
4645
}
4746

4847
ListNode reverse(ListNode head, ListNode newHead) {

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

+1-18
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,4 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3939
}
4040
}
4141

42-
public static class Solution2 {
43-
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
44-
if (p == root || q == root || p == q) {
45-
return root;
46-
}
47-
if (p.val < root.val && q.val > root.val) {
48-
return root;
49-
}
50-
if (p.val < root.val && q.val < root.val) {
51-
return lowestCommonAncestor(root.left, p, q);
52-
}
53-
if (p.val > root.val && q.val > root.val) {
54-
return lowestCommonAncestor(root.right, p, q);
55-
}
56-
return root;
57-
}
58-
}
59-
}
42+
}

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

+23-43
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import java.util.Stack;
66

77
/**
8-
* You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
9-
10-
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
8+
* 496. Next Greater Element I
9+
*
10+
* You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2.
11+
* Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
12+
* The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
1113
1214
Example 1:
1315
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
@@ -16,63 +18,41 @@
1618
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
1719
For number 1 in the first array, the next greater number for it in the second array is 3.
1820
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
21+
1922
Example 2:
2023
Input: nums1 = [2,4], nums2 = [1,2,3,4].
2124
Output: [3,-1]
2225
Explanation:
2326
For number 2 in the first array, the next greater number for it in the second array is 3.
2427
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
28+
2529
Note:
2630
All elements in nums1 and nums2 are unique.
2731
The length of both nums1 and nums2 would not exceed 1000.
2832
*/
2933
public class _496 {
3034

31-
public int[] nextGreaterElement_clever_way(int[] findNums, int[] nums) {
32-
Stack<Integer> stack = new Stack();
33-
Map<Integer, Integer> map = new HashMap();
34-
for (int i = 0; i < nums.length; i++) {
35-
while (!stack.isEmpty() && nums[i] > stack.peek()) {
36-
map.put(stack.pop(), nums[i]);
35+
public static class Solution1 {
36+
public int[] nextGreaterElement(int[] findNums, int[] nums) {
37+
Stack<Integer> stack = new Stack();
38+
Map<Integer, Integer> map = new HashMap();
39+
for (int i = 0; i < nums.length; i++) {
40+
while (!stack.isEmpty() && nums[i] > stack.peek()) {
41+
map.put(stack.pop(), nums[i]);
42+
}
43+
stack.push(nums[i]);
3744
}
38-
stack.push(nums[i]);
39-
}
40-
41-
while (!stack.isEmpty()) {
42-
map.put(stack.pop(), -1);
43-
}
4445

45-
int[] result = new int[findNums.length];
46-
for (int i = 0; i < findNums.length; i++) {
47-
result[i] = map.get(findNums[i]);
48-
}
49-
return result;
50-
}
51-
52-
53-
public int[] nextGreaterElement_naive_way(int[] findNums, int[] nums) {
54-
int[] result = new int[findNums.length];
55-
for (int i = 0; i < findNums.length; i++) {
56-
boolean found = false;
57-
boolean foundNext = false;
58-
for (int j = 0; j < nums.length; j++) {
59-
if (findNums[i] != nums[j] && !found) {
60-
continue;
61-
} else if (!found) {
62-
found = true;
63-
} else {
64-
if (nums[j] > findNums[i]) {
65-
result[i] = nums[j];
66-
foundNext = true;
67-
break;
68-
}
69-
}
46+
while (!stack.isEmpty()) {
47+
map.put(stack.pop(), -1);
7048
}
71-
if (!foundNext) {
72-
result[i] = -1;
49+
50+
int[] result = new int[findNums.length];
51+
for (int i = 0; i < findNums.length; i++) {
52+
result[i] = map.get(findNums[i]);
7353
}
54+
return result;
7455
}
75-
return result;
7656
}
7757

7858
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
*/
2424
public class _498 {
2525

26-
/**Reference: https://discuss.leetcode.com/topic/77865/concise-java-solution/2
26+
public static class Solutoin1 {
27+
/**
28+
* Reference: https://discuss.leetcode.com/topic/77865/concise-java-solution/2
2729
* Just keep walking the matrix, when hitting the four borders (top, bottom, left or right),
28-
* just directions and keep walking.*/
30+
* just directions and keep walking.
31+
*/
2932
public int[] findDiagonalOrder(int[][] matrix) {
3033

3134
if (matrix == null || matrix.length == 0) {
@@ -62,7 +65,7 @@ public int[] findDiagonalOrder(int[][] matrix) {
6265
}
6366
}
6467
return result;
65-
6668
}
69+
}
6770

6871
}

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

-22
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,6 @@
3232
public class _617 {
3333

3434
public static class Solution1 {
35-
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
36-
if (t1 == null) {
37-
return t2;
38-
}
39-
if (t2 == null) {
40-
return t1;
41-
}
42-
TreeNode mergedNode = null;
43-
if (t1 != null && t2 != null) {
44-
mergedNode = new TreeNode(t1.val + t2.val);
45-
} else if (t1 != null) {
46-
mergedNode = t1;
47-
} else if (t2 != null) {
48-
mergedNode = t2;
49-
}
50-
mergedNode.left = mergeTrees(t1.left, t2.left);
51-
mergedNode.right = mergeTrees(t1.right, t2.right);
52-
return mergedNode;
53-
}
54-
}
55-
56-
public static class Solution2 {
5735
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
5836
if (t1 == null) {
5937
return t2;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class Solution1 {
2727
public int minCostClimbingStairs(int[] cost) {
2828
int[] dp = new int[cost.length];
2929
dp[0] = cost[0];
30-
dp[1] = Math.min(cost[1], cost[0] + cost[1]);
30+
dp[1] = cost[1];
3131
for (int i = 2; i < cost.length; i++) {
3232
dp[i] = Math.min(dp[i - 1] + cost[i], dp[i - 2] + cost[i]);
3333
}

src/test/java/com/fishercoder/_167Test.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
import static org.junit.Assert.assertArrayEquals;
88

99
public class _167Test {
10-
private static _167.Solution1 solution1;
11-
private static int[] numbers;
12-
private static int[] expected;
10+
private static _167.Solution1 solution1;
11+
private static int[] numbers;
12+
private static int[] expected;
1313

14-
@BeforeClass
15-
public static void setup() {
16-
solution1 = new _167.Solution1();
17-
}
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _167.Solution1();
17+
}
1818

19-
@Test
20-
public void test1() {
21-
numbers = new int[] {-3, 3, 4, 90};
22-
expected = new int[] {1, 2};
23-
assertArrayEquals(expected, solution1.twoSum(numbers, 0));
24-
}
19+
@Test
20+
public void test1() {
21+
numbers = new int[]{-3, 3, 4, 90};
22+
expected = new int[]{1, 2};
23+
assertArrayEquals(expected, solution1.twoSum(numbers, 0));
24+
}
2525
}

src/test/java/com/fishercoder/_235Test.java

+39-41
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,50 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions._235;
6+
67
import java.util.Arrays;
8+
79
import org.junit.BeforeClass;
810
import org.junit.Test;
911

1012
import static org.junit.Assert.assertEquals;
1113

1214
public class _235Test {
13-
private static _235.Solution1 solution1;
14-
private static _235.Solution2 solution2;
15-
private static TreeNode root;
16-
private static TreeNode p;
17-
private static TreeNode q;
18-
19-
@BeforeClass
20-
public static void setup() {
21-
solution1 = new _235.Solution1();
22-
solution2 = new _235.Solution2();
23-
}
24-
25-
@Test
26-
public void test1() {
27-
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
28-
TreeUtils.printBinaryTree(root);
29-
30-
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
31-
TreeUtils.printBinaryTree(p);
32-
33-
q = TreeUtils.constructBinaryTree(Arrays.asList(8, 7, 9));
34-
TreeUtils.printBinaryTree(q);
35-
36-
assertEquals(root, solution1.lowestCommonAncestor(root, p, q));
37-
assertEquals(root, solution2.lowestCommonAncestor(root, p, q));
38-
}
39-
40-
@Test
41-
public void test2() {
42-
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
43-
TreeUtils.printBinaryTree(root);
44-
45-
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
46-
TreeUtils.printBinaryTree(p);
47-
48-
q = TreeUtils.constructBinaryTree(Arrays.asList(4));
49-
TreeUtils.printBinaryTree(q);
50-
51-
assertEquals(p, solution1.lowestCommonAncestor(root, p, q));
52-
assertEquals(p, solution2.lowestCommonAncestor(root, p, q));
53-
}
15+
private static _235.Solution1 solution1;
16+
private static TreeNode root;
17+
private static TreeNode p;
18+
private static TreeNode q;
19+
20+
@BeforeClass
21+
public static void setup() {
22+
solution1 = new _235.Solution1();
23+
}
24+
25+
@Test
26+
public void test1() {
27+
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
28+
TreeUtils.printBinaryTree(root);
29+
30+
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
31+
TreeUtils.printBinaryTree(p);
32+
33+
q = TreeUtils.constructBinaryTree(Arrays.asList(8, 7, 9));
34+
TreeUtils.printBinaryTree(q);
35+
36+
assertEquals(root, solution1.lowestCommonAncestor(root, p, q));
37+
}
38+
39+
@Test
40+
public void test2() {
41+
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
42+
TreeUtils.printBinaryTree(root);
43+
44+
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
45+
TreeUtils.printBinaryTree(p);
46+
47+
q = TreeUtils.constructBinaryTree(Arrays.asList(4));
48+
TreeUtils.printBinaryTree(q);
49+
50+
assertEquals(p, solution1.lowestCommonAncestor(root, p, q));
51+
}
5452
}

0 commit comments

Comments
 (0)