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插件手把手教会(二)
上次我们将到了简单的jQuery插件,这次我们继续: 面向对象的插件开发 为什么要有面向对象的思维,因为如果不这样,你可能需要一个方法的时候就去定义一个function,当需要另外一个方法的时候,再去 ...
- [nowCoder] 局部最小值位置
定义局部最小的概念.arr长度为1时,arr[0]是局部最小.arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]是局部最小:如果arr[N-1]<arr[ ...
- 翻译:AngularJS应用的认证技术
原文: https://medium.com/opinionated-angularjs/7bbf0346acec 认证 最常用的表单认证就是用户名(或者邮件)和密码登录.这就表示要实现一个用户可以输 ...
- Cent Os 常用操作
开放端口 编辑iptables文件(/etc/sysconfig/iptables) -A INPUT -m state --state NEW -m tcp -p tcp --dport xx端口号 ...
- 如何恢复SQL Server 中的Master库
如何恢复SQL Server 2005中的Master库 2011-05-10 16:34 Vegas Lee 博客园 我要评论(0) 字号:T | T master库对于SQLServer来说, ...
- java基础知识回顾之java Thread类学习(五)--java多线程安全问题(锁)同步的前提
这里举个例子讲解,同步synchronized在什么地方加,以及同步的前提: * 1.必须要有两个以上的线程,才需要同步. * 2.必须是多个线程使用同一个锁. * 3.必须保证同步中只能有一个线程在 ...
- SDUT1500 Message Flood
以前做过的用的字典树,可是貌似现在再用超内存....求解释... 问了LYN用的map函数做的,又去小小的学了map函数.... http://wenku.baidu.com/view/0b08cec ...
- java.util.concurrent包API学习笔记
newFixedThreadPool 创建一个固定大小的线程池. shutdown():用于关闭启动线程,如果不调用该语句,jvm不会关闭. awaitTermination():用于等待子线程结束, ...
- 130701基础练习-first
// 629.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"//#include <iostream.h> class Point ...
- Project Euler 81:Path sum: two ways 路径和:两个方向
Path sum: two ways In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom ...