[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 ...
随机推荐
- 自动解析URL
function parseURL(url) { var a = document.createElement('a'); a.href = url; return { source: url, pr ...
- php——用for循环打印半金字塔、金字塔、正方形、倒金字塔、菱形、空心图形等
1.半金字塔 $n=5; //控制层数 for($i=1;$i<=$n;$i++){ //控制每层的 “*” 数 for($j=1;$j<=$i;$j++){ echo "*& ...
- 省略号 对单行 多行的css
.twoline{ display: -webkit-box !important;; overflow:hidden; text-overflow: ellipsis; word-break: br ...
- 读过的laravel文章
Laravel 中使用 JWT(Json Web Token) 实现基于API的用户认证 http://www.tuicool.com/articles/IRJnaa api token https: ...
- DevExpress GridView对表格的部分说明
1. int[] selects = this.m_grdView1.GetSelectedRows(); // 获取选中的行,可能是几行 2. this.m_grdView1.GetRowCellV ...
- jquery 图片本地预览
uploadPreview.js /* *名称:图片上传本地预览插件 v1.1 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *参数说明: I ...
- LightOj 1213 - Fantasy of a Summation(推公式 快速幂)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1213 #include <stdio.h> int cases, case ...
- SQL基础巩固2
日期函数 函数名称 含义 示例 GetDate 返回当前系统日期和时间,返回值类型为datetime select GETDATE()//输出当前日期 YEAR 返回指定日期的年份 YEAR('08/ ...
- Java 并发性和多线程
一.介绍 在过去单 CPU 时代,单任务在一个时间点只能执行单一程序.之后发展到多任务阶段,计算机能在同一时间点并行执行多任务或多进程.虽然并不是真正意义上的“同一时间点”,而是多个任务或进程共享一个 ...
- Docker的私有仓库
server 192.168.1.107 registry ---push client 192.168.1.103 --pull [192.168.1.107 ...