Two Sum [easy] (Python)

由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个dict中,该dict以原数组中的值为键,原数组中的下标为值;第二遍扫描原数组,对于每个数nums[i]查看target-nums[i]是否在dict中,若在则可得到结果。 
当然,上面两遍扫描是不必要的,一遍即可,详见代码。

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        keys = {}
        for i in xrange(len(nums)):
            if target - nums[i] in keys:
                return [keys[target - nums[i]], i]
            if nums[i] not in keys:
                keys[nums[i]] = i

原文地址:https://www.cnblogs.com/boluo007/p/9121324.html

时间: 2024-07-30 22:33:11

Two Sum [easy] (Python)的相关文章

LeetCode--Array--Two sum (Easy)

1.Two sum (Easy) Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums

67. Add Binary [easy] (Python)

题目链接 https://leetcode.com/problems/add-binary/ 题目原文 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题目翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如:a = "11",b = &q

leetcode 【 Minimum Path Sum 】python 实现

题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 代码:oj测试通过 Runtime: 102 ms 1 c

leetcode:Path Sum【Python版】

1.类中递归调用函数需要加self # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @param sum, an integer # @return a boolean def hasPath

190. Reverse Bits [easy] (Python)

题目链接 https://leetcode.com/problems/reverse-bits/ 题目原文 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001

14. Longest Common Prefix [easy] (Python)

题目链接 https://leetcode.com/problems/longest-common-prefix/ 题目原文 Write a function to find the longest common prefix string amongst an array of strings. 题目翻译 写个函数,找出一个字符串数组中所有字符串的最长公共前缀. 题目描述不清晰...补充几个例子,比如: {"a","a","b"} 返回 &qu

1.Two Sum Leetcode Python

Given an array of integers, 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

58. Length of Last Word [easy] (Python)

题目链接 https://leetcode.com/problems/length-of-last-word/ 题目原文 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word

38. Count and Say [easy] (Python)

题目链接 https://leetcode.com/problems/count-and-say/ 题目原文 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is re