MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / twoSumSortedInput

Function twoSumSortedInput

two_sum_1/solution.go:25–50  ·  view source on GitHub ↗

twoSumSortedInput assumes that nums is sorted.

(nums []int, target int)

Source from the content-addressed store, hash-verified

23
24// twoSumSortedInput assumes that nums is sorted.
25func twoSumSortedInput(nums []int, target int) []int {
26 numsLen := len(nums)
27 if numsLen < 2 {
28 return []int{}
29 }
30
31 front := 0
32 rear := numsLen - 1
33 for front != rear {
34 twoSum := nums[front] + nums[rear]
35 if twoSum == target {
36 return []int{
37 front,
38 rear,
39 }
40 }
41
42 if twoSum < target {
43 front++
44 } else {
45 rear--
46 }
47 }
48
49 return []int{}
50}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected