[LeetCode_1] twoSum
LeetCode: 1. 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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
中文:
说的就是给出一个整形数组nums,和一个整数target;返回两个数字的角标,这两个角标对应在数组中的数字加起来等于target。
你可以假设每种输入只有一个正确答案。
思路
暴力穷举的话就是使用2重循环,对每两个数字进行求和,然后当和为target的时候,返回这两个数。这种方法的时间复杂度也是显而易见的,为O(N^2),这种方法的时间损耗过大,不能接受。
另外一个思路就是利用哈希表,使用一个哈希表,然后对数组进行一重循环就可以了:对数组中的每个数计算c = target - nums[i],再查看c是否存在于哈希表中,如果存在则返回这两个数对应的角标;不存在则把当前数组中的这个数添加进哈希表中,然后继续循环。
代码
由于这个是在LeetCode刷的第一道题,最开始对LeetCode的代码提交方法不太熟悉;和POJ不一样,LeetCode不使用标准输入/输出作为评判依据,写一个类中的方法(名字已给出),然后进行判断。
class Solution{
public:
vector <int> twoSum(vector <int> &numbers, int target)
{
vector <int> v;
map <int ,int > m;
for (int i = 0; i < numbers.size(); ++i){
if (m.find(target - numbers[i]) != m.end()){
v.push_back(m[target - numbers[i]]);
v.push_back(i);
return v;
}
// add into the map
m[numbers[i]] = i;
}// end of for
return v;
}
};
方法返回一个int类型的vector,输入是一个vector numbers和一个整数。
这里面使用的是map,配合map的角标功能实现的哈希表,这个哈希表中的键和值是一样的。
[LeetCode_1] twoSum的更多相关文章
- leetcode_1. Two Sum
leetcode_1. Two Sum 前言: 这段时间开始敲leetcode.我认为这并不仅仅只是为了应付笔试,面试.而是确实有着一定的意义. 尤其,你提交代码后,网站会多方面验证你的答案. 另外, ...
- 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 ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- LeetCode——TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
随机推荐
- Centos上Docker 使用dockerfile构建容器实现ssh
这几日在学习docker.遇到的问题数一年都数不完,网上大多数都是ubuntu的,百度或者谷歌的时候心好累.写写文档来帮助使用centos的docker爱好者们. docker基本操作这里就不介绍了 ...
- Python开发问题和解决方案汇集
1.Sublime Text中用Tab批量替换空格Whitespace缩进:Ctrl+A全选代码,Ctrl+Shift+P打开下拉框,输入indent,找到Convert indentation to ...
- 【iCore3 双核心板】例程二:读取arm按键状态
实验指导书及代码包下载: http://pan.baidu.com/s/1sjrHnM9 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 【iCore3 双核心板_FPGA】实验二十三:使用JTAG UART终端打印信息
实验指导书及代码包下载: http://pan.baidu.com/s/1c83OPC iCore3 购买链接: https://item.taobao.com/item.htm?id=5242294 ...
- MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size
http://dev.mysql.com/doc/refman/5.7/en/create-table.html You can use the TEMPORARY keyword when crea ...
- Population Mean
Probability and Statistics > Moments > History and Terminology > Disciplinary Terminology & ...
- Nosql学习笔记
1.利用Query查询,Query操作只搜索主键属性值,并支持对键属性值使用部分比较运算符,以优化搜索过程. * 查询结果始终按范围键排序.如果范围键的数据类型是数字,则会按数字顺序返回结果:否则,会 ...
- 《Linux内核分析》第二周 操作系统是如何工作的?
[刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK TWO(2 ...
- 使用paramiko模块远程登录并上传或下载文件
1.paramiko安装 1)安装PyCrypto2.6 for Python 2.7 64bit.地址:http://www.voidspace.org.uk/python/modules.shtm ...
- SSD果然劲爆!
前两周入手了一块浦科特128G盘,不说多了,有图为证 以前把机械盘放在主硬盘位的时候,鲁大师显示是SATA II接口,现在把SSD放在主硬盘位,显示居然是SATA III接口了,看上面测试,确实是II ...