Created
August 16, 2017 07:08
-
-
Save luojiyin1987/d175c909e5eef5b9908133051e7e40fa to your computer and use it in GitHub Desktop.
Two Sum II - Input array is sorted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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