python 双指针
和为S的两个数字
http://www.nowcoder.com/questionTerminal/390da4f7a00f44bea7c2f3d19491311b
https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/
题目描述
输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[2,7] 或者 [7,2]
示例 2:
输入:nums = [10,26,30,31,47,60], target = 40
输出:[10,30] 或者 [30,10]
限制:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6
思路
双指针
class Solution(object): def twoSum(self, nums, target): low = 0 high = len(nums) - 1 while high > low: tsum = nums[low] + nums[high] if tsum == target: return [nums[low], nums[high]] elif tsum > target: high -= 1 else: low += 1 return []