letcode第一题, tm的不好弄. 想了很久想到了一个粗蠢的解决办法.

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].

第一次: 没考虑到值相同时

class getIndex():
def getSum(self, nums, target):
for i in nums:
for j in nums:
if i + j == target:
return nums.index(i),nums.index(j) s = getIndex()
s = s.getSum([3, 2, 4], 6)
print(s)

写了三四遍, 写成了这样,不过被accept了.

class Solution:
def twoSum(self, nums, target):
d = {}
# 将arr搞成字典
for k, v in enumerate(nums):
d[k] = v for i in nums:
# 计算target的另一半
tmp = target - i
# 如果i移出去了, 另一半还在, 求另一半的索引
arr = list(d.values())
arr.remove(i)
if tmp in arr:
index1 = nums.index(i)
index2 = arr.index(tmp) + 1
return index1, index2
if tmp in d.values() and nums.index(i) != nums.index(tmp):
return nums.index(i), nums.index(tmp) s = Solution()
# s = s.twoSum([3, 2, 3], 6)
# s = s.twoSum([3, 3], 6)
s = s.twoSum([2, 5, 5, 11], 10)
print(s)

别人更优雅的解决办法

这就是距离啊

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hash_map = {}
for index, value in enumerate(nums):
hash_map[value] = index
for index1, value in enumerate(nums):
if target - value in hash_map:
index2 = hash_map[target - value]
if index1 != index2:
return [index1 + 1, index2 + 1]

[py]letcode第一题求和的更多相关文章

  1. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

  2. [算法 笔记]2014年去哪儿网 开发笔试(续)第一题BUG修正

    上一篇的blog地址为:http://www.cnblogs.com/life91/p/3313868.html 这几天又参加了一个家公司的笔试题,在最后的编程题中竟然出现了去哪儿网开发的第一题,也就 ...

  3. 《学习OpenCV》练习题第五章第一题ab

    这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...

  4. 《学习OpenCV》练习题第四章第一题b&c

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  5. 《学习OpenCV》练习题第四章第一题a

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  6. Google Code Jam 第一题

    通过的第一题,留做纪念,呵呵,非常简单,Africa 2010, Qualification Round: Store Credit. #include <stdio.h> #includ ...

  7. 图论测试题(一)第一题:longest

    第一题:longest 乌托邦有n个城市,某些城市之间有公路连接.任意两个城市都可以通过公路直接或者间接到达,并且任意两个城市之间有且仅有一条路径(What does this imply? A tr ...

  8. ZOJ 2334(Monkey King-左偏树第一题)

    Monkey King Time Limit: 10 Seconds      Memory Limit: 32768 KB Once in a forest, there lived N aggre ...

  9. BZOJ 3172([Tjoi2013]单词-后缀数组第一题+RMQ)

    3172: [Tjoi2013]单词 Time Limit: 10 Sec   Memory Limit: 512 MB Submit: 268   Solved: 145 [ Submit][ St ...

随机推荐

  1. jquery on=>keyup无法绑定未来js添加的元素

    $("[name=red_count]").on("keyup", countKeyUpBind);即使使用on的,也无法绑定未来元素, 所以直接在动态添加的时 ...

  2. [转载]WebConfig配置文件详解

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  3. Disk Genius 彻底清理硬盘空闲

  4. how-to-build-c-static-libraries-boost

    http://tungchingkai.blogspot.jp/2016/11/how-to-build-c-static-libraries-boost.html How to build C++ ...

  5. 跟bWAPP学WEB安全(PHP代码)--邮件头和LDAP注入

    背景 由于时间限制和这俩漏洞也不是特别常用,在这里就不搭建环境了,我们从注入原来和代码审计的角度来看看. 邮件头注入 注入原理: 这个地方首先要说一下邮件的结构,分为信封(MAIL FROM.RCPT ...

  6. jquery.fly.min.js 拋物插件

    插件官方: https://github.com/amibug/fly, 官方例子: http://codepen.io/hzxs1990225/full/ogLaVp 首先加载jQuery.js和j ...

  7. 2015.7.12js-11(DOM基础)

    1.childNodes,获取子节点,本身就是一个数组,可以通过下标childNodes[i]来获取某个子节点. alert(obj.childNodes.length); //高级浏览器会有空白节点 ...

  8. 一个lucene源码分析的博客

    ITpub上的一个lucene源码分析的博客,写的比较全面:http://blog.itpub.net/28624388/cid-93356-list-1/

  9. mysql语句性能分析

    1.开启慢查询 slow_query_log = 1 //开启 slow_query_log_file = mysql_slow_query.log //日志文件位置 long_query_time ...

  10. HDU 3903 Trigonometric Function(数学定理)

    Trigonometric Function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Oth ...