面试:数组:twosum

  • 双指针
  • 复杂度=O(n+nlogn+n+n) = O(nlogn)。
//HelloDate.java
import java.util.*;


public class MyDemo{
     public static boolean hassum(int[] A,int target){
         boolean res=false;
         if(A==null||A.length<2)
             return res;
         Arrays.sort(A);
         int i=0,j=A.length-1;
         while(iif(A[i]+A[j]==target){
                 res=true;
                 break;
             }else if(A[i]+A[j]>target){
                 j--; 
             }else{
                 i++;
             }

         }
         return res;         
     }

     public static void main(String agrs[])
     {
          int[] A = {0,4,3,0};
           int target = 5;
           boolean result =  hassum(A, target);
           System.out.println(result);

     }

}
  • 哈希函数
  • O(n)
import java.util.HashMap;
import java.util.Scanner;

public class Solution {

    public static int[] twoSum(int[] numbers, int target) {

        //输入
        int[] res = new int[2];

        //数据结构准备
        HashMap nums = new HashMap();

        for (int i = 0; i < numbers.length; ++i) {
            // add i-th number
            Integer a = nums.get(numbers[i]);  //得到数组的下标
            if (a == null)
                nums.put(numbers[i], i);

            //算法
            // find (target - numbers[i])
            a = nums.get(target - numbers[i]);
            if (a != null && a0] = a + 1;
                res[1] = i + 1;
                break;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        //输入
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < numbers.length; ++i) {
            numbers[i] = cin.nextInt();
        }
        int target = cin.nextInt();

        //算法
        int[] res = twoSum(numbers, target);

        //输出
        System.out.println(res[0] + " " + res[1]);
    }
}


c++版本

#include
#include
#include
#include   
using namespace std;  
using namespace stdext;  



class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {

        //输入
        hash_map<int, int> rec;  
        vector<int> ret;  

        //数据结构准备
        int s = numbers.size();  
        for (int i = 0; i < s; ++i) 
            rec.insert(pair<int, int>(numbers[i], i + 1));  

        // 算法
        for (int i = 0; i < s; ++i){
            int v = numbers[i];  
            hash_map<int, int>::iterator iter = rec.find(target - v);  
                if (rec.end() != iter && iter->second <(i+1)){  //不可以重复
                int j = iter->second;  
                if (i < j) {ret.push_back(i+1); ret.push_back(j);} 
                else {ret.push_back(j); ret.push_back(i+1);} 
                return ret;  
        }  
    }  
    return ret;  
    }
};

int main(int ac, char* av[])
{

    //输入
    Solution s;
    vector<int> testTwoSum;
    int n,t;
    cin >> n;


    while(n--){
        cin>>t;
        testTwoSum.push_back(t);
    }

    int target;
    cin>>target;

    //算法
    vector<int> res=s.twoSum(testTwoSum,target);

    //输出
    for (vector<int>::iterator it=res.begin(); it!=res.end(); it++){
        cout<<*it<return 0;
}

你可能感兴趣的:(面试(java))