[Leetcode][001] Two Sum (Java)
题目在这里: 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)的更多相关文章
- 【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 ...
- LeetCode #001# Two Sum(js描述)
索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...
- 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 ...
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- leetcode 1. Two Sum [java]
注意点: HashMap<Integer, Integer> return new int[]{}; 3 2 4 target:6 return null; public int[] tw ...
- 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 ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- 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 ...
- 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 ...
随机推荐
- JBPM4.4GPD设计器中文乱码问题的另一种解决方法
在eclipse中使用JBPM4.4的设计器时,输入中文后直接查看Source发现xml里中文全都乱码了,这时候大约整个人都不好了!赶紧百度.谷歌,搜到的多数结果都是要你在eclipse.ini或my ...
- 郝斌老师C语言学习笔记(一)
在给变量分配内存时,很可能这段内存存在以前其他程序使用留下的值.当使用VC编译器,若编译器发现没有给变量赋值而使用,就会返回一个以“85”开头的很大的数字(此时该段内存中为一个垃圾数,为了避免出现较常 ...
- CSS 3层嵌套居中布局
<html> <head> <style type="text/css"> .root{ background-color: red; widt ...
- 深度围观block:第三集
深度围观block:第三集 发布于:2013-07-12 10:09阅读数:7804 本文是深度围观block的第三篇文章,也是最后一篇.希望读者阅读了之后,对block有更加深入的理解,同时也希望之 ...
- 『电脑技巧』破解Win7/Win8登录密码
Pic via baidu 0x 00 破解思路 用户的明文密码经过单向Hash加密生成Hash散列,Hash散列又被加密存放在系统盘\Windiws\System32\config文件下 要获得明文 ...
- <item.../>元素可指定如下常用属性
android:id:为菜单项指定一个唯一表实.android:title:指定菜单项的标题.android:icon:指定菜单项的图标.android:alphabeticShortcut:为菜单项 ...
- LED驅動芯片對LED壽命的影響
5050年,領導作為一種新型節能光源在世界和中國有非常高的熱情和偉大的問題,不得不贏得市場占有率從室外到室內照明應用,中國也如雨后春筍般涌現在大型和小型LED照明企業.鑒于LED照明的主要原因是其促進 ...
- android中对线程池的理解与使用
前段时间有幸接到腾讯上海分公司的 Android开发面试,虽然最后一轮被毙了.但还是得总结一下自己在android开发中的一些盲点,最让我尴尬的是面试官问我几个android中线程池的使用与理解..哎 ...
- Android圆角矩形的实现
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android=&quo ...
- windows下MongoDB的安装及配置
http://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html 使用MongoVUE链接本地mongodb 基本用法见这里:http:// ...