Skip to content

Commit 95794c6

Browse files
committed
review-2
1 parent 032af70 commit 95794c6

4 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎src/main/java/com/chen/algorithm/znn/array/offer/test29/Solution.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chen.algorithm.znn.array.offer.test29;
22

3+
import org.junit.Test;
4+
35
import java.util.Arrays;
46

57
/**
@@ -18,7 +20,7 @@
1820
*/
1921
public class Solution {
2022

21-
public static int[] spiralOrder(int[][] matrix) {
23+
public int[] spiralOrder(int[][] matrix) {
2224
if (matrix.length == 0) {
2325
return new int[0];
2426
}
@@ -54,7 +56,7 @@ public static int[] spiralOrder(int[][] matrix) {
5456
return res;
5557
}
5658

57-
private static int[] spiralOrder1(int[][] matrix) {
59+
public int[] spiralOrder1(int[][] matrix) {
5860
if (matrix == null || matrix.length == 0) return new int[0];
5961

6062
int numEle = matrix.length * matrix[0].length;
@@ -87,7 +89,8 @@ private static int[] spiralOrder1(int[][] matrix) {
8789
return result;
8890
}
8991

90-
public static void main(String[] args) {
92+
@Test
93+
public void test() {
9194
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
9295
int[] res = spiralOrder1(matrix);
9396
System.out.println(Arrays.toString(res));

‎src/main/java/com/chen/algorithm/znn/array/test1/Solution.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
public class Solution {
2323

24-
public int[] twoSum1(int[] nums, int target) {
24+
public int[] twoSum(int[] nums, int target) {
2525
int n = nums.length;
2626
for (int i = 0; i < n; ++i) {
2727
for (int j = i + 1; j < n; ++j) {
@@ -34,12 +34,16 @@ public int[] twoSum1(int[] nums, int target) {
3434
}
3535

3636
public int[] twoSum2(int[] nums, int target) {
37-
Map<Integer, Integer> hashtable = new HashMap<>();
37+
if (nums == null || nums.length < 2) {
38+
return new int[0];
39+
}
40+
41+
Map<Integer, Integer> map = new HashMap<>();
3842
for (int i = 0; i < nums.length; ++i) {
39-
if (hashtable.containsKey(target - nums[i])) {
40-
return new int[]{hashtable.get(target - nums[i]), i};
43+
if (map.containsKey(target - nums[i])) {
44+
return new int[]{map.get(target - nums[i]), i};
4145
}
42-
hashtable.put(nums[i], i);
46+
map.put(nums[i], i);
4347
}
4448
return new int[0];
4549
}
@@ -48,7 +52,7 @@ public int[] twoSum2(int[] nums, int target) {
4852
public void testCase() {
4953
int[] array = {4, 5, 6, 7, 2};
5054

51-
int[] result = twoSum1(array, 9);
55+
int[] result = twoSum2(array, 9);
5256
System.out.println(JSON.toJSONString(result));
5357

5458
}

‎src/main/java/com/chen/algorithm/znn/array/test240/Solution.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chen.algorithm.znn.array.test240;
22

3+
import org.junit.Test;
4+
35
/**
46
* https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/er-fen-fa-pai-chu-fa-python-dai-ma-java-dai-ma-by-/
57
* 240. 搜索二维矩阵 II
@@ -71,4 +73,16 @@ public boolean searchMatrix(int[][] matrix, int target) {
7173
}
7274

7375

76+
@Test
77+
public void test() {
78+
int[][] matrix = {
79+
{1, 4, 7, 11, 15},
80+
{2, 5, 8, 12, 19},
81+
{3, 6, 9, 16, 22},
82+
{10, 13, 14, 17, 24},
83+
{18, 21, 23, 26, 30}
84+
};
85+
System.out.println(searchMatrix(matrix, 5));
86+
System.out.println(searchMatrix(matrix, 20));
87+
}
7488
}

‎src/main/java/com/chen/algorithm/znn/array/test56/Solution.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.ArrayList;
77
import java.util.Arrays;
8+
import java.util.Comparator;
89
import java.util.List;
910

1011
/**
@@ -68,12 +69,36 @@ public int[][] merge(int[][] intervals) {
6869
return array;
6970
}
7071

72+
public int[][] merge1(int[][] intervals) {
73+
if (intervals == null || intervals.length == 0) {
74+
return null;
75+
}
76+
77+
//Arrays.sort(intervals, new Comparator<int[]>() {
78+
// @Override
79+
// public int compare(int[] o1, int[] o2) {
80+
// return o1[0] - o2[0];
81+
// }
82+
//});
83+
Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
84+
85+
List<int[]> merged = new ArrayList<>();
86+
for (int i = 0; i < intervals.length; i++) {
87+
int L = intervals[i][0], R = intervals[i][1];
88+
if (merged.size() == 0 || merged.get(merged.size() - 1)[1] < L) {
89+
merged.add(new int[]{L, R});
90+
} else {
91+
merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], R);
92+
}
93+
}
94+
return merged.toArray(new int[merged.size()][]);
95+
}
7196

7297
@Test
7398
public void testCase() {
7499

75100
int[][] n = {{1, 3}, {2, 6}, {15, 18}, {8, 10}};
76-
System.out.println(JSONObject.toJSONString(merge(n)));
101+
System.out.println(JSONObject.toJSONString(merge1(n)));
77102
}
78103

79104
}

0 commit comments

Comments
 (0)