题解 | #和为S的两个数字# golang
和为S的两个数字
http://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b
func FindNumbersWithSum( array []int , sum int ) []int {
// write code here
left :=0
right := len(array)-1
if left==right {
return []int{}
}
// maxInt := int(^uint(0) >> 1)
maxInt := 100000
var tempMu int
answer := make([]int,2)
flag := false
var temp int
for left < right {
temp = array[left]+array[right]
if temp<sum {
left++
} else if temp>sum {
right--
} else {
tempMu = array[left] * array[right]
if tempMu<maxInt {
flag = true
answer[0]=array[left]
answer[1]=array[right]
maxInt = tempMu
}
left++
}
}
if flag {
return answer
} else {
return []int{}
}
}