[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 ...
随机推荐
- 带清空按钮的EditText
public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { // 删除按钮的 ...
- Yii源码阅读笔记(二十六)
Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: /** * Handles the specified request. * 处 ...
- php5-fpm.sock failed (13: Permission denied) error
In order to fix the php5-fpm.sock failed error follow these instructions 1) Make sure your virtual h ...
- 一些小题<代码>
1.用传统方法计算一个数的二进制 1.只能计算小于2**16的数字 i = 17 num_2 = int(input("输入一个数字:").strip()) while True: ...
- JQuery对象操作支持链式法则源码分析
JQuery链式法则 何为链式法则?先给出非链式写法的例子 //非链式写法 $("div").css("width", 45px); $("div&q ...
- Design and Analysis of Algorithms_Introduction
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Python 迭代器&生成器
1.内置参数 Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice ...
- Unity操作
聚焦到游戏物体: Hierarchy界面选中需要聚焦的物体,双击或者使用快捷键“F”: 在Scene面板中选中物体,使用快捷键“F” 放大缩小物体: alt+鼠标右键:鼠标滑轮 从各个角度观察 ...
- 通过setDB2Client*来方便的使用TRACE调优jdbc程序
一般来说通过TRACE的report来分析DDF的性能问题的话,基本对DBA都是噩梦一样.因为所有的Thread都是通过DDF一个类型的Thread,所以不管你怎么分类,看Accounting rep ...
- 微信、qq信息汇总、回复(一)
想法: 有的人喜欢用qq,有的人喜欢用微信,总而言之,是一个通信工具. qq上有很多群,微信上有很多群,每个群挨个浏览一遍.回复,还容易回复错误,前言不接后语. ...