题目在这里: 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. SQL通过xml插入批量数据

    存储过程: CREATE PROCEDURE [dbo].[UP_PurchasexxxCard] @OrderInfo XMLASBEGIN SET NOCOUNT ON; DECLARE @Dat ...

  2. PYTHON 正则表达示入门

    确实是实践出真知,自己手打代码之后,以前停在理论上的东东,慢慢可以进入实战了. 比如,MATCH和SEARCH之间的区别. #encoding: UTF-8 import re pattern = r ...

  3. Android中的手势

    Android对两种手势行为提供了支持:1.对于第一种手势行为而言,Android提供了手势检测,并为手势检测提供了相应的监听器.2.对于第二种手势行为,Android允许开发者添加手势,并提供了相应 ...

  4. Android中设置文字大小的定义类型

    在Android中所有的组件可以设置大小,但是在设置大小的时候需要指定其单位,这些单位如下: px(pixels):像素: dip(device independent pixels):依赖于设备的像 ...

  5. Ubuntu下Qt-4.7.1的静态编译

    最近在学习Qt的静态编译,相比较来说windows的Qt静态编译比较容易,相反对于linux编译网上的文章实践下来都有这样那样的错误,这里简要小结一下自己的编译成果. 一.实验环境 1.Ubuntu  ...

  6. 强大疯狂的qttools

    就是有点疑惑,为什么不整合到QT主项目中呢? 有空好好看看: https://github.com/qtproject/qttools/tree/dev/src ------------------- ...

  7. 可以供MFC调用的,QT实现的DLL(使用qt-solutions的qtwinmigrate实现)

    MFC和QT的消息循环机制不同,所以,要让QT写的DLL可以供MFC调用,要做一点特殊的处理 #include <qmfcapp.h> #include <qwinwidget.h& ...

  8. git “bad index file sha1 signature fatal: index file corrupt”错误

    在执行commit或revert等操作时,提示“bad index file sha1 signature fatal: index file corrupt”错误,导致操作失败.这是由于git的in ...

  9. [又是BUG]常见的RuntimeException

    妈蛋这异常那异常都是异常,不能忍了! 下面总结一些经常遇到的异常(RuntimeExecption):   算术异常类:ArithmeticExecption 数组下标越界异常:ArrayIndexO ...

  10. [LeetCode] 56. Merge Intervals 解题思路

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...