zoukankan      html  css  js  c++  java
  • 001.两数之和

    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 = [2, 7, 11, 15], target = 9,

    Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

    题目:
    给定一个整数数列,找出其中和为特定值的那两个数。

    你可以假设每个输入都只会有一种答案,同样的元素不能被重用。

    示例:

    给定 nums = [2, 7, 11, 15], target = 9

    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]

    创建一个dict,用来存data  idx , 
     

    class Solution:
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            d = {}
            for i,num in enumerate(nums):
                if target - num in d:
                    return [d[target - num], i]
                d[num] = i
    关注公众号 海量干货等你
  • 相关阅读:
    struts2 DMI
    MFC添加背景图片
    c++ 副本构造器
    climits
    Qt中的qreal
    Http概述(一)
    重构学习-重构原则
    QDir的mkdir和mkpath区别
    Qt学习笔记网络(一)
    Qt5 新特性
  • 原文地址:https://www.cnblogs.com/sowhat1412/p/12734329.html
Copyright © 2011-2022 走看看