Leetcode Link: 剑指 Offer 57. 和为s的两个数字 - 力扣(LeetCode)

题目

解法一:对向双指针

思路: 两边对向走,大于则右-,小于则左+

题解

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        l = 0
        r = len(nums)-1
        while(l!=r):
            if nums[l]+nums[r] > target:
                r -= 1
            elif nums[l]+nums[r] < target:
                l += 1
            else:
                return [nums[l], nums[r]]

解法二

思路

题解

解法三

思路

题解

启发和联系