twoSum

 1 package code;
 2
 3 /* Given an array of integers, find two numbers such that they add up to a specific target number.
 4
 5 The function twoSum should return indices of the two numbers such that they add up to the target,
 6 where index1 must be less than index2. Please note that your returned answers (both index1 and index2)
 7 are not zero-based.
 8
 9 You may assume that each input would have exactly one solution.
10 Input: numbers={2, 7, 11, 15}, target=9
11 Output: index1=1, index2=2

给定一个数组,寻找在这个数组中是否存在两个数之和为某个定值。


要点:


1、一个数就是一个数,比如[2, 1],寻找和为4时,不能使用2+2。


2、需要输出位置。

3、要考虑时间复杂度,两个for循环的时间复杂度是O(N^2),为了降低时间复杂度,可以利用HashMap。

思路是循环一次,每次都判断当前数组索引位置的值在不在hashtable里,不在的话,加入进去,key为数值,value为它的索引值;在的话,取得他的key,记为n(此时n一定小于循环变量i),接下来再在h    ashtable中查找(target-当前数值)这个数,利用了hashtable中查找元素时间为常数的优势,如果找到了就结束,此处需要注意的是,如果数组中有重复的值出现,那么第二次出现时就不会加入到hasht    able里了,比如3,4,3,6;target=6时,当循环到第二个3时,也可以得到正确结果。

12 */
13
14 import java.util.HashMap;
15
16
17 public class TwoSum {
18
19     //遍历两次数组,时间复杂度为O(N^2) ,不符合要求
20     public int[] twoSum(int[] array,int target){
21         int[] result = new int[2];
22         int length = array.length;
23         out:for(int i = 0;i<length-1;i++){
24             for(int j = i+1;j<length;j++){
25                 if(array[i] + array[j] == target){
26                     result[0] = i;
27                     result[1] = j;
28                     break out;
29                 }
30
31             }
32
33             if((i == length-2) && (array[i] + array[i+1]) != target){
34                 result[0] = -1;
35                 result[1] = -1;
36             }
37         }
38         return result;
39     }
40
41     //1.时间复杂度降为O(n)
42     //2.考虑出现同样数字的情况,如果map中已经有该数字,则不add进map中。第一个相同数字的下标根据map的get函数获取,
43     //第二个相同数字的下标由i记录
44     public int[] twoSum2(int[] array,int target){
45         int[] a = new int[2];
46         HashMap<Integer,Integer> nums = new HashMap<Integer,Integer>();
47         for(int i = 0;i<array.length;i++){
48             Integer n = nums.get(array[i]);
49             if(n == null)
50                 nums.put(array[i],i);
51             n = nums.get(target-array[i]);
52             if(n!=null && n<i){
53                 a[0] = n+1;
54                 a[1] = i+1;
55                 return a;
56                 }
57         }
58         return a;
59
60     }
61 }
时间: 2024-10-28 07:11:59

twoSum的相关文章

JavaScript的two-sum问题解法

一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: 1 var twoSum = function(nums, target) { 2 var hash = {}; 3 var i; 4 for (var i = 0; i < nums.length; i++ ) { 5 if (typeof hash[nums[i]] !== "undefi

LeetCode_1 TwoSum

看书虽然有必要,但是光看书大家斗志到是没用的,但是没办法,科研项目和互联网没关,只能找点题目来刷了! 不多说,开始! LeetCode_1   TwoSum 题目说明: 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 tha

Leecode -- TwoSum

question: 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

[Lintcode two-sum]两数之和(python,双指针)

题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个指针分别从左从右开始扫描,每次判断这两个数相加是不是符合题意,如果小了,那就把左边的指针向右移,同理右指针.然后在备份的数组里找到位置. 1 class Solution: 2 """ 3 @param numbers : An array of Integer 4 @param

LeetCode——TwoSum

题目: 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 t

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

leetcode001:TwoSum(5.5)

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

LeetCode 之 TwoSum

题目: 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 t

LeetCode 1 # TwoSum

LeetCode 1 # TwoSum 很有意思的问题. Two Sum Total Accepted: 63448 Total Submissions: 350576 My Submissions Question Solution Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return in