我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步。

题目:

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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

Tag:

Array; Hash Table

体会:

啊哈哈,这道题一次成功,虽然版本跟下面这个不一样,但是思路是一样的。

这个题很巧妙的用了map,因为是找两个数凑起来能是target, 所以遇到一个数,你就知道了它应该和谁去凑对,虽然不知道能凑对的这个数是否在array中存在又或者存在在什么地方。搞一个<number, index>的map, 记录每个数字出现的位置。逐个去检查数字,看看他要凑成对的那个数字是不是已经存在了即可。

P.S. 我的map里直接存的是要去找的那个数。比如target=9, 现在在位置0遇到了2,那我就存一个map[9-2]=0,然后检查的时候就可以直接去keys里面找有没有7了。

 class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
int n = numbers.size();
vector<int> result;
map<int, int> index;
for (int i = ; i < n; i++) {
if (index.count(numbers[i]) != ) {
// if exists
result.push_back(index[numbers[i]] + );
result.push_back(i + );
break;
}
index[target - numbers[i]] = i;
}
return result;
}
};

[Leetcode] Two Sum (C++)的更多相关文章

  1. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  2. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  6. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  8. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  10. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. Java中list<Object>集合去重实例

    一:Java中list去重的方法很多,下面说一下其中一种方法:把list里的对象遍历一遍,用list.contain(),如果不存在就放入到另外一个list集合中: 二:实例 这里需要注意的是:使用c ...

  2. Eclipse开发Python项目

    最近倒腾python自带的开发工具idle,用的很不习惯,还是用Eclipse编写python项目方便(自动补齐,智能报错,调试方便),下面就说说怎么用Eclipse编写python代码吧~ 1.安装 ...

  3. MS Build参考

    以下链接很详细的讲述了Build方面相关的知识,顺带连Link的参数都解释了,以后不知道就来这里查一查了. http://msdn.microsoft.com/zh-CN/library/ee8624 ...

  4. Linux用户基础

    http://itercast.com/lecture/21 操作系统通过用户.组概念来管理使用计算机的人 用户代表一个使用计算机的使用者,操作系统通过用户概念限制一个使用者能够访问的资源 组用来组织 ...

  5. Java vs Python

    面试时常问到这两种语言的区别,在此总结一下. Referrence: Udemy:python-vs-java Generally, Python is much simpler to use, an ...

  6. poj2787 算24

    每次枚举两个数,一个运算符,得到一个运算结果.在重复执行,直到只剩下一个数即可. #include <iostream> #include <cmath> using name ...

  7. TextView总结

    先看看TextView的类继承结构: java.lang.Object ↳ android.view.View ↳ android.widget.TextView 是直接继承View类的,可扩展性很强 ...

  8. onethink 验证码二维码不显示的问题

    常规思路: 1 检查GD和FreeType.在项目根目录(index.php同级)下放一个php文件 <?php echo phpinfo(); ?> 访问此文件,查看GD和FreeTyp ...

  9. 第01讲- Android背景知识

    第01讲Android背景知识 Android是基于Linux系统 Android系统框图 : 第一.操作系统层(OS) 第二.各种库(Libraries)和Android 运行环境(RunTime) ...

  10. 学艺不精,又被shell的管道给坑了

    我用过bash shell,而且时间不短了.但我从来没学过shell,至少没有像C++这么认真去学.平时写些基本的脚本没问题,不懂也可以google.百度.可在2014最后一天,掉坑里了. 其实脚本也 ...