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 ...
随机推荐
- execvp使用实例
问题描述: 本程序实现模拟shell功能,用户输入命令,返回相应的结果 问题解决: 注: 以上指出了execvp函数的使用,使用时第一个参数是文件名,第二个参数是一个 ...
- ajax post 跨域
H5页面永远无法避开跨域问题-- php中, header('Access-Control-Allow-Origin:*'); 搞定. 兼容性先不管了. 来自为知笔记(Wiz)
- SDUT2241计算组合数C(n,m)(组合数)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2241 这个题的代码适应性也挺强,但这个题倒不适 ...
- POJ1046Color Me Less
http://poj.org/problem?id=1046 据说这个题是个水题,但我还是WA了好几次,最后才改对了 #include<cstdio> #include<cstrin ...
- OpenStack重启之后,dashboard登录不上去
ubuntu 12.04装好openstack之后,安装成功,终端打出如下信息: Horizon is now available at http://192.168.0.2/Keystone is ...
- 小心!#define max(a,b) a>b?a:b
今天做oj的时候,定义了两个宏: //wrong code#define max_2(a,b) a>b?a:b #define max_3(a,b,c) (a>b?a:b)>c?(a ...
- 通过一个简单的数据库操作类了解PHP链式操作的实现
class Model{ public $table; //操作的表; private $opt; //查询的参数; private $pri; //表的主键; private $lastSql; / ...
- 【PHPsocket编程专题(实战篇①)】php-socket通信演示
建立Socket连接至少需要一对套接字,其中一个运行于客户端,称为ClientSocket ,另一个运行于服务器端,称为ServerSocket . 套接字之间的连接过程分为三个步骤:服务器监听,客户 ...
- Linux资源监控命令/工具(网络)
1.手动/自动设定与启动/关闭IP参数:ifconfig,ifup,ifdown 这三个指令的用途都是在启动网络接口,不过,ifup与ifdown仅能就/etc/sysconfig/netw ...
- IOS中实现图片点击全屏预览
//// ViewController.m// XWZoomImageView//// Created by xiao on 15/11/13.// Copyright © 2015年 xiao. A ...