面试:数组:twosum

来源:互联网 发布:淘宝申请企业店铺条件 编辑:程序博客网 时间:2024/04/28 07:35
  • 双指针
  • 复杂度=O(n+nlogn+n+n) = O(nlogn)。
//HelloDate.javaimport 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