-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-two-sum.go
49 lines (43 loc) · 1.01 KB
/
1-two-sum.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
package main
import (
"fmt"
)
func main() {
var testIntArr = []int{2, 7, 11, 17}
fmt.Println(twoSum(testIntArr, 9)) // [0,1]
fmt.Println(twoSum2(testIntArr, 24)) // [1,3]
}
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// example:
// Given nums = [2, 7, 11, 15], target = 9,
// Because nums[0] + nums[1] = 2 + 7 = 9,
// return [0, 1].
func twoSum(nums []int, target int) []int {
var ret = []int{}
for k, v := range nums {
for kk, vv := range nums {
if target == v+vv {
ret = append(ret, k)
ret = append(ret, kk)
return ret
}
}
}
return ret
}
func twoSum2(nArr []int, t int) []int {
ret := []int{}
lens := len(nArr)
for i := 0; i < lens; i++ {
for j := 0; j < lens; j++ {
fmt.Println(i, j)
if i < j && nArr[i]+nArr[j] == t {
ret = append(ret, i)
ret = append(ret, j)
return ret
}
}
}
return ret
}