Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 16, 2017 07:08
Show Gist options
  • Save luojiyin1987/d175c909e5eef5b9908133051e7e40fa to your computer and use it in GitHub Desktop.
Save luojiyin1987/d175c909e5eef5b9908133051e7e40fa to your computer and use it in GitHub Desktop.
Two Sum II - Input array is sorted
func twoSum(numbers []int, target int) []int {
if len(numbers) <2 {
return nil
}
i :=0
j := len(numbers)-1
for i< j {
sum := numbers[i] + numbers[j]
if sum == target {
return []int{i+1, j+1}
break
} else if sum > target {
j--
} else {
i++
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment