题目在这里: https://leetcode.com/problems/two-sum/

【标签】Array; Hash Table

【个人分析】

  这个题目,我感觉也可以算是空间换时间的例子。如果是O(n^2)的那种思路,就是对于一个数字,去扫剩下的所有数字,看有没有能够加起来和为target的组合。但是如果加入一个哈希表,我们扫过的数字都可以记录下来。

我用的是 (target - number) 作为key, 用number在nums中的 index 作为 value, 遇到一个新的数字number,如果它存在于map 中(说明我们先前遇到过target - number),那么我们就是找到结果了。

 public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
int number = nums[i];
if (indexMap.containsKey(number)) {
// found the two numbers we are looking for!
// ! required result is not zero-based indexed, but 1-based
result[0] = 1 + indexMap.get(number);
result[1] = 1 + i;
break;
} else {
// put the number we are expecting into the map
indexMap.put(target - number, i);
}
}
return result;
} }

[Leetcode][001] Two Sum (Java)的更多相关文章

  1. 【JAVA、C++】LeetCode 001 Two Sum

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

  2. LeetCode #001# Two Sum(js描述)

    索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...

  3. leetcode 112 Path Sum ----- java

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  4. leetcode 39 Combination Sum --- java

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. leetcode 1. Two Sum [java]

    注意点: HashMap<Integer, Integer> return new int[]{}; 3 2 4 target:6 return null; public int[] tw ...

  6. Java for LeetCode 216 Combination Sum III

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

  7. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  8. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  9. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

随机推荐

  1. centos6.2下安装星际译王stardict3.0

    星际译王是一个Linux下很好的翻译软件. 我的系统是centos6.2 32位版.本来在http://code.google.com/p/stardict-3/downloads/list 上下的源 ...

  2. 找不到请求的 .Net Framework Data Provider。可能没有安装。

    解决方法: 安装Microsoft SQL Server Compact 4.0. 安装Microsoft SQL Server Compact 4.0之后,程序运行正常. 问题的原因就是程序连接.s ...

  3. MVC中修改报错

    修改的时候有空值传入.

  4. 探讨VMP 2.12.3 导入表修复

    壳版本:VMProtect.Ultimate.2.12.3 样本:notepad.exe 目的:IAT修复 作者:MrWrong 标题:探讨VMP 2.12.3 导入表修复 只是感兴趣,没有其他目的. ...

  5. 转:VC中UpdateData()函数的使用

    VC中UpdateData()函数的使用 UpdateData(FALSE)与UpdateData(TRUE)是相反的过程     UpdateData(FALSE)是把程序中改变的值更新到控件中去  ...

  6. PHP 之isset() 与 unset()

    isset()用来判断某个变量是否已经被声明,他返回一个boolean类型的值,如果声明则返回true否则返回false.如果变量被声明后,给他赋值为NULL,他也返回false. 如: <?p ...

  7. Ubuntu 下启动/停止/重启mysql服务

    1:sudo start mysql 2:sudo stop mysql 3:sudo restart mysql

  8. android数据保存

    永久保存数据的方法:1.Shared Preferences 以键值对的形式存储基本数据类型( booleans, floats, ints, longs, and strings),存储的数据在限制 ...

  9. Eclipse插件管理

    Eclipse 的特色之一,就是它的插件功能.可以说, Eclipse 是一个插件的大集合,所有的模块都以插件的形式存在.那么,究竟什么是插件呢? 插件( plug-in ),即 Eclipse 的功 ...

  10. DP #1 Singleton Pattern线程安全问题

    单例模式确保一个类只有一个实例,自行提供这个实例并向整个系统提供这个实例. 其中涉及到最主要的问题就是在多线程并发时线程安全问题. 单例模式的实现也有一个循序渐进的过程:1.最基本要求:每次从getI ...