博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Two Sum leetcode java
阅读量:4332 次
发布时间:2019-06-06

本文共 2662 字,大约阅读时间需要 8 分钟。

题目:

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

 

题解:

这道题也是比较经典处理数组的,有以下两个思路。

 

思路1

 利用HashMap,把target-numbers[i]的值放入hashmap中,value存index。遍历数组时,检查hashmap中是否已经存能和自己加一起等于target的值存在,存在的话把index取出,连同自己的index也出去,加1(index要求从1开始)后存入结果数组中返回。如果不存在的话,把自己的值和index存入hashmap中继续遍历。由于只是一遍遍历数组,时间复杂度为O(n)。

代码如下:

 1     
public 
int[] twoSum(
int[] numbers, 
int target) {
 2         
int [] res = 
new 
int[2];
 3         
if(numbers==
null||numbers.length<2)
 4             
return res;
 5         HashMap<Integer,Integer> map = 
new HashMap<Integer,Integer>();
 6         
for(
int i = 0; i < numbers.length; i++){
 7             
if(!map.containsKey(target-numbers[i])){
 8                 map.put(numbers[i],i);
 9             }
else{
10                 res[0]= map.get(target-numbers[i])+1;
11                 res[1]= i+1;
12                 
break;
13             }
14         }
15         
return res;
16     }

 

思路2

又碰到敏感的关键字array和target,自然而然想到二分查找法。但是这道题不能像传统二分查找法那样舍弃一半在另外一半查找,需要一点点挪low和high指针,所以时间复杂度为O(n)。

首先先将整个list拷贝并排序,使用Arrays.Sort()函数,时间复杂度O(nlogn)

然后利用二分查找法,判断target和numbers[low]+numbers[high]。

target == numbers[low]+numbers[high], 记录copy[low]和copy[high];

target > numbers[low]+numbers[high],说明最大的和最小的加一起还小于target,所以小值要取大一点,即low++;

target > numbers[low]+numbers[high], 说明最大的和最小的加一起大于target,那么大值需要往下取一点,即high--。

再把找到的两个合格值在原list中找到对应的index,返回即可。

总共的时间复杂度为O(n+nlogn+n+n) = O(nlogn)。

代码如下:

 1     
public 
int[] twoSum(
int[] numbers, 
int target) {
 2         
int [] res = 
new 
int[2];
 3         
if(numbers==
null||numbers.length<2)
 4             
return res;
 5         
 6         
//
copy original list and sort
 7 
        
int[] copylist = 
new 
int[numbers.length];  
 8         System.arraycopy(numbers, 0, copylist, 0, numbers.length);  
 9         Arrays.sort(copylist);    
10         
11         
int low = 0;
12         
int high = copylist.length-1;
13         
14         
while(low<high){
15             
if(copylist[low]+copylist[high]<target)
16                 low++;
17             
else 
if(copylist[low]+copylist[high]>target)
18                 high--;
19             
else{
20                 res[0]=copylist[low];
21                 res[1]=copylist[high];
22                 
break;
23             }
24         }
25         
26         
//
find index from original list
27 
        
int index1 = -1, index2 = -1;  
28         
for(
int i = 0; i < numbers.length; i++){  
29             
if(numbers[i] == res[0]&&index1==-1)
30                 index1 = i+1;
31             
else 
if(numbers[i] == res[1]&&index2==-1)
32                 index2 = i+1;
33        } 
34         res[0] = index1;
35         res[1] = index2;
36         Arrays.sort(res);
37         
return res;
38     }

Reference:

http://blog.csdn.net/linhuanmars/article/details/19711387

http://blog.csdn.net/likecool21/article/details/10504885

转载于:https://www.cnblogs.com/springfor/p/3859618.html

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>
crash 收集
查看>>
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>