Leetcode_Two Sum II

来源:互联网 发布:手机伴唱软件 编辑:程序博客网 时间:2024/05/29 17:04

Tag
Array\Two Pointers\Binary Search
Difficulty
Easy
Description
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 and you may not use the same element twice.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
Code
正常思路,一个一个搜索,时间复杂度O(n^2)
因为是有序数组,所以可以用二分搜索,复杂度降为O(nlogn)

class Solution(object):    def twoSum(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: List[int]        """        for i in range(len(nums)):            tmp=target-nums[i]            start=i+1            end=len(nums)-1            while(start<=end):                if tmp==nums[end]:                    return [i+1,end+1]                if tmp==nums[start]:                    return [i+1,start+1]                mid = (end - start) / 2+start                if mid==start or mid==end:                    break                if tmp > nums[mid]:                    start=mid                elif tmp< nums[mid]:                    end=mid                else:                    return [i+1,mid+1]
0 0
原创粉丝点击