Skip to content

Commit cb0260c

Browse files
committed
其他解法
1 parent 1b15857 commit cb0260c

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.chen.algorithm.study.test238;
2+
3+
import com.alibaba.fastjson.JSON;
4+
5+
/**
6+
* @Auther: zhunn
7+
* @Date: 2020/10/16 14:30
8+
* @Description: 除自身以外数组的乘积
9+
*/
10+
public class Solution1 {
11+
12+
/**
13+
* 时间复杂度O(N),空间复杂度O(N)
14+
*
15+
* @param nums
16+
* @return
17+
*/
18+
public static int[] productExceptSelf(int[] nums) {
19+
if (nums == null || nums.length == 0) {
20+
return new int[0];
21+
}
22+
23+
int length = nums.length;
24+
25+
// left和right 分别表示左右两侧的乘积列表
26+
int[] left = new int[length];
27+
int[] right = new int[length];
28+
29+
int[] answer = new int[length];
30+
31+
// left[i] 为索引 i 左侧所有元素的乘积
32+
// 对于索引为 ‘0’ 的元素,因为左侧没有元素,所以left[0]=1
33+
left[0] = 1;
34+
for (int i = 1; i < length; i++) {
35+
left[i] = left[i - 1] * nums[i - 1];
36+
}
37+
38+
// right[i] 为索引 i 右侧所有元素的乘积
39+
// 对于索引为 ‘length-1’ 的元素,因为右侧没有元素,所以right[length - 1] = 1
40+
right[length - 1] = 1;
41+
for (int i = length - 2; i >= 0; i--) {
42+
right[i] = right[i + 1] * nums[i + 1];
43+
}
44+
45+
// 对于索引i,除nums[i]之外其余各元素的乘积就是左侧所有元素的乘积乘以右侧所有元素的乘积
46+
for (int i = 0; i < length; i++) {
47+
answer[i] = left[i] * right[i];
48+
}
49+
return answer;
50+
}
51+
52+
/**
53+
* 时间复杂度O(N),空间复杂度O(1)
54+
*
55+
* @param nums
56+
* @return
57+
*/
58+
//public static int[] productExceptSelf1(int[] nums) {
59+
// if (nums == null || nums.length == 0) {
60+
// return new int[0];
61+
// }
62+
//
63+
// int length = nums.length;
64+
// int[] answer = new int[length];
65+
//
66+
// // answer[i] 为索引 i 左侧所有元素的乘积
67+
// // 对于索引为 ‘0’ 的元素,因为左侧没有元素,answer[0]=1
68+
// answer[0] = 1;
69+
// for (int i = 1; i < length; i++) {
70+
// answer[i] = answer[i - 1] * nums[i - 1];
71+
// }
72+
//
73+
// // right 为右侧所有元素的乘积
74+
// // 刚开始右侧没有元素,所以right = 1
75+
// int right = 1;
76+
// for (int i = length - 1; i >= 0; i--) {
77+
// // 对于索引 i,左边的乘积为answer[i],右边的乘积为right
78+
// answer[i] = right * answer[i];
79+
// // right 需要包含右边所有的乘积,所以计算下一个结果时需要将当前值乘到 right 上
80+
// right = right * nums[i];
81+
// }
82+
// return answer;
83+
//}
84+
public static int[] productExceptSelfTest(int[] nums) {
85+
if (nums == null || nums.length == 0) {
86+
return new int[0];
87+
}
88+
89+
int length = nums.length;
90+
int[] answer = new int[length];
91+
92+
answer[0] = 1;
93+
for (int i = 1; i < length; i++) {
94+
answer[i] = answer[i - 1] * nums[i - 1];
95+
}
96+
97+
int right = 1;
98+
for (int i = length - 1; i >= 0; i--) {
99+
answer[i] = right * answer[i];
100+
right = right * nums[i];
101+
}
102+
return answer;
103+
}
104+
105+
public static void main(String[] args) {
106+
int[] nums = {1, 2, 3, 4, 2};
107+
System.out.println(JSON.toJSONString(productExceptSelf(nums)));
108+
}
109+
110+
}

src/main/java/com/chen/algorithm/study/test283/Solution2.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import java.util.Arrays;
66

77
/**
8-
* wrong.......wrong.......wrong.......
9-
*
8+
* 移动0
109
* @author : chen weijie
1110
* @Date: 2019-11-03 18:44
1211
*/
@@ -31,7 +30,6 @@ public void moveZeroes(int[] nums) {
3130
}
3231

3332

34-
3533
}
3634

3735
@Test
@@ -45,4 +43,27 @@ public void testCase() {
4543
}
4644

4745

46+
/**
47+
* @author: zhunn
48+
* @param nums
49+
* @return
50+
*/
51+
public int[] moveZeroes1(int[] nums) {
52+
if (nums == null || nums.length == 0) {
53+
return nums;
54+
}
55+
56+
int i = 0;
57+
for (int j = 0; j < nums.length; j++) {
58+
if (nums[j] != 0) {
59+
nums[i] = nums[j];
60+
i++;
61+
}
62+
}
63+
64+
for (int k = i; k < nums.length; k++) {
65+
nums[k] = 0;
66+
}
67+
return nums;
68+
}
4869
}

src/main/java/com/chen/algorithm/study/test581/Solution.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
*
1111
* @author : chen weijie
1212
* @Date: 2019-11-07 23:34
13+
* @Description: 最短无序连续子数组
1314
*/
1415
public class Solution {
1516

1617

1718
public int findUnsortedSubarray(int[] nums) {
19+
if(nums == null || nums.length == 0){
20+
return 0;
21+
}
1822

1923
int[] snums = nums.clone();
2024
Arrays.sort(snums);

0 commit comments

Comments
 (0)