"""
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]. """ """
第一种是思路简单的方法,先遍历求出目标值,再遍历目标值是否再数组中,
判断如果下标相等则继续,最后返回[i,j]
"""
class Solution1(object):
def twoSum(self, nums, target):
n = len(nums)
for i in range(n):
a = target - nums[i]
if (a in nums): #这里其实隐含了一层循环
j = nums.index(a)
if (i==j):
continue
else:
return [i,j]
break
else:
continue
"""
第二种方法用了dict查找,提高效率
enumerate函数用法:
seasons = ['Spring', 'Summer', 'Fall']
list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall')]
"""
class Solution2(object):
def twoSum(self, nums, target):
dict = {}
for i, num in enumerate(nums):
if target - num in dict:
return [dict[target - num], i]
else:
dict[num] = i
nums = [2, 7, 11, 15]
target = 9
result1 = Solution1()
result2 = Solution2()
print(result1.twoSum(nums, target))
print(result1.twoSum(nums, target))

leetcode1 twoSum的更多相关文章

  1. Leetcode1——两数之和 详细解析

    Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...

  2. JavaScript的two-sum问题解法

    一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...

  3. twoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  5. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  6. LeetCode1:Two Sum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  7. leetcode1:在数组中找2个数的和正好等于一个给定值--哈希

    package java_net_test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; pu ...

  8. [Lintcode two-sum]两数之和(python,双指针)

    题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...

  9. LeetCode1 Two Sum

    题目 :Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

随机推荐

  1. 泛型DAO模型设计

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKC

  2. 计算机网络历史与基本概念&分层与参考模型(TCP/IP与OSI)&通信过程

    Definition: 计算机网络:使用单一技术相互连接的自主计算机的互联集合. 单台计算机独立自主(不受制于其他计算机),连接介质可以使光纤.铜线也可以是微波.红外.卫星. 互联网络(Interne ...

  3. HDU1029 简单DP

    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. &quo ...

  4. MySQL : INSERT INTO SELECT

    INSERT INTO wx_announcement_push ( title, content, STATUS, del_flag, user_login_name ) SELECT '大家好', ...

  5. Codeforces Round #620 (Div. 2) 题解

    A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...

  6. 图片切换.----so屌

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 搭建 nginx + rtmp 媒体服务器笔记

    工作需要搭建一个流媒体服务器,用来接收前端推过来的视频流,达到实时保存的目的. 具体步骤网上已经比较详细了 可以参考下面这个文档参考文档 https://www.cnblogs.com/monjeo/ ...

  8. 学习Javascript的8张思维导图

    分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...

  9. Genymotion连接失败问题

    adb启动问题:Invalid argument: cannot open transport registration socketpair could not read ok from ADB S ...

  10. Prometheus组件

    Prometheus组件 上一小节,通过部署Node Exporter我们成功的获取到了当前主机的资源使用情况.接下来我们将从Prometheus的架构角度详细介绍Prometheus生态中的各个组件 ...