Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Autosaved

...

https://leetcode.com/problems/two-sum/

LC 15. 3Sum 三数之和

...

Code Block
languagego
func twoSum(nums []int, target int) []int {
    contains := make(map[int]int, len(nums))
    for i, num := range nums {
        contains[num] = i
    }

    for j, num := range nums {
        if i, exist := contains[target-num]; i != j && exist {
            return []int{i, j}
        }
    }

    return nil
}

LC 167. Two Sum II - Input Array Is Sorted 两数之和 II- 输入有序数组

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

Code Block
func twoSum(nums []int, target int) []int {
    sort.Ints(nums)

    i, j := 0, len(nums)-1
    for i < j {
        sum := nums[i] + nums[j]
        if sum = target {
            return []int{nums[i], nums[j]}
        } else if sum < target {
            i++
        } else {
            j--
        }
    }

    return nil
}

LC 15. 3Sum 三数之和

https://leetcode.com/problems/3sum/

LC 18. 4Sum 四数之和

https://leetcode.com/problems/4sum/

...