Java [leetcode 1] Two Sum
小二终于开通博客了,真是高兴。最近在看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
解体思路:
设立一个HashMap,其中键值key为数组中数值,对应值value则为该数组下标值。
从数组最开始往后遍历,每次求出该值对应target-numbers[i]的值在HashMap中是否存在。如果该值已经存在,那么则找到两个值的和为target值,返回即可;如果该值不存在,则把(numbers[i], i)放入HashMap中。
整个算法的时间复杂度为O(n)。
代码如下:
import java.util.*; public class Solution {
public int[] twoSum(int[] numbers, int target) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(numbers.length * 2);
int[] results = new int[2]; for(int i = 0; i < numbers.length; i++){
Integer temp1 = map.get(target - numbers[i]);
if(temp1 == null){
map.put(numbers[i], i);
}
else{
results[0] = i + 1;
results[1] = temp1 + 1;
if(results[0] > results[1]){
int temp2 = results[0];
results[0] = results[1];
results[1] = temp2;
}
return results;
}
} return null; }
}
Java [leetcode 1] Two Sum的更多相关文章
- Java [Leetcode 303]Range Sum Query - Immutable
题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- Java [Leetcode 40]Combination Sum II
题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...
- Java [Leetcode 39]Combination Sum
题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Java [Leetcode 167]Two Sum II - Input array is sorted
题目描述: Given an array of integers that is already sorted in ascending order, find two numbers such th ...
- 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 one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [LeetCode] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- jquery加入收藏代码
<html> <head> <script type="text/javascript" src="jquery-1.9.1.js" ...
- 如何深入理解 StatsD 与 Graphite ?
众所周知,StatsD 负责收集并聚合测量值.之后,它会将数据传给 Graphite,后者以时间序列为依据存储数据,并绘制图表.但是,我们不知道,基于 http 访问的图表在展示时,是基于每秒钟的请求 ...
- change Username for SVN(Subclipse) in Eclipse
Subclipse does not own the information about users and passwords (credentials), so there is no way f ...
- IOS 中的MVC设计模式
- intellij idea 12、13 win8 下 中文输入覆盖的问题(搜狗输入法或者其他输入法)
最近升级到idea12,发现中文输入存在问题,输入中文的时候会出现空格,并且覆盖后面的字符,这个问题让我很郁闷. 假设idea的安装位置为:D:\Program Files\JetBrains\Int ...
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
- 1、Web容器的理解&Tomcat的安装与配置
Web容器的理解 <Java Web开发实战经典——基础篇>一书中对Web容器这一概念阐述得很好,借用其观点对Web容器加以理解: 想要运行一个Java Web的程序,则必须有相应的Web ...
- 写Java程序要体现面向对象
对于之前写的一篇文章现在想想存在不足之处,之前写的测试ArrayList和LinkedList的各项操作性能比较的程序没有体现面向对象的封装特性,所以,今天把代码重新写了一遍,其实改动的地 ...
- [HDOJ1698]Just a Hook(线段树,区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 陈题,更新后查询所有叶节点的和.撸一遍模版,形成自己的风格. #include <algo ...
- Apache httpd + tomcat 简单集群
集群其实很简单,我们就来说一下httpd+tomcat集群都要注意哪些部分: 首先使用的东西有 apache-tomcat-8.0.32 下载地址: http://tomcat.apache ...