leetcode - 4Sum
生活随笔
收集整理的這篇文章主要介紹了
leetcode - 4Sum
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述:點(diǎn)擊此處
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 class Solution { 7 public: 8 vector<vector<int> > fourSum(vector<int> &num, int target) { 9 // Start typing your C/C++ solution below 10 // DO NOT write int main() function 11 vector<vector<int> > result; 12 vector<int> entry; 13 entry.assign(4,target+1); 14 sort(num.begin(),num.end()); 15 vector<int>::iterator i ,j ,k ,l; 16 for (i=num.begin();i<num.end();i++){ 17 if (i>num.begin() && *i==*(i-1)) 18 continue; 19 if (*i+*i+*i+*i>target) 20 break; 21 for (j=i+1; j<num.end();j++){ 22 if (j>i+1 && *j==*(j-1)) 23 continue; 24 if (*i+*j+*j+*j>target) 25 break; 26 for (k=j+1; k<num.end();k++){ 27 if (k>j+1 && *k == *(k-1)) 28 continue; 29 if (*i+*j+*k+*k>target) 30 break; 31 l = lower_bound(k+1,num.end(),target-*i-*j-*k); 32 if (l>=num.end()) 33 continue; 34 if (*i+*j+*k+*l == target) { 35 if (*i!=entry[0]||*j!=entry[1]||*k!=entry[2]||*l!=entry[3]){ 36 entry[0] = *i; 37 entry[1] = *j; 38 entry[2] = *k; 39 entry[3] = *l; 40 result.push_back(entry); 41 } 42 continue; 43 } 44 } 45 } 46 } 47 return result; 48 } 49 };?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhanghs/archive/2013/03/03/2941830.html
總結(jié)
以上是生活随笔為你收集整理的leetcode - 4Sum的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。