Facebook Phone Interview: 3, 4 Sum (Easy)

本文探讨了经典的3Sum和4Sum问题解决方案,通过排序和双指针技巧避免重复组合,实现高效查找数组中三个或四个数相加等于目标值的元素组合。此外,还讨论了使用哈希表解决两数之和问题,并提到了浮点数在哈希表应用中的局限性。

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...

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值