Facebook Phone Interview: 3, 4 Sum (Easy)

Facebook really usually has a high frequency of posting repetitive problems.偷笑

This one is the typical 3Sum, 4Sum problem.

Given an input array and a target value, find whether there are three/four values in the array which sum to the target. target value can be 0.

The input array may contains repetitive numbers.

// Typical problems.
// find 3 values in the input array which sum to the target. If not exist, return NULL.
vector< vector<int> > findThreeSum(vector<int> array, int target) {
    vector< vector<int> > res;
    if(array.size() < 3) return res; // if the input array has less than 3 values, just return empty vector.
    sort(array.begin(), array.end()); // need to sort this array first to avoid repeat combinations.
    for(int i = 0; i < array.size(); ++i) {
        if(i > 0 && array[i] == array[i-1]) continue; // if array[i] == array[i+1], skip the (i+1)th number.
        int start = i + 1;
        int end = array.size() - 1;
        while(start  < end) {
            if(start - 1 > i && (array[start] == array[start + 1])) {
                start++;
                continue;  // kick out the repetitive values.
            }
            if(end + 1 < array.size() && (array[end] == array[end + 1])) {
                end--;
                continue;  // same here.
            }
            int sum = array[i] + array[start] + array[end];
            if(sum == target) {
                vector<int> back;
                back.push_back(array[i]);
                back.push_back(array[start]);
                back.push_back(array[end]);
                res.push_back(res);
            } else if(sum < target) {
                start++;
            } else end--;
        }
    }
    return res;
}

Sort algorithm takes o(NlgN), Final Time Complexity is O(n^2)

To be continue: this problem can use HashTable to solve as well.

Two Sum, solve it using hashtable

#include <vector>
#include <iostream>
#include <set>
using namespace std;

bool twoSum(vector<int>& nums, int target) {
  set<int> maps;
  for(int i = 0; i < nums.size(); ++i) {
    if(maps.find(target - nums[i]) == maps.end()) {
      maps.insert(nums[i]);
    } else return true;
  }
  return false;
}

int main(void) {
  vector<int> nums {0, 1, 2};
  cout << twoSum(nums, 1) << endl;
}

However, the hash table one will be problematic once the input number are float.

Because hash table is used for fast loopup for exact numbers and most floats are the result of caculations that produce a float which is only an approximation to the correct number. Normally, because of rounding and inherent limitations of floating point arithmetic, if you expect that floating point number a and b should be equal to each other because math says so, you need to pick some relatively small delta > 0...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值