Beispiel #1
0
        int threeSumClosest(vector<int> &num, int target) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            sort(num.begin(), num.end());
            int n = num.size();

            if (threeSum(num, n, target)) return target;
            int t = 1;
            while (1)
            {
                if (threeSum(num, n, target+t)) return target+t;
                if (threeSum(num, n, target-t)) return target-t;
                t++;
            }
        }
Beispiel #2
0
 vector<vector<int> > fourSum(vector<int> &num, int target) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     vector<vector<int> > res;
     int n = num.size();
     if (n < 4) return res;
     sort(num.begin(), num.end());
     
     
     
     for (int i = 0; i < n; i++) {
         if (i > 0 && num[i] == num[i - 1]) {
             continue;
         }
         int ntarget = target - num[i]; 
         vector<int> v(num.begin() + i + 1, num.end());
         
         vector<vector<int> > threeRes = threeSum(v, ntarget);
         for (int j = 0; j < threeRes.size(); j++) {
             threeRes[j].insert(threeRes[j].begin(), num[i]);
             res.push_back(threeRes[j]);
         }
     }
     
     return res;
 }
Beispiel #3
0
 vector<vector<int>> fourSum(vector<int>& nums, int target) {
     vector<vector<int>> res;
     int len = nums.size();
     if(len <4)
         return res;
         
     sort(nums.begin(), nums.end());
     vector<vector<int>> thr_ans;
     for(int i=0; i<len-3; i++)
     {
         if(i >0 && nums[i] == nums[i-1])
             continue;
             
         vector<int> temp(nums.begin()+i+1, nums.end());
         thr_ans = threeSum(temp, target-nums[i]);
         int ans_len = thr_ans.size();
         for(int j=0; j<ans_len; j++)
             thr_ans[j].insert(thr_ans[j].begin(), nums[i]);
         res.insert(res.end(), thr_ans.begin(), thr_ans.end());
     }
     
     return res;
     
     
 }
Beispiel #4
0
std::vector<std::vector<int> > Algo::fourSum(std::vector<int> &num, int target)
{
  std::vector<std::vector<int> > res;

  if( num.size()<4 )
    return res;

  std::sort(num.begin(), num.end());

  for(unsigned i=num.size()-1; i>=3; i--)
  {

    if( i<num.size()-1 && num[i]==num[i+1] )
      continue;
           
    int threeTarget = target - num[i];

    std::vector<std::vector<int> > threeRes = threeSum(num, i, threeTarget);

    for(unsigned j=0; j<threeRes.size(); j++)
    {
      threeRes[j].push_back(num[i]);

      res.push_back(threeRes[j]);
    }
  }

  return res;

}
Beispiel #5
0
int main(int argc, char const *argv[]) {
	int nums[7] = {1, 2, 0, -1, -2, 4, -2};
	int *returenSize = malloc(sizeof(int));
	int **result = threeSum(nums, 7, returenSize);
	for (int i = 0; i < *returenSize; ++i) {
		printf("%d %d %d\n", result[i][0], result[i][1], result[i][2]);
	}
	return 0;
}
Beispiel #6
0
	void test() {
		vector<int> num = { -1, 0, 1, 2, -1, -4 };
		vector<vector<int>> result = threeSum(num);
		for (auto v : result) {
			for (int x : v) {
				cout << x << ' ';
			}
			cout << endl;
		}
	}
Beispiel #7
0
 vector<vector<int> > fourSum(vector<int> &num, int target) {
     const int n = num.size();
     
     std::sort(num.begin(), num.end());  // sort first!!!
     
     for (int i = 0; i < n-3; ++i)
     {
         if ((i > 0) && (num[i] == num[i-1]))
             continue;
         threeSum(num, i, target);
     }
     return vvnums;
 }
Beispiel #8
0
std::vector<std::vector<int> > Algo::threeSum(std::vector<int> &num, int target)
{
  std::vector<std::vector<int> > res;

  if( num.size()<3 )
    return res;

  std::sort(num.begin(), num.end());

  res = threeSum(num, num.size(), target);

  return res;
}
Beispiel #9
0
 vector<vector<int>> fourSum(vector<int>& nums, int target) {
     vector<vector<int>> ans;
     if (nums.size()==0)
         return ans;
     sort(nums.begin(), nums.end());
     for (int i=3; i<nums.size(); ++i) {
         vector<vector<int>> triplet = threeSum(nums, i, target-nums[i]);
         for (int j=0; j<triplet.size(); ++j) {
             triplet[j].push_back(nums[i]);
             if (!haveExisted(triplet[j], ans))
                 ans.push_back(triplet[j]);
         }
     }
     return ans;
 }
Beispiel #10
0
void ArrayQuiz::test15() {
    cout << "LeetCode 15   3Sum  16.9%  Medium" << endl;
    vector<int> nums = {-1, 0, 1, 2, -1, -4};
    for_each(nums.begin(), nums.end(), [&](int u) { cout << u << " "; });
    cout << endl;
    auto result = threeSum(nums);

    for (auto i : result) {
        for (auto j : i) {
            cout << j << " ";
        }
        cout << endl;
    }
    cout << result.size() << endl;
}
Beispiel #11
0
main()
{
	int i;
	int arr[] = { -2, 0, 1, 1, 2, };
	threeSum(arr, 5, &i);
}
Beispiel #12
0
int main(){
    int a[]={-1,0,1,2,5,-1,-4};
    int size=sizeof(a)/sizeof(a[0]);
    threeSum(a,size,0);
    return 0;
}