[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 ...
随机推荐
- phpmyadmin密码字段加密方法
UPDATE member SET password=md5('password')
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...
- DbUtility-查询DataTable
直接上源码 using System; using System.Data; using System.Threading.Tasks; using DbUtility; namespace Test ...
- IIS Server Farms集群负载
序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一样,时不时的汹涌而至,让我们防不胜防.那么后台的高可用,以及服务器的处理能力就要做一个横向扩展的方案,以使后台业务持续的稳定 ...
- Entity Framework with MySQL 学习笔记一(关系)
这一篇说说 EF Fluent API 和 DataAnnotations 参考 : http://msdn.microsoft.com/en-us/data/jj591617.aspx http:/ ...
- 我只能说,CDH5真的屌爆了!!!
参考URL http://blog.csdn.net/yangzhaohui168/article/details/34185579 http://blog.csdn.net/yangzhaohui1 ...
- ExpandableList列表的简单应用
package com.test;//Download by http://ww.codefans.netimport java.util.ArrayList;import java.util.Has ...
- 抛出异常的区别 throw 和throw ex
在面试的过程中提到了异常捕获的的几种用法,之前一直使用但是没有仔细留意,调试程序的过程中发现还是有区别的,主要区别在堆栈信息的起始点不同,下边我们通过实例来看这集中不同的抛出异常的方法. 一般我们推荐 ...
- SQL-MICK基础
/*Select语句完整的执行顺序:1.from子句组装来自不同数据源的数据:2.where子句基于指定的条件对记录行进行筛选:3.group by子句将数据划分为多个分组:4.使用聚集函数进行计算: ...
- Struts 2零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...