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 ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:显示在 <abbr> 元素中的文本以小号字体展示,且可以将小写字母转换为大写字母
<!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...
- Spark程序编译报错error: object apache is not a member of package org
Spark程序编译报错: [INFO] Compiling 2 source files to E:\Develop\IDEAWorkspace\spark\target\classes at 156 ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)B(SET)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[2007];set<int& ...
- dp - 活动选择问题
算法目前存在问题,待解决.. 活动选择问题是一类任务调度的问题,目标是选出一个最大的互相兼容的活动集合.例如:学校教室的安排问题,几个班级需要在同一天使用同一间教室,但其中一些班级的使用时间产生冲突, ...
- 莫烦 - Pytorch学习笔记 [ 一 ]
1. Numpy VS Torch #相互转换 np_data = torch_data.numpy() torch_data = torch.from_numpy(np_data) #abs dat ...
- Mysql 锁定 读情况
在一个事务中,标准的SELECT语句是不会加锁,但是有两种情况例外. SELECT ... LOCK IN SHARE MODE SELECT ... FOR UPDATE SELECT ... LO ...
- java#临时文件目录
String tmpDir=System.getProperty("java.io.tmpdir");
- rhel7 系统服务——unit(单元)
Linux内核版本从3.10后开始使用systemd管理服务,这也是系统开机后的第一个服务.systemd通过unit单元文件来管理服务. 它保存了服务.设备.挂载点和操作系统其他信息的配置文件,并能 ...
- ubuntu14 安装git
1.安装git Step1 测试git是否安装,终端输入 $ git 没有安装时,不会识别git命令:
- Laplacian Mesh Editing 拉普拉斯形变(待回学校更新)
前言 因为实验需要用到拉普拉斯形变,但找了好久找到一个非常适合入门的资料.再此记录下我的学习过程,也算搬运翻译过来. Introduction / Basic Laplacian Mesh Repre ...