N Sum
题目:N Sum
描述:
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.
样例:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
解决思路:像这种有关联关系的输入输出,C解决的话要考虑用哈希,Python的话我们直接考虑用字典解决就可以了,遍历数组,并且保存在字典中,查找字典中是否有符合的数字若符合直接返回数组下标和字典的value。
代码:
class Solution(object): #这里我用Python解决
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dict = {}
for i in xrange(len(nums)):
x = nums[i]
if target - x in dict:
return (dict[target - x],i)
dict[x] = i
知识点:
range和xrange的区别
>>> a = range(5)
>>> print type (a)
<type 'list'> #我们可以看出range生成的是一个list对象
>>> print range(5)[0] #从list列表中打印list[0]
0
>>> b = xrange(5)
>>> print type(b)
<type 'xrange'>
>>> print xrange(5)[0]#而xrange是一个xrange类型,不会直接生成一个list,而是在每次调用的时候返回其中的一个值,节省内存
0
N Sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- jmeter内存溢出
当我用jmeter来测试elasticsearch性能的时候,发生过三种性质的内存溢出. 1. index 由于数据流过大,内存使用超过jmeter默认的上限,就溢出了. 用记事本打开jmeter.b ...
- elasticsearch插件之一:bigdesk
bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况,http连接数等. 可用项目git地址:https:// ...
- winform的comboBox使鼠标滑轮修改值失效
目标: winform窗体很多combobox下拉框,当他们其中的一个获得焦点的时候,如果滚动鼠标就会改变下拉框的值,要实现让鼠标滚轮不对下拉框的值造成影响 如下代码直接拷贝粘贴,不用修改 方法一: ...
- MyEclipse8.5集成Tomcat7
我最近需要在MyEclipse中使用Tomcat7,已经在Servers中配置了本地的Tomcat路径,之后发布项,在MyEclipse启动Tomcat服务则出现如下错误提示: Exception i ...
- AIX查看内存卡槽
1.lscfg -vp|grep Processor 2.lscfg -vp|grep -p Memory
- Android 时间戳的转换
在Android应用中,经常会碰到后台的时间是时间戳而现实的需要今天什么时候,昨天什么时候,就像微博的时间显示一样.现在我上一个把时间戳转换的代码: public static String getT ...
- 标签navtab
创建navtab 创建一个navtab有以下两种方式: 1.Data属性:DOM添加属性data-toggle="navtab"后,单击触发. a链接示例: <a href= ...
- C# IList<T>转为DataTable
public class WebUtil { /// <summary> /// 转换IList<T>为DataTable/// </summary> /// &l ...
- C#基础之类、组件和命名空间(二)
一.实例化对象 Student s; 首先是在栈中开辟一块空间叫s,s里面的内容是空: s = new Student(); 在堆实例化Student对象,将对象的引用地址保存到栈s里.因此,s指向S ...
- objectARX 获取ucs的X方向
struct resbuf var; acedGetVar(_T("UCSXDIR"), &var);//获取用户坐标系下X方向量 ver = asVec3d(var.re ...