面试:数组: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(i<j){
             if(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<Integer, Integer> nums = new HashMap<Integer, Integer>();

        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 && a<i ) {
                res[0] = 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<iostream>
#include<vector>
#include<map>
#include <hash_map>  
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<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值