LeeCode a daily problem - two numbers and enter an ordered array II-

  [Introduction] adhere day more LeeCode brush title series

    short step, a thousand miles; not small streams into a mighty torrent. We would like to encourage each other and gentlemen!


  [136.] Title number only appears once

    subject description: has been given a follow ascending ordered array to find the number of two numbers such that their sum is equal to the target sum. Function
number should return these two index values and index1 index2, which must be less than index1 index2.

    Description:

      Return values of the index (index1, index2 and) is not zero.
      You can assume that each input corresponding to only the only answer, but you can not reuse the same elements.


    Example:

		示例 1:
			输入: numbers = [2, 7, 11, 15], target = 9
			输出: [1,2]
			解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。

    思路一:First, we have to use violent means to solve or to think about this question. The following steps:
         1. Establish two cycles through the array
         2. If the index does not equal two (the same number of added negative) and the corresponding index number in addition to target, the list is returned.
         Specific code as follows:

class Solution(object):
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int
        :rtype: List[int]
        """
        result_list = []
        for i in range(len(numbers)):
            for j in range(len(numbers)):
                if(i!=j and numbers[i]+numbers[j]==target):
                    result_list.append(i+1)
                    result_list.append(j+1)
                    return result_list

    Run Result: the 超时error, by way of two-cycle, the time complexity is O (n ^ 2), and therefore For long list, a timeout occurs the phenomenon.


    思路二:I refer to this in the use of the dictionary memory, analog hash table to solve this problem. Specific code as follows:

class Solution(object):
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int
        :rtype: List[int]
        """
        hashmap = {}      #因为我们想得到index,所以我们将num作为键
        for index,num in enumerate(numbers):
            another_num = target - num
            if(another_num in hashmap):
                return [hashmap[another_num]+1,index+1] #注意return时两个index的顺序
            else:
                hashmap[num] = index
        return None

    operation result:
    Here Insert Picture Description

    思路三:It is clear that the official is not the same two questions, then there is a focus on the title tag of the conditions 升序排列, then we should be how to take advantage of this condition is it? My first thought was half way to find and see the details of a senior thinking about the dichotomy in the solution to a problem, so a direct link leads to big brother like, friends who are interested can get to know next.


    Some links on knowledge:
    half posture

    Share on here, welcome to discuss the exchange.


    注明

    Topic Source: stay button (LeetCode)
    link: https: //leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted

Published 32 original articles · won praise 62 · views 1294

Guess you like

Origin blog.csdn.net/Mingw_/article/details/104800851