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 ...
随机推荐
- 2012 Asia Hangzhou Regional Contest
Friend Chains http://acm.hdu.edu.cn/showproblem.php?pid=4460 图的最远两点距离,任意选个点bfs,如果有不能到的点直接-1.然后对于所有距离 ...
- 原 Linux搭建SVN 服务器
原 Linux搭建SVN 服务器 发表于1年前(2014-08-05 17:55) 阅读(12257) | 评论(3) 31人收藏此文章, 我要收藏 赞3 摘要 Linux搭建SVN 服务器 目录 ...
- android2.3 -添加自定义按键:作唤醒功能 .
最近需要做个唤醒功能,当按键的时候android系统唤醒并点亮屏,在长按键中,系统不能在进入睡眠. 驱动方面: 1:在平台设备文件中添加 一个按键,定义为唤醒源! \arch\arm\mach-s5p ...
- Ubuntu 下使用Remmina Remote Desktop client 连接windows server输入法的问题
Ubuntu 自带的Remmina Remote Desktop 用来连接windows,vnc,ssh等非常方便好用, 但我在连接windows 2008 r2 server时遇到一个问题: ...
- HTTP/2 对 Web 性能的影响(上)
一.前言 HTTP/2 于 2015 年 5 月正式推出.自诞生以来,它就一直在影响着网络性能最佳实践.在本篇文章中,我们将讨论 HTTP/2 的二进制帧.延迟削减.潜在利弊以及相应的应对措施. 超文 ...
- SPOJ 7259 Light Switching (水题,区间01取反)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- QAQ高精度模板笔记√
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #i ...
- hdu 1005 java(System.out.println();与System.out.println(“\n”);)
//package Main; import java.util.Scanner; public class Main { static int [][] mat=new int [2][2]; st ...
- mvn 安装ojdbc6.jar
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion= - Dpackaging=jar -Dfile ...
- iOS开发--多线程
前面在<Bison眼中的iOS开发多线程是这样的(二)>一文中讲完了多线程的NSThread,不难发现这种方式的多线程实现起来非常的复杂,为了简化多线程的开发,iOS提供了GCD来实现多线 ...