LeetCode题解—TwoSum

首先先看下题目吧

Two Sum

  Total Accepted: 54230  Total Submissions: 297756 My Submissions

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

按照国际惯例,第一种解法一点是最简单的。也是失败的解法。两个循环的暴力查找法。

public class Solution {
	public int[] twoSum(int[] numbers, int target) {
		int[] result=new int[2];
		for(int i=0;i<numbers.length;i++){
			for(int j=i+1;j<numbers.length;j++){
				if(numbers[i]+numbers[j]==target){
					result[0]=i+1;
					result[1]=j+1;
					return result;
				}
			}
		}
		return result;
	}
}
下面说一下第二种解法,利用hashmap的快速查找的特点,将数组中的数先迁移到hashmap中,在迁移的过程中判断是否有两个值的和与target相等

public class Solution {
	public int[] twoSum(int[] numbers, int target) {
		int[] result=new int[2];
		HashMap<Integer, Integer> tempNums=new HashMap<Integer, Integer>();
		for(int i=0;i<numbers.length;i++){
			Integer n=tempNums.get(numbers[i]);
			if(n==null){
				tempNums.put(numbers[i], i);
			}
			n=tempNums.get(target-numbers[i]);
			if(n!=null&&n<i){
				result[0]=n+1;
				result[1]=i+1;
				return result;
			}
		}
		return result;
	}
}
第二种方法是可以通过的,我当时的想出的第二种解决方法也是用hashmap,但是我那会把数组迁移和目标数查找分开来做了,也就是第一次遍历只是将数组中的数转移到hashmap中,第二次遍历才开始查找,so结果也是一样 超时了!!!
在两种方式失败后,我又想到了排序和二分查找,所以写下了下面冗长而且仍然超时的方法。我是不会计算算法复杂度的,但我想至少比第一中方法好吧。
public class Solution {
	public int[] twoSum(int[] numbers, int target) {
		int[] result=new int[2];
		int[][] tempNums=new int[numbers.length][2];
		for(int i=0;i<numbers.length;i++){
			tempNums[i][0]=numbers[i];
			tempNums[i][1]=i;
		}
		mSort(tempNums);
		for(int i=0;i<tempNums.length;i++){
			int n=target-tempNums[i][0];
			int low=i+1;
			int high=tempNums.length-1;
			while(low<=high){
				int mid=((low+high)>>1);
				if(n==tempNums[mid][0]){
					result[0]=tempNums[i][1]+1;
					result[1]=tempNums[mid][1]+1;
					return result;
				}
				else if(n<tempNums[mid][0]){
					high=mid-1;
				}
				else{
					low=mid+1;
				}
			}
		}
		return result;	
	}

	private void mSort(int[][] tempNums) {
		// TODO Auto-generated method stub
		mergeSort(tempNums,0,tempNums.length-1);
	}

	private void mergeSort(int[][] tempNums, int low, int high) {
		// TODO Auto-generated method stub
		if(low<high){
			int mid=((low+high)>>1);
			mergeSort(tempNums, low, mid);
			mergeSort(tempNums, mid+1, high);
			merge(tempNums, low,mid, high);
		}
	}

	private void merge(int[][] tempNums, int low,int mid, int high) {
		// TODO Auto-generated method stub
		int i=low,j=mid+1;
		int m=mid,n=high;
		int k=0;
		int[][] temp=new int[high-low+1][2];
		
		while(i<=m&&j<=n){
			if(tempNums[i][0]<=tempNums[j][0]){
				temp[k++]=tempNums[i++];
			}
			else{
				temp[k++]=tempNums[j++];
			}
		}
		while(i<=m){
			temp[k++]=tempNums[i++];
		}
		while(j<=n){
			temp[k++]=tempNums[j++];
		}
		
		for(int index=0;index<temp.length;index++){
			tempNums[low++]=temp[index];
		}
	}
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值