Skip to content

Commit 22831eb

Browse files
committed
review-3
1 parent 95794c6 commit 22831eb

4 files changed

Lines changed: 84 additions & 34 deletions

File tree

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

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,69 @@
1919
*
2020
* @author: zhunn
2121
* @Date: 2020-10-04 00:06
22-
* @@Description: zhunn 找到所有数组中消失的数字
22+
* @@Description: zhunn 找到所有数组中消失的数字:迭代并赋值为负数
2323
*/
2424
public class Solution {
2525

2626

2727
public List<Integer> findDisappearedNumbers(int[] nums) {
28-
29-
int temp = 0;
30-
int nextIndex = 0;
28+
if (nums == null || nums.length == 0) {
29+
return null;
30+
}
3131

3232
for (int i = 0; i < nums.length; i++) {
33+
if (nums[i] < 0) {
34+
continue;
35+
}
3336

34-
if (nums[i] > 0) {
35-
temp = nums[i];
36-
while (temp > 0) {
37-
nums[i] = 0;
38-
nextIndex = nums[temp - 1];
39-
nums[temp - 1] = -1;
40-
temp = nextIndex;
41-
}
37+
int temp = nums[i];
38+
while (temp > 0) {
39+
int nextIndex = nums[temp - 1];
40+
nums[temp - 1] = -1;
41+
temp = nextIndex;
4242
}
4343

4444
}
4545

46-
4746
List<Integer> result = new ArrayList<>();
4847

4948
for (int i = 0; i < nums.length; i++) {
50-
if (nums[i] == 0) {
49+
if (nums[i] > 0) {
5150
result.add(i + 1);
5251
}
5352
}
5453

5554
return result;
5655
}
5756

57+
/**
58+
* 官方解法
59+
*
60+
* @param nums
61+
* @return
62+
*/
63+
public List<Integer> findDisappearedNumbers1(int[] nums) {
64+
if (nums == null || nums.length == 0) {
65+
return null;
66+
}
67+
68+
for (int i = 0; i < nums.length; i++) {
69+
int newIndex = Math.abs(nums[i]) - 1;
70+
if (nums[newIndex] > 0) {
71+
nums[newIndex] *= -1;
72+
}
73+
}
74+
75+
List<Integer> result = new ArrayList<>();
76+
for (int i = 1; i <= nums.length; i++) {
77+
if (nums[i - 1] > 0) {
78+
result.add(i);
79+
}
80+
}
81+
82+
return result;
83+
}
84+
5885

5986
@Test
6087
public void testCase() {
@@ -66,5 +93,4 @@ public void testCase() {
6693

6794
}
6895

69-
7096
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author: zhunn
1818
* @Date: 2020-10-07 23:34
19-
* @Description: 最短无序连续子数组
19+
* @Description: 最短无序连续子数组:排序对比原数组
2020
*/
2121
public class Solution {
2222

@@ -29,25 +29,41 @@ public int findUnsortedSubarray(int[] nums) {
2929
int[] snums = nums.clone();
3030
Arrays.sort(snums);
3131
int start = snums.length, end = 0;
32+
33+
for (int i = 0; i < snums.length; i++) {
34+
if (snums[i] == nums[i]) {
35+
continue;
36+
}
37+
start = Math.min(start, i);
38+
end = Math.max(end, i);
39+
}
40+
return (end - start >= 0 ? end - start + 1 : 0);
41+
}
42+
43+
public int findUnsortedSubarray2(int[] nums) {
44+
if (nums == null || nums.length == 0) {
45+
return 0;
46+
}
47+
48+
int[] snums = nums.clone();
49+
Arrays.sort(snums);
50+
int start = snums.length, end = 0;
51+
3252
for (int i = 0; i < snums.length; i++) {
3353
if (snums[i] != nums[i]) {
3454
start = Math.min(start, i);
3555
end = Math.max(end, i);
3656
}
3757
}
38-
return (end - start >= 0 ? end - start + 1 : 0);
58+
return (end - start) < 0 ? 0 : end - start + 1;
3959
}
4060

41-
4261
@Test
4362
public void testCase() {
44-
45-
4663
int[] n = {2, 6, 4, 8, 10, 9, 15};
4764

4865
System.out.println(findUnsortedSubarray(n));
49-
50-
66+
System.out.println(findUnsortedSubarray2(n));
5167
}
5268

5369

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
public class Solution {
2525

2626
public int findLengthOfLCIS(int[] nums) {
27-
int res = 0, anchor = 0;
27+
if (nums == null || nums.length == 0) {
28+
return 0;
29+
}
30+
31+
int res = 0, start = 0;
32+
2833
for (int i = 0; i < nums.length; i++) {
2934
if (i > 0 && nums[i - 1] >= nums[i]) {
30-
anchor = i;
35+
start = i;
3136
}
32-
res = Math.max(res, i - anchor + 1);
37+
res = Math.max(res, i - start + 1);
3338
}
3439
return res;
3540
}

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

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

3+
import org.junit.Test;
4+
35
/**
46
* 704. 二分查找
57
* 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
@@ -21,7 +23,7 @@ public class Solution {
2123
/**
2224
* 适用于升序数组,可做相应调整适用降序数组
2325
*/
24-
public static int bsearch(int[] array, int target) {
26+
public int bsearch(int[] array, int target) {
2527
if (array == null || array.length == 0
2628
/*|| target < array[0] || target > array[array.length - 1]*/) {
2729
return -1;
@@ -51,7 +53,7 @@ public static int bsearch(int[] array, int target) {
5153
* 查找第一个与target相等的元素
5254
* 当key=array[mid]时, 往左边一个一个逼近,right = mid -1; 返回left
5355
*/
54-
public static int bsearch1(int[] array, int target) {
56+
public int bsearch1(int[] array, int target) {
5557
if (array == null || array.length == 0
5658
/*|| target < array[0] || target > array[array.length - 1]*/) {
5759
return -1;
@@ -80,7 +82,7 @@ public static int bsearch1(int[] array, int target) {
8082
*
8183
* @return
8284
*/
83-
public static int bsearch2(int[] array, int target) {
85+
public int bsearch2(int[] array, int target) {
8486
if (array == null || array.length == 0 /*|| target < array[0] || target > array[array.length - 1]*/) {
8587
return -1;
8688
}
@@ -123,7 +125,7 @@ public static int bsearch2(int[] array, int target) {
123125
/**
124126
* 查找第一个等于或者大于key的元素
125127
*/
126-
public static int bsearch3(int[] array, int target) {
128+
public int bsearch3(int[] array, int target) {
127129
if (array == null || array.length == 0) {
128130
return -1;
129131
}
@@ -143,7 +145,7 @@ public static int bsearch3(int[] array, int target) {
143145
/**
144146
* 查找第一个大于key的元素
145147
*/
146-
public static int bsearch4(int[] array, int target) {
148+
public int bsearch4(int[] array, int target) {
147149
if (array == null || array.length == 0) {
148150
return -1;
149151
}
@@ -163,7 +165,7 @@ public static int bsearch4(int[] array, int target) {
163165
/**
164166
* 查找最后一个等于或者小于key的元素
165167
*/
166-
public static int bsearch5(int[] array, int target) {
168+
public int bsearch5(int[] array, int target) {
167169
if (array == null || array.length == 0) {
168170
return -1;
169171
}
@@ -183,7 +185,7 @@ public static int bsearch5(int[] array, int target) {
183185
/**
184186
* 查找最后一个小于key的元素
185187
*/
186-
public static int bsearch6(int[] array, int target) {
188+
public int bsearch6(int[] array, int target) {
187189
if (array == null || array.length == 0) {
188190
return -1;
189191
}
@@ -200,9 +202,10 @@ public static int bsearch6(int[] array, int target) {
200202
return right;
201203
}
202204

203-
public static void main(String[] args) {
205+
@Test
206+
public void test() {
204207
int[] array = {1, 1, 3, 6, 6, 6, 7, 9, 17, 17};
205-
int index = bsearch6(array, 0);
208+
int index = bsearch6(array, 6);
206209
System.out.println(index);
207210
}
208211
}

0 commit comments

Comments
 (0)