[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 ...
随机推荐
- 前端技术-PS切图
页面制作部分之PS切图 <--本标签下,通过页面制作.页面架构.javascript程序设计.DOM编程艺术.产品前端架构五部分来分享总结笔记,总结笔记会陆续分享--> 网页设计在技术层面 ...
- SQL Server 2008 下载及安装教程
sql server 2008 是微软公司开发的一套数据库管理系统.是目前大型数据库中常用数据库之一.性能稳定,功能强大,是面向中大型企业的一款数据库解决方案.我们安装SqlServer2008的时候 ...
- WordPress插件开发实例教程 - 版权插件
说明:本教程仅限学习,高手请绕道 开发程序:WordPress 3.9-RC1 使用主题:Twenty Fourteen 在开始之前,需要注意三件事情 I.给插件取一个个性化的名字,越个性化越好,以防 ...
- Web前端开发基础 第三课(与浏览者交互)
来自慕课网,整理 语法: <form method="传送方式" action="服务器文件"> 讲解: 1.<form> :<f ...
- sqlserver集合操作
SQLServer2005通过intersect,union,except和三个关键字对应交.并.差三种集合运算 详细如下 use tempdb go if (object_id ('t1' ) is ...
- spring log4j.properties 没有日志的问题
一. log4j.properties 1. log4j.properties放在spring工程的src/main/rescours目录下无法读取. 测试后发现需要把log4j.properti ...
- mongo vue的常用操作
查找在某个范围内的记录: {"_id":{$in: [a,b,c]}} 如果images是个数组,则查询方式与普通数据一样:{"images":&quo ...
- JS中的_proto_(2)
function God(){} function Foo(){ this.name="Foo~~"; } Foo.prototype = new God(); function ...
- AngularJS 实现简单购物车
使用AngularJS实现一个简单的购物车,主要感受强大的双向绑定和只关注对象不关注界面特性. 先看看界面: 点击+-操作和删除: 这些全部只需要操作数据源就行,不需要关注界面. 实现过程: 一.使用 ...
- 根据value选择select
<script> var tem="{$Zgoods.type_2}"; $("#type_2 option[value='"+tem+" ...