Skip to content

Commit 33c7775

Browse files
committed
feat: 0238.Product-of-Array-Except-Self
1 parent 64744cf commit 33c7775

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: 238. Product of Array Except Self
3+
subtitle: "https://leetcode.com/problems/product-of-array-except-self/description/"
4+
date: 2024-02-19T18:01:00+08:00
5+
lastmod: 2024-02-19T18:01:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0238.Product-of-Array-Except-Self"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Product of Array Except Self]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [238. Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
67+
68+
## 解題思路
69+
70+
## Big O
71+
時間複雜 : ``
72+
空間複雜 : ``
73+
74+
## 來源
75+
* https://leetcode.com/problems/product-of-array-except-self/description/
76+
* https://leetcode.cn/problems/product-of-array-except-self/description/
77+
78+
## 解答
79+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0238.Product-of-Array-Except-Self/main.go
80+
81+
```go
82+
83+
```
84+
85+
## Benchmark
86+
87+
```sh
88+
89+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package productofarrayexceptself
2+
3+
// 時間複雜 O(), 空間複雜 O()
4+
func productExceptSelf(nums []int) []int {
5+
result, left, right := make([]int, len(nums)), make([]int, len(nums)), make([]int, len(nums))
6+
7+
// left 為左側所有的成績
8+
// 索引為'0' 的元素, 因為左側沒有元素,所以left[0]=1
9+
left[0] = 1
10+
for i := 1; i < len(nums); i++ {
11+
left[i] = left[i-1] * nums[i-1]
12+
}
13+
14+
right[len(nums)-1] = 1
15+
for i := len(nums) - 2; i >= 0; i-- {
16+
right[i] = right[i+1] * nums[i+1]
17+
}
18+
19+
for i := 0; i < len(nums); i++ {
20+
result[i] = left[i] * right[i]
21+
}
22+
return result
23+
}
24+
25+
func productExceptSelf2(nums []int) []int {
26+
result := make([]int, len(nums))
27+
28+
result[0] = 1
29+
for i := 1; i < len(nums); i++ {
30+
result[i] = result[i-1] * nums[i-1]
31+
}
32+
33+
rightProduct := 1
34+
for i := len(nums) - 1; i >= 0; i-- {
35+
result[i] = result[i] * rightProduct
36+
rightProduct = rightProduct * nums[i]
37+
}
38+
39+
return result
40+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package productofarrayexceptself
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
var tests = []struct {
9+
arg1 []int
10+
want []int
11+
}{
12+
{
13+
[]int{1, 2, 3, 4},
14+
[]int{24, 12, 8, 6},
15+
},
16+
}
17+
18+
func TestProductExceptSelf(t *testing.T) {
19+
for _, tt := range tests {
20+
if got := productExceptSelf(tt.arg1); !reflect.DeepEqual(got, tt.want) {
21+
// if got := productExceptSelf(tt.arg1); got != tt.want {
22+
t.Errorf("got = %v, want = %v", got, tt.want)
23+
}
24+
}
25+
}
26+
27+
func BenchmarkProductExceptSelf(b *testing.B) {
28+
b.ResetTimer()
29+
for i := 0; i < b.N; i++ {
30+
productExceptSelf(tests[0].arg1)
31+
}
32+
}
33+
34+
func TestProductExceptSelf2(t *testing.T) {
35+
for _, tt := range tests {
36+
if got := productExceptSelf2(tt.arg1); !reflect.DeepEqual(got, tt.want) {
37+
// if got := productExceptSelf2(tt.arg1); got != tt.want {
38+
t.Errorf("got = %v, want = %v", got, tt.want)
39+
}
40+
}
41+
}
42+
43+
func BenchmarkProductExceptSelf2(b *testing.B) {
44+
b.ResetTimer()
45+
for i := 0; i < b.N; i++ {
46+
productExceptSelf2(tests[0].arg1)
47+
}
48+
}
49+
50+
/*
51+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0238.Product-of-Array-Except-Self -bench=.
52+
goos: darwin
53+
goarch: amd64
54+
pkg: LeetcodeGolang/Leetcode/0238.Product-of-Array-Except-Self
55+
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
56+
BenchmarkProductExceptSelf-8 12772174 158.4 ns/op 96 B/op 3 allocs/op
57+
BenchmarkProductExceptSelf2-8 32292304 63.74 ns/op 32 B/op 1 allocs/op
58+
PASS
59+
ok LeetcodeGolang/Leetcode/0238.Product-of-Array-Except-Self 4.228s
60+
*/

0 commit comments

Comments
 (0)