leetcode--Two Sum

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


题意:给你一个数组和一个target,在数组中找出两个和为target的整数。

返回这两个整数的index,并且要求index1小于index2

注意,index是基于1开始的,也就是说数组的第一个index是1,而不是0

分类:数组,Hash


解法1:由于数组是无序的,容易想到的方法就是排序以后,从头尾开始找。

排序以后,我们要记录原来元素在数组中的位置,所以创建了一个类用于记录。

Java里面我们要实现自己的比较器就可以了。排序算法的时间复杂度是O(nlogn)。

排序以后从头尾开始找,如果头尾和比target大,说明尾要减小。如果比target小,说明头要增大。

直到找到为止,时间复杂度是O(n)

所以总的时间复杂度是O(nlogn)

[java]  view plain  copy
  1. public class Solution {  
  2.     public int[] twoSum(int[] nums, int target) {  
  3.         class Num{  
  4.             int pos;  
  5.             int val;  
  6.             public Num(int pos, int val) {            
  7.                 this.pos = pos;  
  8.                 this.val = val;  
  9.             }         
  10.               
  11.             @Override  
  12.             public String toString() {  
  13.                 return val+"";  
  14.             }  
  15.         };  
  16.           
  17.         List<Num> arr = new ArrayList<Num>();  
  18.         for(int i=0;i<nums.length;i++){  
  19.             arr.add(new Num(i,nums[i]));  
  20.         }         
  21.         Collections.sort(arr,new Comparator<Num>() {//先排序  
  22.             @Override  
  23.             public int compare(Num o1, Num o2) {                  
  24.                 return o1.val>o2.val?1:-1;  
  25.             }                     
  26.         });       
  27.         ArrayList<Integer> result = new ArrayList<Integer>();  
  28.         int i=0,j=nums.length-1;  
  29.         while(i<j){//从前后两个方向  
  30.             if(arr.get(i).val+arr.get(j).val>target){  
  31.                 j--;  
  32.             }else if(arr.get(i).val+arr.get(j).val<target){  
  33.                 i++;  
  34.             }else{  
  35.                 result.add(arr.get(i).pos);  
  36.                 result.add(arr.get(j).pos);  
  37.                 i++;j--;  
  38.             }  
  39.         }  
  40.         Collections.sort(result);  
  41.           
  42.         int[] r = new int[result.size()];  
  43.         for(i=0;i<r.length;i++){  
  44.             r[i] = result.get(i)+1;  
  45.         }         
  46.         return r;  
  47.     }  
  48. }  

解法二:使用HashMap来记录元素所在的位置。

遍历数组,对于当前元素cur,用target减去cur,然后判断这个差值在hashMap中能不能找到。

如果找不到,先将cur和其index存入map

接着判断下一个元素。

[java]  view plain  copy
  1. public class Solution {  
  2.     public int[] twoSum(int[] nums, int target) {  
  3.         //存储值-》位置  
  4.         HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();  
  5.         //结果,题目只要求有一组解  
  6.         int[] result = new int[2];  
  7.         for(int i=0;i<nums.length;i++){  
  8.             if(map.get(target-nums[i])==null){//如果差值不存在,就先存入  
  9.                 map.put(nums[i], i);  
  10.             }else{//如果差值存在,得出结果  
  11.                 result[0] = map.get(target-nums[i])+1;  
  12.                 result[1] = i+1;  
  13.                 break;  
  14.             }  
  15.         }     
  16.           
  17.         return result;  
  18.     }  
  19. }  


原文链接http://blog.csdn.net/crazy__chen/article/details/45441671

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值