Skip to content

Commit 5542b97

Browse files
committed
add 238 solution
1 parent 2ee0e04 commit 5542b97

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@
375375
|0235|Lowest Common Ancestor of a Binary Search Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0235.Lowest-Common-Ancestor-of-a-Binary-Search-Tree)|60.1%|Medium||
376376
|0236|Lowest Common Ancestor of a Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0236.Lowest-Common-Ancestor-of-a-Binary-Tree)|57.9%|Medium||
377377
|0237|Delete Node in a Linked List|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0237.Delete-Node-in-a-Linked-List)|75.1%|Medium||
378-
|0238|Product of Array Except Self||64.7%|Medium||
378+
|0238|Product of Array Except Self|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0238.Product-of-Array-Except-Self)|64.7%|Medium||
379379
|0239|Sliding Window Maximum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0239.Sliding-Window-Maximum)|46.6%|Hard||
380380
|0240|Search a 2D Matrix II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0240.Search-a-2D-Matrix-II)|50.5%|Medium||
381381
|0241|Different Ways to Add Parentheses||63.1%|Medium||
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
// 解法一 前缀积 * 后缀积。不使用除法,时间复杂度 O(n),除输出数组外空间复杂度 O(1)。
4+
func productExceptSelf(nums []int) []int {
5+
n := len(nums)
6+
res := make([]int, n)
7+
// res[i] 先存 nums[i] 左边所有元素的乘积
8+
res[0] = 1
9+
for i := 1; i < n; i++ {
10+
res[i] = res[i-1] * nums[i-1]
11+
}
12+
// 再从右往左乘上 nums[i] 右边所有元素的乘积,right 累计后缀积
13+
right := 1
14+
for i := n - 1; i >= 0; i-- {
15+
res[i] *= right
16+
right *= nums[i]
17+
}
18+
return res
19+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question238 struct {
9+
para238
10+
ans238
11+
}
12+
13+
// para is the parameter
14+
// one represents the first parameter
15+
type para238 struct {
16+
one []int
17+
}
18+
19+
// ans is the answer
20+
// one represents the first answer
21+
type ans238 struct {
22+
one []int
23+
}
24+
25+
func Test_Problem238(t *testing.T) {
26+
27+
qs := []question238{
28+
29+
{
30+
para238{[]int{1, 2, 3, 4}},
31+
ans238{[]int{24, 12, 8, 6}},
32+
},
33+
34+
{
35+
para238{[]int{-1, 1, 0, -3, 3}},
36+
ans238{[]int{0, 0, 9, 0, 0}},
37+
},
38+
39+
{
40+
para238{[]int{2, 3}},
41+
ans238{[]int{3, 2}},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 238------------------------\n")
46+
47+
for _, q := range qs {
48+
a, p := q.ans238, q.para238
49+
got := productExceptSelf(p.one)
50+
fmt.Printf("【input】:%v 【output】:%v\n", p, got)
51+
if !equal238(got, a.one) {
52+
t.Fatalf("input %v: expected %v, got %v", p.one, a.one, got)
53+
}
54+
}
55+
fmt.Printf("\n\n\n")
56+
}
57+
58+
func equal238(a, b []int) bool {
59+
if len(a) != len(b) {
60+
return false
61+
}
62+
for i := range a {
63+
if a[i] != b[i] {
64+
return false
65+
}
66+
}
67+
return true
68+
}

0 commit comments

Comments
 (0)