Skip to content

Commit 1ae9ff6

Browse files
[LEET-238] add 238
1 parent e625911 commit 1ae9ff6

File tree

4 files changed

+72
-44
lines changed

4 files changed

+72
-44
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
|243|[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ShortestWordDistance.java) | O(n) | O(1) |
167167
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Searcha2DMatrixII.java)| O(log(m*n))|O(1) | Medium| Binary Search
168168
|239|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SlidingWindowMaximum.java)| O(nlogn)|O(k) | Hard| Heap
169-
|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ProductOfArrayExceptItself.java)| O(?)|O(?) | Medium|
169+
|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ProductofArrayExceptSelf.java)| O(n)|O(1) | Medium| Array
170170
|237|[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DeleteNodeInALinkedList.java)| O(1)|O(1) | Easy| LinkedList
171171
|235|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LowestCommonAncestorOfABinaryTree.java)| O(h)|O(1) | Medium| DFS
172172
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LowestCommonAncestorOfABinarySearchTree.java)| O(h)|O(1) | Easy| DFS

leetcode-algorithms/src/main/java/com/stevesun/solutions/ProductOfArrayExceptItself.java

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
5+
6+
Solve it without division and in O(n).
7+
8+
For example, given [1,2,3,4], return [24,12,8,6].
9+
10+
Follow up:
11+
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
12+
*/
13+
public class ProductofArrayExceptSelf {
14+
15+
public int[] productExceptSelf(int[] nums) {
16+
int n = nums.length;
17+
int[] result = new int[n];
18+
result[0] = 1;
19+
for (int i = 1; i < n; i++) {
20+
result[i] = result[i-1]*nums[i-1];
21+
}
22+
int right = 1;
23+
for (int i = n-1; i >= 0; i--) {
24+
result[i] *= right;
25+
right *= nums[i];
26+
}
27+
return result;
28+
}
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.ProductofArrayExceptSelf;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
10+
public class ProductofArrayExceptSelfTest {
11+
private static ProductofArrayExceptSelf test;
12+
private static int[] expected;
13+
private static int[] actual;
14+
private static int[] nums;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
test = new ProductofArrayExceptSelf();
19+
}
20+
21+
@Before
22+
public void setupForEachTest(){
23+
expected = new int[]{};
24+
actual = new int[]{};
25+
}
26+
27+
@Test
28+
public void test1(){
29+
nums = new int[]{0, 0};
30+
expected = new int[]{0, 0};
31+
actual = test.productExceptSelf(nums);
32+
assertArrayEquals(expected, actual);
33+
}
34+
35+
@Test
36+
public void test2(){
37+
nums = new int[]{1, 0};
38+
expected = new int[]{0, 1};
39+
actual = test.productExceptSelf(nums);
40+
assertArrayEquals(expected, actual);
41+
}
42+
}

0 commit comments

Comments
 (0)