167. Two Sum II - Input array is sorted

Posted thinker-pcw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了167. Two Sum II - Input array is sorted相关的知识,希望对你有一定的参考价值。

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
 

来自 <https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/>

 

思路1:最简单直接的做法 

 1 class Solution(object):
 2     def twoSum(self, numbers, target):
 3         """
 4         :type numbers: List[int]
 5         :type target: int
 6         :rtype: List[int]
 7         """
 8         length = len(numbers)
 9         for i in range(length):
10             for j in range(1,length-i):
11                 if numbers[i] + numbers[i + j] == target:
12                     return i + 1, i + j + 1

 

思路2: 发现上面的做法将会超时,问题出现在最后一个测试样例,最后一个测试样例有许多重复样本,考虑先去除重复样本,减小计算花费

 1 class Solution(object):
 2     def twoSum(self, numbers, target):
 3         """
 4         :type numbers: List[int]
 5         :type target: int
 6         :rtype: List[int]
 7         """
 8         new_numbers = []
 9         counts = []
10         for i in numbers:
11             if new_numbers in new_numbers:
12                 counts[-1] += 1
13             else:
14                 new_numbers.append(i)
15                 counts.append(1)
16 
17         length = len(new_numbers)
18         for i in range(length):
19             for j in range(1, length - i):
20                 if new_numbers[i] + new_numbers[i + j] == target:
21                     pos_1 = pos_2 = 1
22                     for k in range(i):
23                         pos_1 += counts[k]
24                     for q in range(i + j):
25                         pos_2 += counts[q]
26                     return pos_1, pos_2

 

思路3:以上做法还是会超时,对于每个选中的元素,改用二分查找的做法查找另一个相加数

 1 class Solution(object):
 2     def twoSum(self, numbers, target):
 3         """
 4         :type numbers: List[int]
 5         :type target: int
 6         :rtype: List[int]
 7         """
 8         length = len(numbers)
 9         for i in range(length):
10             p = i
11             q = length
12             while q - p > 1:
13                 mid = (p + q) // 2
14                 if numbers[i] + numbers[mid] == target:
15                     return i + 1, mid + 1
16                 elif numbers[i] + numbers[mid] < target:
17                     p = mid
18                 else:
19                     q = mid

 

思路4:使用两个坐标,朝中间遍历

 1 class Solution(object):
 2     def twoSum(self, numbers, target):
 3         """
 4         :type numbers: List[int]
 5         :type target: int
 6         :rtype: List[int]
 7         """
 8         length = len(numbers)
 9         i = 0
10         j = length - 1
11         while j - i >= 1:
12             if numbers[i] + numbers[j] == target:
13                 return i + 1, j + 1
14             elif numbers[i] + numbers[j] < target:
15                 i = i + 1
16             else:
17                 j = j - 1

 



以上是关于167. Two Sum II - Input array is sorted的主要内容,如果未能解决你的问题,请参考以下文章

167. Two Sum II - Input array is sorted

167. Two Sum II - Input array is sortedeasy

167. Two Sum II - Input array is sorted

LeetCode167. Two Sum II - Input array is sorted

167. Two Sum II - Input array is sorted

167.Two Sum II–Input is sorted