[py]letcode第一题求和
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第一题求和的更多相关文章
- Project Euler 第一题效率分析
		
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
 - [算法 笔记]2014年去哪儿网 开发笔试(续)第一题BUG修正
		
上一篇的blog地址为:http://www.cnblogs.com/life91/p/3313868.html 这几天又参加了一个家公司的笔试题,在最后的编程题中竟然出现了去哪儿网开发的第一题,也就 ...
 - 《学习OpenCV》练习题第五章第一题ab
		
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...
 - 《学习OpenCV》练习题第四章第一题b&c
		
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
 - 《学习OpenCV》练习题第四章第一题a
		
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
 - Google Code Jam 第一题
		
通过的第一题,留做纪念,呵呵,非常简单,Africa 2010, Qualification Round: Store Credit. #include <stdio.h> #includ ...
 - 图论测试题(一)第一题:longest
		
第一题:longest 乌托邦有n个城市,某些城市之间有公路连接.任意两个城市都可以通过公路直接或者间接到达,并且任意两个城市之间有且仅有一条路径(What does this imply? A tr ...
 - ZOJ 2334(Monkey King-左偏树第一题)
		
Monkey King Time Limit: 10 Seconds Memory Limit: 32768 KB Once in a forest, there lived N aggre ...
 - BZOJ 3172([Tjoi2013]单词-后缀数组第一题+RMQ)
		
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 268 Solved: 145 [ Submit][ St ...
 
随机推荐
- 【大数据系列】hadoop集群设置官方文档翻译
			
Hadoop Cluster Setup Purpose Prerequisites Installation Configuring Hadoop in Non-Secure Mode Config ...
 - JS - 常用效果代码库 (四)
			
1.首字母大写示例: var value = “一段文本或一个参数”; value = value.toString() return value.charAt(0).toUpperCase() + ...
 - SVN —— 如何设置代理
			
如果在使用SVN下载外网的资源时,出现这样的提示:No such host is known. 或者 不知道这样的主机,可能是机器网络的问题. 如果浏览器能够正常访问外网,那应该是网络设置了代理的问题 ...
 - 阅读代码工具:Visual Studio Code
			
打开一个文件夹,直接阅读,体验还不错 版本: 1.25.1提交: 1dfc5e557209371715f655691b1235b6b26a06be日期: 2018-07-11T15:43:11.471 ...
 - pycharm的版本问题
			
1.分类: 专业版是收费的 Professional 教育版是免费 edu https://www.jetbrains.com/pycharm-edu/whatsnew/ 社区版是免费的 Free C ...
 - Mysql On Mac OS: Remove & Install
			
If you downloaded and installed from .dmg package already, and mightbe sometime it sucks because of ...
 - 微信小游戏 50M那部分的缓存机制的使用
			
一.使用 AssetsManager 灵活定制微信小游戏的缓存策略 官网教程:http://developer.egret.com/cn/github/egret-docs/Engine2D/mini ...
 - Java虚拟机六 堆溢出的处理
			
在Java程序中,如果堆空间不足,有可能抛出内存溢出错误:Out Of Memory,简称OOM. Exception in thread "main" java.lang.Out ...
 - was cached in the local repository, resolution will not be reattempted until the update interval of localhost-repository has elapsed or updates are forced
			
ailed to collect dependencies at com.eshore:common:jar:0.0.1-SNAPSHOT: Failed to read artifact descr ...
 - vsftpd java程序无法创建和切换目录
			
# Example config file /etc/vsftpd/vsftpd.conf # # The default compiled in settings are fairly parano ...