Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3d119f4

Browse files
Create find-original-array-from-doubled-array.go
1 parent 572be6c commit 3d119f4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//https://leetcode.com/problems/find-original-array-from-doubled-array/
2+
3+
package leetcode_solutions_golang
4+
5+
import "sort"
6+
7+
func findOriginalArray(changed []int) []int {
8+
if len(changed)%2 == 1 {
9+
return []int{}
10+
}
11+
12+
sort.Ints(changed)
13+
result := make([]int, 0)
14+
var count [100001]int
15+
for _, i := range changed {
16+
count[i]++
17+
}
18+
for i := 100000; i >= 0; i -= 2 {
19+
half := i / 2
20+
for count[i] > 0 && count[half] > 0 {
21+
count[i]--
22+
count[half]--
23+
result = append(result, half)
24+
}
25+
}
26+
for i := 0; i <= 100000; i++ {
27+
if count[i] > 0 {
28+
return []int{}
29+
}
30+
}
31+
32+
return result
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /