leetcode1 twoSum
"""
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的更多相关文章
- Leetcode1——两数之和 详细解析
Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...
- JavaScript的two-sum问题解法
一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...
- twoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LeetCode_1] twoSum
LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...
- LeetCode1:Two Sum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- leetcode1:在数组中找2个数的和正好等于一个给定值--哈希
package java_net_test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; pu ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- LeetCode1 Two Sum
题目 :Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
随机推荐
- python爬虫(九) requests库之post请求
1.方法: response=requests.post("https://www.baidu.com/s",data=data) 2.拉勾网职位信息获取 因为拉勾网设置了反爬虫机 ...
- c++ 读取、保存单张图片
转载:https://www.jb51.net/article/147896.htm 实际上就是以二进制形式打开文件,将数据保存到内存,在以二进制形式输出到指定文件.因此对于有图片的文件,也可以用这种 ...
- WCF服务调用方式
WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务代理对象进行服务调用.
- redhat 7.6 apache 服务简单安装-01
rpm -qa | grep httpd //该命令查看apache是否安装,下面图片是已安装,未安装不会显示任何内容 yum install httpd -y ...
- python 中的 *args 和 **kwargs
在阅读Python代码时,经常会看到如下函数的定义: def fun(*args, **kwargs): 很多同学可能会对此感到困惑,这个 * args和 **kwargs是什么东西.为啥会在源码中应 ...
- ISAP 算法
Dinic 算法其实已经足够处理大多数的网络流了,但还不够快.接下来介绍的是最优秀的增广路最大流算法:ISAP(Improve Shortest Argumenting Path).它的时间复杂度上界 ...
- LeetCode 804 唯一摩尔斯密码词
package com.lt.datastructure.Set; import java.util.TreeSet; /* * 一个摩斯码,对应一个字母.返回我们可以获得所有词不同单词翻译的数量. ...
- Pycharm设置默认HTML模板
Pycharm设置默认HTML模板 Bootstrap导入链接 <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/ ...
- Linux系统chmod命令的含义和权限详解
许多喜欢使用chmod命令的用户,对chmod命令的含义和权限仍然不是很清楚,因此在使用的时候对它们造成了一定的麻烦.为了解决这些用户的迷惑,今天小编就和大家一起分享下chmod命令的含义和权限. 对 ...
- EMIS快速开发平台 - 微服务版技术选型
http://demo.zuoyour.com/system/login EMIS快速开发平台 - 微服务版技术选型 开发框架:Spring Boot 2.1.3.RELEASE 微服务:Spring ...