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

题目大意:一个数组,取两个数,让他们的和是给定的的一个值,题目假定这样的值只会有一对
输入:int[]A, int target
返回:X1,X2这两个值在A中的位置x1,x2:(x1<x2)
**/

import java.util.*;

public class Solution {
    
    public int[] twoSum(int[] numbers, int target) {
        //return solve1(numbers, target);
        //return solve2(numbers, target);
        return solve3(numbers, target);
    }
    
    //================================= solve1 =====================================
    //O(n^2)
    //暴力解决,没什么特别的
    private int[] solve1(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;
    }
    
    //================================= solve2 =====================================
    //O(n * log(n))
    //对数组进行排序,然后二分查找
    private int[] solve2(int[] numbers, int target){
        int[] result = new int[2];
        
        //由于需要返回原本的index,所以需要记录原本的index
        int[] index = new int[numbers.length];
        for(int i=0; i<numbers.length; i++){
            index[i] = i+1;
        }
        
        sort(numbers, index);

        for(int i=0; i<numbers.length; i++){
            int numA = numbers[i];
            int numB = target - numA;
            
            int bIndex = Arrays.binarySearch(numbers, numB);
            
            //注意,Arrays.binarySearch返回的值不一定是-1,只要小于0就是错
            if(bIndex>=0 && bIndex!=i){
                //index存储着数字原来的位置
                int a = index[i];
                int b = index[bIndex];
                
                if(a < b){
                    result[0] = a;
                    result[1] = b;
                }else{
                    result[1] = a;
                    result[0] = b;
                }
                break;
            }
        }
        return result;
    }
    
    //数组A是真正的数据,数组B是每个数据对应的原本的位置
    //在排序的时候同时操作数组A和B,这样每个数组和他原本对应的位置可以轻松找到
    private void sort(int[] A, int[] B){
        quickSort(A, B, 0, A.length-1);
    }
    
    private void quickSort(int[] A, int[] B, int l, int r){
        if(l < r){
            int m = depart(A, B, l, r);
        
            quickSort(A, B, l, m-1);
            quickSort(A, B, m+1, r);
        }
    }
    
    private int depart(int[] A, int[] B, int l, int r){
        int point = A[r];
        int m = l;
        
        for(int i=l; i<r; i++){
            if(A[i] < point){
                swap(A, i, m);
                swap(B, i, m);
                m++;
            }
        }
        swap(A, r, m);
        swap(B, r, m);
        return m;
    }
    
    private void swap(int[] A, int i, int j){
        int tmp = A[j];
        A[j] = A[i];
        A[i] = tmp;
    }

    //================================= solve3 =====================================
    //O(n)
    //采用Map,需要使用额外的空间,但是将检索的复杂度直接降到O(1),虽然还要看HashMap本身的内部效率,但就当成O(1)好了
    private int[] solve3(int[] numbers, int target){
        int[] result = new int[2];
        Map<Integer, Integer> newNumbers = new HashMap<Integer, Integer>();
        
        //将数字及其index存入Map
        for(int i=0; i<numbers.length; i++){
            newNumbers.put(numbers[i], i+1);
        }
        
        for(int i=0; i<numbers.length; i++){
            int numA = numbers[i];
            int numB = target - numA;
            if(newNumbers.containsKey(numB)){
                int bIndex = newNumbers.get(numB);
                
                if(i+1 < bIndex){
                    result[0] = i+1;
                    result[1] = bIndex;
                }else{
                    result[1] = i+1;
                    result[0] = bIndex;
                }
                break;
            }
        }
        return result;    
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值