Skip to content

Commit 3cb8e16

Browse files
committed
product of array except self
1 parent 1585876 commit 3cb8e16

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed

src/main/java/joshua/leetcode/array/DualSum.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package joshua.leetcode.array;
22

3+
import joshua.leetcode.solutiontag.TwoPointers;
4+
35
import java.util.Arrays;
46
import java.util.HashMap;
57

@@ -64,6 +66,7 @@ public int[] twoSum(int[] numbers, int target) {
6466
* @author joy
6567
*
6668
*/
69+
@TwoPointers
6770
static class Solution2 extends DualSum{
6871

6972
@Override
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package joshua.leetcode.array;
2+
3+
/**
4+
* 238. Product of Array Except Self<br/>
5+
* <p/>
6+
* <a href = "https://leetcode.com/problems/product-of-array-except-self/">leetcode link</a>
7+
* <p/>
8+
* Created by joshu on 2016/5/27.
9+
*/
10+
public abstract class ProductOfArrayExceptSelf {
11+
12+
/**
13+
* Given an array of n integers where n > 1, nums,
14+
* return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
15+
* <p/>
16+
* Solve it without division and in O(n).
17+
* <p/>
18+
* For example, given [1,2,3,4], return [24,12,8,6].
19+
* <p/>
20+
* Follow up:
21+
* Could you solve it with constant space complexity?
22+
* (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
23+
*
24+
* @param nums
25+
* @return
26+
*/
27+
public abstract int[] productExceptSelf(int[] nums);
28+
29+
/**
30+
* o(n)的算法,但是需要n的额外空间。
31+
* 维护两个数组,保存从左到右和从右到左的元素的乘积,即[0,..,k]和[k,..,nums.length-1]的乘积。
32+
* 最后将两个数组上相同位置上的值相乘即可。
33+
*/
34+
public static class Solution1 extends ProductOfArrayExceptSelf {
35+
36+
@Override
37+
public int[] productExceptSelf(int[] nums) {
38+
int[] fromLeft = new int[nums.length];
39+
int[] fromRight = new int[nums.length];
40+
41+
for(int i = 0 ;i < nums.length; i++) {
42+
if(i == 0) {
43+
fromLeft[i] = 1;
44+
} else {
45+
fromLeft[i] = fromLeft[i-1] * nums[i-1];
46+
}
47+
}
48+
for(int i = nums.length -1 ;i > -1; i--) {
49+
if(i == nums.length-1) {
50+
fromRight[i] = 1;
51+
} else {
52+
fromRight[i] = fromRight[i+1] * nums[i+1];
53+
}
54+
}
55+
// multiple the two array calculated above
56+
for(int i = 0; i < nums.length; i++) {
57+
fromLeft[i] *= fromRight[i];
58+
}
59+
return fromLeft;
60+
}
61+
}
62+
}

src/main/java/joshua/leetcode/array/FindCelebrity.java renamed to src/main/java/joshua/leetcode/array/twopointers/FindCelebrity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package joshua.leetcode.array;
1+
package joshua.leetcode.array.twopointers;
22

33
import joshua.leetcode.solutiontag.TwoPointers;
44

src/main/java/joshua/leetcode/array/twopointers/TrappingRainWater.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static class Solution1 extends TrappingRainWater {
3737
* 因此可以继续移动低的一侧的指针,同时判断移动的过程中能够trap的水量(小方块),直到到达最低点。
3838
* 这时候再次判断left pointer和right pointer的大小。
3939
* 直到两个指针相遇。
40+
* <a href ="https://leetcode.com/discuss/16171/sharing-my-simple-c-code-o-n-time-o-1-space">leetcode link</a>
4041
*/
4142
@Override
4243
public int trap(int[] height) {
@@ -108,4 +109,42 @@ public int trap(int[] height) {
108109
return water;
109110
}
110111
}
112+
113+
/**
114+
* 一个更直观的two pointer分解之后的解法
115+
*
116+
* <a href="https://leetcode.com/discuss/104352/java-2ms-o-n-time-o-1-space">leetcode link</a>
117+
*/
118+
@TwoPointers
119+
public static class Solution3 extends TrappingRainWater {
120+
121+
@Override
122+
public int trap(int[] height) {
123+
int water = 0;
124+
if (height.length < 3) return water;
125+
126+
int maxHeight = 0;
127+
int maxHeightPos = 0;
128+
for (int i = 0; i < height.length; i++) {
129+
if (height[i] > maxHeight) {
130+
maxHeight = height[i];
131+
maxHeightPos = i;
132+
}
133+
}
134+
135+
int prevHeight = height[0];
136+
for (int i = 1; i < maxHeightPos; i++) {
137+
if (height[i] < prevHeight) water += prevHeight - height[i];
138+
else prevHeight = height[i];
139+
}
140+
141+
prevHeight = height[height.length - 1];
142+
for (int i = height.length - 1; i > maxHeightPos; i--) {
143+
if (height[i] < prevHeight) water += prevHeight - height[i];
144+
else prevHeight = height[i];
145+
}
146+
147+
return water;
148+
}
149+
}
111150
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package joshua.leetcode.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
/**
9+
* Created by joshu on 2016/5/27.
10+
*/
11+
public class ProductOfArrayExceptSelfTest {
12+
13+
ProductOfArrayExceptSelf solution;
14+
15+
@Before
16+
public void setUp() {
17+
solution = new ProductOfArrayExceptSelf.Solution1();
18+
}
19+
20+
@Test
21+
public void testSolution() {
22+
int[] input = new int[]{1,2,3,4};
23+
int[] output = solution.productExceptSelf(input);
24+
assertArrayEquals(new int[]{24,12,8,6}, output);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)