167. Two Sum II - Input array is sorted**

原创 2016年08月29日 21:32:27

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. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2


My code:

class Solution(object):
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int
        :rtype: List[int]
        """
        n = len(numbers)
        low = 0
        high = n-1
        sums=numbers[low]+numbers[high]
        while sums!=target:
            if sums >target:
                high -=1
            else:
                low +=1
            sums=numbers[low]+numbers[high]
        return low+1, high+1


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode167.Two Sum II - Input array is sorted题解

1. 题目描述Given an array of integers that is already sorted in ascending order, find two numbers suc...
  • lby0910
  • lby0910
  • 2017年03月08日 13:07
  • 187

leetcode题解-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 ...

leetcode note--leetcode 167 Two Sum II - Input array is sorted

167. Two Sum II - Input array is sorted Given an array of integers that is already ...

【LeetCode】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 the...

[leetcode] 167. Two Sum II - Input array is sorted

[leetcode] 167. Two Sum II - Input array is sorted

【leetcode】167. Two Sum II - Input array is sorted

Difficulty:Medium Given an array of integers that is already sorted in ascending order, find two ...

LeetCode 167. Two Sum II - Input array is sorted

题目 : Given an array of integers that is already sorted in ascending order, find two numbers such th...

LeetCode 167:Two Sum II - Input array is sorted

排好序(升序)的一列数numbers,给定一个数key,求数列中是否存在两个数的和为给定的数key,若有则返回下标(从1开始,并不是0) 双指针问题,一个指向前部st,一个指向尾部etd。 那么分...

167. Two Sum II - Input array is sorted M

167. Two Sum II - Input array is sorted M

Leetcode 167. Two Sum II - Input array is sorted (Medium) (cpp)

167. Two Sum II - Input array is sorted (Medium)
  • Niko_Ke
  • Niko_Ke
  • 2016年08月20日 11:20
  • 256
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:167. Two Sum II - Input array is sorted**
举报原因:
原因补充:

(最多只允许输入30个字)