-
Notifications
You must be signed in to change notification settings - Fork 0
/
question7.go
75 lines (61 loc) · 1.37 KB
/
question7.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package chapter02
import (
"crypto/rand"
"math/big"
"github.com/syhily/code-interviews/common"
)
func threeSum(nums ...int) [][]int {
quickSortArray(nums)
var res [][]int
for i := 0; i < len(nums)-2 && nums[i] <= 0; i++ {
r := innerThreeSum(nums, i)
if len(r) == 3 {
res = append(res, r)
}
}
return res
}
func innerThreeSum(nums []int, index int) []int {
target, s, e := -nums[index], index+1, len(nums)-1
for s < e {
sum := nums[s] + nums[e]
if sum < target {
s++
} else if sum > target {
e--
} else {
return []int{nums[index], nums[s], nums[e]}
}
}
return nil
}
func quickSortArray(nums []int) {
stack := common.NewStack[int]()
stack.Push(0)
stack.Push(len(nums) - 1)
for !stack.Empty() {
right := stack.Pop()
left := stack.Pop()
if left < right {
middle := partitionArray(nums, left, right)
stack.Push(left)
stack.Push(middle - 1)
stack.Push(middle + 1)
stack.Push(right)
}
}
}
func partitionArray(nums []int, start, end int) int {
r, _ := rand.Int(rand.Reader, big.NewInt(int64(end-start)))
middle := int(r.Int64()) + start
nums[start], nums[middle] = nums[middle], nums[start]
middle = start
for i := start + 1; i <= end; i++ {
if nums[i] <= nums[middle] {
middle++
nums[middle], nums[i] = nums[i], nums[middle]
nums[middle-1], nums[middle] = nums[middle], nums[middle-1]
}
}
return middle
}