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 ...
随机推荐
- Unable to instantiate Action, xxxAction, defined for 'xxx' in namespace '/'xxxAction解决方案
出现这个问题的原因主要有两个 1.如果项目没有使用Spring,则struts.xml配置文件中,这个action的class属性的路径没有写完整,应该是包名.类名 2.如果项目使用了Spring,那 ...
- 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库
1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:Oracle系统调优
--修改 alter system set large_pool_size=64m; --显示 show parameter large_pool_size; select sum(getmisses ...
- 【转载】CentOS和Ubuntu的区别
CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代 ...
- Spring Cloud 从入门到入门
参考:https://blog.csdn.net/hellozpc/article/details/83692496 参考:https://www.fangzhipeng.com/spring-clo ...
- Java基础 -4.3
While循环结构 while循环 public static void main(String[] args) { while(布尔表达式) { 条件满足时执行; 修改循环条件; } } do wh ...
- 事件类型-UI事件、焦点事件
DOM3级事件包括以下几类事件: UI事件:当用户与页面上的元素交互时触发 焦点事件:当元素获得或失去焦点时触发 鼠标事件:当用户通过鼠标在页面上执行操作时触发 滚轮事件:当使用鼠标滚轮时触发 文本事 ...
- ubuntu14 安装git
1.安装git Step1 测试git是否安装,终端输入 $ git 没有安装时,不会识别git命令:
- 中山普及Day17——普及
今天换教室,本来教室多好嘛,易守难攻,结果...今天今天仅下午就被熊抄了2次,熊超真TMD不是人呐,走路连脚步声都没有. 然后,播报分数: 爆0了!!!
- 中山Day5——普及
今天题目真是贼难呐...才38... 收获:树状数组单个修改 树状数组区间修改 T1:旅行 题意:有n个数,问;从中取任意个数,他们的和为质数的方案数是多少?(n<=50) 暴力模拟即可,这里不 ...