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:

  1. Given nums = [2, 7, 11, 15], target = 9,
  2. Because nums[0] + nums[1] = 2 + 7 = 9,
  3. return [0, 1].

中文:

说的就是给出一个整形数组nums,和一个整数target;返回两个数字的角标,这两个角标对应在数组中的数字加起来等于target。

你可以假设每种输入只有一个正确答案。

思路

暴力穷举的话就是使用2重循环,对每两个数字进行求和,然后当和为target的时候,返回这两个数。这种方法的时间复杂度也是显而易见的,为O(N^2),这种方法的时间损耗过大,不能接受。

另外一个思路就是利用哈希表,使用一个哈希表,然后对数组进行一重循环就可以了:对数组中的每个数计算c = target - nums[i],再查看c是否存在于哈希表中,如果存在则返回这两个数对应的角标;不存在则把当前数组中的这个数添加进哈希表中,然后继续循环。

代码

由于这个是在LeetCode刷的第一道题,最开始对LeetCode的代码提交方法不太熟悉;和POJ不一样,LeetCode不使用标准输入/输出作为评判依据,写一个类中的方法(名字已给出),然后进行判断。

  1. class Solution{
  2. public:
  3. vector <int> twoSum(vector <int> &numbers, int target)
  4. {
  5. vector <int> v;
  6. map <int ,int > m;
  7. for (int i = 0; i < numbers.size(); ++i){
  8. if (m.find(target - numbers[i]) != m.end()){
  9. v.push_back(m[target - numbers[i]]);
  10. v.push_back(i);
  11. return v;
  12. }
  13. // add into the map
  14. m[numbers[i]] = i;
  15. }// end of for
  16. return v;
  17. }
  18. };

方法返回一个int类型的vector,输入是一个vector numbers和一个整数。

这里面使用的是map,配合map的角标功能实现的哈希表,这个哈希表中的键和值是一样的。

[LeetCode_1] twoSum的更多相关文章

  1. leetcode_1. Two Sum

    leetcode_1. Two Sum 前言: 这段时间开始敲leetcode.我认为这并不仅仅只是为了应付笔试,面试.而是确实有着一定的意义. 尤其,你提交代码后,网站会多方面验证你的答案. 另外, ...

  2. JavaScript的two-sum问题解法

    一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...

  3. twoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  5. [Lintcode two-sum]两数之和(python,双指针)

    题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...

  6. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  7. LeetCode——TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  8. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  9. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

随机推荐

  1. 前端技术-PS切图

    页面制作部分之PS切图 <--本标签下,通过页面制作.页面架构.javascript程序设计.DOM编程艺术.产品前端架构五部分来分享总结笔记,总结笔记会陆续分享--> 网页设计在技术层面 ...

  2. SQL Server 2008 下载及安装教程

    sql server 2008 是微软公司开发的一套数据库管理系统.是目前大型数据库中常用数据库之一.性能稳定,功能强大,是面向中大型企业的一款数据库解决方案.我们安装SqlServer2008的时候 ...

  3. WordPress插件开发实例教程 - 版权插件

    说明:本教程仅限学习,高手请绕道 开发程序:WordPress 3.9-RC1 使用主题:Twenty Fourteen 在开始之前,需要注意三件事情 I.给插件取一个个性化的名字,越个性化越好,以防 ...

  4. Web前端开发基础 第三课(与浏览者交互)

    来自慕课网,整理 语法: <form method="传送方式" action="服务器文件"> 讲解: 1.<form> :<f ...

  5. sqlserver集合操作

    SQLServer2005通过intersect,union,except和三个关键字对应交.并.差三种集合运算 详细如下 use tempdb go if (object_id ('t1' ) is ...

  6. spring log4j.properties 没有日志的问题

    一.   log4j.properties 1. log4j.properties放在spring工程的src/main/rescours目录下无法读取. 测试后发现需要把log4j.properti ...

  7. mongo vue的常用操作

    查找在某个范围内的记录: {"_id":{$in: [a,b,c]}}     如果images是个数组,则查询方式与普通数据一样:{"images":&quo ...

  8. JS中的_proto_(2)

    function God(){} function Foo(){ this.name="Foo~~"; } Foo.prototype = new God(); function ...

  9. AngularJS 实现简单购物车

    使用AngularJS实现一个简单的购物车,主要感受强大的双向绑定和只关注对象不关注界面特性. 先看看界面: 点击+-操作和删除: 这些全部只需要操作数据源就行,不需要关注界面. 实现过程: 一.使用 ...

  10. 根据value选择select

    <script> var tem="{$Zgoods.type_2}"; $("#type_2 option[value='"+tem+" ...