Skip to content

Commit c816394

Browse files
committed
two_sum_II
1 parent c45acab commit c816394

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
144144
#### [164. Maximum Gap](https://github.com/hitzzc/go-leetcode/tree/master/maximum_gap)
145145
#### [165. compare version numbers](https://github.com/hitzzc/go-leetcode/tree/master/compare_version_numbers)
146146
#### [166. Fraction to Recurring Decimal](https://github.com/hitzzc/go-leetcode/tree/master/fraction_to_recurring_decimal)
147+
#### [167. Two Sum II](https://github.com/hitzzc/go-leetcode/tree/master/two_sum_II)
147148

148149

149150

two_sum_II/two_sum_II.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package two_sum_II
2+
3+
func twoSum(numbers []int, target int) []int {
4+
i, j := 0, len(numbers)-1
5+
var sum int
6+
for i < j {
7+
sum = numbers[i] + numbers[j]
8+
if sum == target {
9+
return []int{i + 1, j + 1}
10+
}
11+
if sum < target {
12+
i++
13+
} else {
14+
j--
15+
}
16+
}
17+
return []int{}
18+
}

0 commit comments

Comments
 (0)