我在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. [Mugeda HTML5技术教程之12]制作跨屏互动应用

    mugeda动画平台还可以用来制作跨屏互动的动画应用,比如在PC端的大屏幕上显示动画的主界面,同时会显示出供手机扫描的二维码,手机扫描后会在手机上显示手机端动画界面.通过手机就可以和PC端的显示界面跨 ...

  2. 永久存储:腌制一缸美味的泡菜 - 零基础入门学习Python031

    永久存储:腌制一缸美味的泡菜 让编程改变世界 Change the world by program 从一个文件里读取字符串非常简单,但如果想要读取出数值,那就需要多费点儿周折.因为无论是read() ...

  3. 《转》ACTIONBAR-PULLTOREFRESHLIBS+沉浸式在部分手机上的布局错乱,目前知道的三星系统(TouchWiz)

    转载:http://www.cnblogs.com/wubingshenyin/p/4413672.html(原文连接) 前段时间看见ActionBar-PullToRefreshLibs用来刷新很好 ...

  4. 【转】ios开发之生成所缩略图方式

    亲测:两种方式都有效 第一种方式:缩略成固定的尺寸大小 - (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSiz ...

  5. PowerShell_零基础自学课程_1_初识PowerShell

    欢迎转载本系列文章:转载请注明出处:www.cnblogs.com/volcanol 自从微软推出.Net以来,微软旗下的windows体系就发生了很大的变化,首先是操作系统的界面的变化,例如vist ...

  6. 【Xamarin破解补丁找不到?】

    前面的博文,推荐竟然那么点数目?下面的这个网址是个各种破解资源的站点,里面说不定有你想要的. http://onhax.net/ 要学会在搜索框搜索... 好吧,其实里面就有Xamarin的破解补丁 ...

  7. unix c 01

    gcc编译器(代码的 预处理/汇编/编译/连接) C程序员一般写程序会定义 .c和.h两种文件 .c文件(源文件)中一般放代码的实现,.h文件(头文件)中放 各种声明和定义.   gcc -E __. ...

  8. DBA 经典面试题(4)

    1.如果信息采集管理系统(ICM)崩溃了怎么办?  答案:所有其他的管理器都会继续工作.ICM只会处理队列控制请求,意思是开启和关闭其他并发的管理器.    2.你如何加速打补丁的过程?    答案: ...

  9. Linux 通过HTTP进行域名更新

    一.3322动态域名更新接口 接口地址 API URL http://members.3322.net/dyndns/update HTTP请求 GET /dyndns/update?hostname ...

  10. C#使用自定义字体(从文件获取)

    在进行软件开发,尤其是开发WinForm程序时,有时为了实现界面的美化,不可避免的需要使用一些特殊的字体,但是在开发完成之后,将程序移到其他的机器上时,由于这些机器可能没有安装相应的字体,所以整个界面 ...