-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1.go
More file actions
61 lines (54 loc) · 1.38 KB
/
Copy pathsolution1.go
File metadata and controls
61 lines (54 loc) · 1.38 KB
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
package solution1
// ============================================================================
// 1. Two Sum
// URL: https://leetcode.com/problems/two-sum/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1---Two-Sum
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_twoSum_Iterations-24 100000000 13.31 ns/op 16 B/op 1 allocs/op
Benchmark_twoSum_Map-24 38308172 29.08 ns/op 16 B/op 1 allocs/op
PASS
*/
func twoSum_Iterations(nums []int, target int) []int {
maxval := len(nums)
if maxval < 2 || maxval > 10_000 {
return []int{}
}
if maxval == 2 && nums[0]+nums[1] == target {
return []int{0, 1}
}
for i := 0; i < maxval; i++ {
for j := i + 1; j < maxval; j++ {
t := nums[i] + nums[j]
if t == target {
return []int{i, j}
}
}
}
return []int{}
}
// ============================================================================
func twoSum_Map(nums []int, target int) []int {
maxval := len(nums)
if maxval < 2 || maxval > 10_000 {
return []int{}
}
if maxval == 2 && nums[0]+nums[1] == target {
return []int{0, 1}
}
var t int
m := make(map[int]int, maxval)
for k, v := range nums {
t = target - v
_, ok := m[t]
if ok {
return []int{m[t], k}
}
m[v] = k
}
return []int{}
}