TEST(TwoSumTestCase, Normal) { int a[] = {2, 7, 11, 15}; std::vector<int> numbers(a, a+4); std::vector<int> result = twoSum(numbers, 9); EXPECT_EQ(1, result[0]); EXPECT_EQ(2, result[1]); //------------------------- int b[] = {1, 4, 2, 23, 11, 8, 62, 34, 23, 9, 10}; std::vector<int> numbers_2(b, b+11); std::vector<int> result_2 = twoSum(numbers_2, 17); EXPECT_EQ(6, result_2[0]); EXPECT_EQ(10, result_2[1]); //------------------------- int c[] = {0, 4, 3, 0}; std::vector<int> numbers_3(c, c+4); std::vector<int> result_3 = twoSum(numbers_3, 0); EXPECT_EQ(1, result_3[0]); EXPECT_EQ(4, result_3[1]); }
int main(void) { int i; int Input[] = {2, 7, 11, 15}; int target = 9; //int Input[] = {230,863,916,585,981,404,316,785,88,12,70,435,384,778,887,755,740,337,86,92,325,422,815,650,920,125,277,336,221,847,168,23,677,61,400,136,874,363,394,199,863,997,794,587,124,321,212,957,764,173,314,422,927,783,930,282,306,506,44,926,691,568,68,730,933,737,531,180,414,751,28,546,60,371,493,370,527,387,43,541,13,457,328,227,652,365,430,803,59,858,538,427,583,368,375,173,809,896,370,789,542}; //int target = 542; //int Input[] = {-10,-1,-18,-19}; //int target = -19; int InputLength = sizeof(Input)/sizeof(int); #ifdef DEBUG printf("Input:\n"); for (i=0;i<InputLength;i++) printf("%d ", Input[i]); printf("\n"); #endif //printf("Input[28]:%d Input[45]:%d\n", Input[28], Input[45]); int * returned_index = twoSum(Input, InputLength, target); #ifdef DEBUG printf("The return indices are: %d and %d\n", returned_index[0], returned_index[1]); #endif return 0; }
int main(int argc, const char * argv[]) { int numbers[] = {3, 3, 4, 5}; int target = 9; int *indice = twoSum(numbers, sizeof(numbers) / sizeof(int), target); printf("index1=%d, index2=%d\n", indice[0], indice[1]); return 0; }
vector<vector<int> > kSum(const vector<int> &num, int begin, int count, int target) { vector<vector<int> > res; if(count < 2) return res; set<int> visited; if(count == 2) { return twoSum(num, begin, target); } else { int size = num.size(); for(int i = begin; i<size; i++) { if(visited.find(num[i]) != visited.end()) continue; visited.insert(num[i]); vector<vector<int> > subRes = kSum(num, i+1, count-1, target-num[i]); if(!subRes.empty()) {// we can pass num[i] to kSum, instead of inserting it here. int subSize = subRes.size(); for(int j=0; j < subSize; j++) { subRes[j].insert(subRes[j].begin(), num[i]); } res.insert(res.end(), subRes.begin(), subRes.end()); } } } return res; }
int main(int argc, char *argv[]) { int target, *arr, i; int *indexes; if (argc < 2) { fprintf(stderr, "usage: ./a.out TARGET NUMBER NUMBER [NUMBER ...]\n"); exit(1); } NEXT_ARG; target = atoi(argv[0]); NEXT_ARG; arr = malloc(argc * sizeof(int)); if (!arr) exit(1); for (i = 0; i < argc; i++) arr[i] = atoi(argv[i]); indexes = twoSum(arr, argc, target); if (!indexes) fprintf(stderr, "fail to twoSum\n"); else { fprintf(stderr, "indexes: %d %d\n", indexes[0], indexes[1]); free(indexes); } free(arr); exit(0); }
std::vector<std::vector<int> > Algo::threeSum(std::vector<int> &num, int len, int target) { std::vector<std::vector<int> > res; assert( len<=num.size() ); if( len<3 ) return res; for(unsigned i=len-1; i>=2; i--) { if( i<len-1 && num[i]==num[i+1] ) continue; int twoTarget = target - num[i]; std::vector<std::vector<int> > twoRes = twoSum(num, i, twoTarget); for(unsigned j=0; j<twoRes.size(); j++) { twoRes[j].push_back(num[i]); res.push_back(twoRes[j]); } } return res; }
int main(void) { int nums[] = {0, 4, 3, 0}; int *idx = twoSum(nums, 4, 0); printf("%d %d\n", idx[0], idx[1]); free(idx); return 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>> result; vector<twoSum> twosums; for (int i = 0; i < num.size(); i++) { for (int j = i + 1; j < num.size(); j++) twosums.push_back(twoSum(i, j, num[i]+num[j])); } sort(twosums.begin(), twosums.end()); int n = twosums.size(); for (int i = 0; i < n; i++) { int value = target - twosums[i].val; int lf = find_lower_bound(twosums, i + 1, n - 1, value); int rt = find_upper_bound(twosums, i + 1, n - 1, value); for (int j = lf; j <= rt; j++) { if (twosums[i].lf == twosums[j].lf) continue; if (twosums[i].rt == twosums[j].lf) continue; if (twosums[i].lf == twosums[j].rt) continue; if (twosums[i].rt == twosums[j].rt) continue; vector<int> four; four.push_back(num[twosums[i].lf]); four.push_back(num[twosums[i].rt]); four.push_back(num[twosums[j].lf]); four.push_back(num[twosums[j].rt]); sort(four.begin(), four.end()); if (find(result.begin(), result.end(), four) == result.end()) result.push_back(four); } } return move(result); }
int main() { int n = 4; int target = 9; int numbers[4] = {2, 7, 11, 15}; int *ans = twoSum(numbers, n, target); printf("index1=%d, index2=%d", ans[0], ans[1]); return 0; }
void Solution::SolveTwoSum() { //vector<int> sum = { 0, 4, 3, 0 }; vector<int> sum = { 3, 2, 4 }; vector<int> nums = twoSum(sum, 6); for (int i = 0; i < nums.size(); ++i) { cout << nums[i]; } cout << endl; }
int main(void){ int test[] = {4,2,3}; int* result = twoSum(test, 3, 6); printf("[%d,%d]\n", *result, *(result+1)); return 0; }
int main() { int sets[] = {2, 7, 11, 15}; int target = 9; int *res = twoSum(sets, sizeof(sets)/sizeof(int), target); printf("res[0] is %d\n", res[0]); printf("res[1] is %d\n", res[1]); }
int main(){ int array[4] = {2,7,11,15}; int *index = twoSum(array, 4, 9); printf("%d, %d \n", index[0], index[1]); return 0; }
int main(int argc, char** argv) { int test[3] = {3,2,4}; int target = atoi(argv[1]); int *two = twoSum(test, 10, target); if (two != NULL) printf("%d, %d\n", *two, *(two+1)); return 0; }
void threeSum(vector<int> &num, int i1, int target) { for (int i = i1+1; i < num.size()-2; ++i) { if ((i > i1+1) && (num[i] == num[i-1])) continue; twoSum(num, i1, i, target); } return; }
int main(){ int* array ; int numbers[]={-1,-2,-3,-4,-5} ; int target = -8 ; int returnSize ; array = twoSum(numbers,5,target,&returnSize) ; printf("%d\n",array[0]) ; printf("%d\n",array[1]) ; return 0 ; }
vector<vector<int>> threeSum(vector<int> & nums){ int n = nums.size(); vector<vector<int>> triples; sort(nums.begin(), nums.end()); for (int i = 0; i < n - 2; ++i){ if(i > 0 && nums[i] == nums[i - 1]) continue; twoSum(nums, i + 1, 0 - nums[i], triples); } return triples; }
vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> threeSumVec; std::sort(nums.begin(), nums.end()); int i = 0; while (i + 2 < nums.size()) { twoSum(nums, i++, threeSumVec); while (i < nums.size() - 2 && nums[i] == nums[i-1]) i++; } return threeSumVec; }
int main(){ int nums[5] = {1, 2, 3, 4, 5}; int start, end int *p; start = clock(); p = twoSum(nums, 16204, 16021); end = clock(); printf("ÓÃʱ:%d", end - start); getchar(); return 0; }
int main() { int nums[] = {0, 4, 3, 0}; int *retList = NULL; retList = twoSum(nums, 4, 0); printf("[%d, %d]\n", retList[0], retList[1]); free(retList); }
vector<vector<int> > threeSum(vector<int> &num, int target) { int n = num.size(); vector<vector<int> > res; for(int i = 0; i < n-2; i++) { if(i > 0 && num[i] == num[i-1])continue;//重复的元素不用计算 int target2 = target - num[i]; twoSum(num, i+1, target2, res); } return res; }
void ArrayQuiz::test1() { cout << "LeetCode 1 Two Sum 17.6% Medium\n"; vector<int> numbers{0, 2, 7, 9, 11, 15}; cout << "input = \n"; for_each(numbers.cbegin(), numbers.cend(), [&](const int i) { cout << i << " "; }); cout << endl; int target = 18; cout << "target = " << target << endl; vector<int> result = twoSum(numbers, target); for_each(result.cbegin(), result.cend(), [&](const int i) { cout << i << " "; }); cout << endl; }
int main() { int k[688] = {438,507,629,255,813,423,536,428,340,767,208,808,882,142,835,423,331,545,627,750,397,675,662,92,465,627,15,522,395,727,561,73,570,606,826,651,743,214,881,685,820,326,653,334,698,604,938,260,51,597,291,855,427,117,943,142,166,439,833,4,180,10,531,350,785,989,607,303,554,764,769,451,654,752,15,90,505,159,1,516,801,938,442,543,761,548,523,766,445,696,217,352,333,383,868,764,556,943,280,140,627,870,635,753,743,978,611,326,405,143,564,256,304,913,570,331,340,222,952,959,535,113,148,125,874,354,984,753,423,448,235,621,796,355,64,682,326,500,609,293,566,974,808,568,729,173,735,764,987,588,227,961,621,340,245,570,640,814,635,482,520,563,695,399,95,92,813,135,342,513,410,943,64,458,801,835,977,932,838,604,500,266,395,108,788,161,769,662,697,167,143,383,880,19,758,552,396,226,548,560,916,766,568,192,100,734,639,288,187,465,345,535,293,130,488,172,108,313,800,662,644,758,843,953,73,543,630,37,711,372,372,410,60,973,750,833,827,572,31,157,473,529,410,650,930,635,253,203,159,431,848,969,982,229,504,617,273,723,854,572,879,382,18,418,990,321,871,544,662,900,340,358,788,998,376,87,520,543,717,67,713,419,108,796,143,342,896,526,926,362,23,63,426,514,526,499,857,87,499,635,496,69,558,635,1,635,125,611,459,11,14,305,969,225,677,354,90,509,317,895,617,798,210,830,63,488,139,500,744,564,295,782,214,569,863,835,14,70,687,321,130,933,822,761,985,637,995,604,932,54,51,170,951,774,117,117,863,940,599,820,473,350,572,776,198,958,1,281,539,874,38,205,591,901,588,743,926,270,675,135,208,588,438,444,752,172,709,191,199,494,743,854,362,340,170,909,434,258,621,983,119,37,281,987,53,486,709,824,817,590,632,798,207,691,592,358,92,316,864,345,488,408,172,392,402,188,880,336,350,739,301,953,314,414,261,701,431,984,350,800,476,370,952,580,585,335,14,486,448,710,637,134,665,166,631,526,877,940,492,855,563,417,517,543,496,979,802,100,100,854,706,350,796,140,46,434,811,543,743,614,838,803,583,280,748,124,570,134,18,282,879,324,142,904,861,884,974,630,319,340,130,954,130,199,681,465,295,597,492,893,552,920,370,221,293,26,632,695,967,372,321,723,907,701,733,520,482,927,11,606,466,404,529,402,166,646,60,802,101,822,93,12,336,136,650,539,640,791,108,997,268,833,562,132,423,879,884,362,374,522,188,496,472,531,434,942,46,528,423,504,664,867,467,685,899,884,746,548,937,296,436,295,486,722,288,465,89,736,900,174,497,37,84,817,411,486,387,949,893,187,776,771,617,175,516,705,296,452,268,6,922,402,991,723,609,98,347,588,554,80,169,197,931,19,597,888,164,347,27,916,378,630,321,644,156,362,4,705,164,465,543,806,282,827,448,670,950,458,388,239,744,507,516,414,459,934,130,924,440,166,788,343,379,884,758,808,912,187,706,419,88,355,996,331,777,500,863,796,627,124,535,561,466,370,101,383,813,44}; int *p = twoSum(k, 688, 929); if (!p) { printf("F**K\n"); return 1; } printf("%d, %d\n", p[0], p[1]); free(p); return 0; }
vector<vector<int>> threeSum(vector<int>& nums, int end, int target) { vector<vector<int>> ans; for (int i=2; i<end; ++i) { vector<vector<int>> pairs = twoSum(nums, i, target-nums[i]); for (int j=0; j<pairs.size(); ++j) { pairs[j].push_back(nums[i]); if (!haveExisted(pairs[j], ans)) ans.push_back(pairs[j]); } } return ans; }
std::vector<std::vector<int> > Algo::twoSum(std::vector<int> &num, int target) { std::vector<std::vector<int> > res; if( num.size()<2 ) return res; std::sort(num.begin(), num.end()); res = twoSum(num, num.size(), target); return res; }
int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(), nums.end()); int size = nums.size(); if (size < 3) { return 0; } int sum = nums[0] + nums[1] + nums[2]; for (int i = 0; i < size; i++) { while (i > 0 && nums[i] == nums[i - 1]) ++i; twoSum(i, target, sum, size, nums); } return sum; }
int main() { int *nums; nums=(int *)malloc(sizeof(int ) * 4); *(nums)=-3; *(nums+1)=4; *(nums+2)=3; *(nums+3)=90; int *ret; ret=(int *)malloc(sizeof(int ) *2); ret=twoSum(nums,4,0); printf("the index is %d,%d,\n",*(ret),*(ret+1)); }
vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int>> ans; if (num.size() < 3) return ans; sort(num.begin(), num.end()); int lastRepeat = num[0]; for (size_t i = 0; i < num.size(); ++i){ if (i > 0 && num[i] == lastRepeat) continue; lastRepeat = num[i]; int x = 0, y = 0; twoSum(ans, num, i + 1, num.size() - 1); } return ans; }
//上面的代码虽然能够正常运行,但是在leetcode中不能提交,报“超时”错误。 int main(void){ int len; int target; while(scanf("%d %d",&len,&target)!=EOF&&len>1){ int *nums=(int *)malloc(len*sizeof(int)); if(nums==NULL){ exit(EXIT_FAILURE); } for(int i=0;i<len;i++){ scanf("%d",nums+i); } int *result=twoSum(nums, len, target); printf("%d , %d\n",result[0],result[1]); } }
vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int> >res; set<vector<int> > pool; sort(num.begin(), num.begin()+num.size()); for(int i =0; i<num.size(); ++i){ vector<vector<int> >temp = twoSum(num, -num[i], i); for(int j=0; j<temp.size(); ++j){ if(pool.find(temp[j])==pool.end()){ pool.insert(temp[j]); res.push_back(temp[j]); } } while(num[i]==num[i+1]) i++; } return res; }