vector<vector<int> > fourSum(vector<int> &num, int target) { // wyuan; 10/15/2014; This is very similar with "Combination Sum II" sort(num.begin(), num.end()); // !!! So careless. Not 0, but target! // return kSum(num, 0, 4, 0); return kSum(num, 0, 4, target); // use the same code with 3Sum. }
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; }
vector<vector<int> > fourSum(vector<int> &num, int target) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vv.clear(); sort(num.begin(), num.end()); vector<int> tmp; kSum(num, target, 4, 0, tmp); return vv; }
moDMatrix<Real> moDMatrix<Real>::operator+ (const moDMatrix& rkM) const { moDMatrix<Real> kSum(rkM.m_iRows,rkM.m_iCols); for (int i = 0; i < m_iQuantity; i++) { kSum.m_afData[i] = m_afData[i] + rkM.m_afData[i]; } return kSum; }
PolyBase PolyBase::operator/(const double V) const /** PolyBase division @param V :: Value division @return (*this/V); */ { PolyBase kSum(*this); return kSum /= V; }
PolyBase PolyBase::operator*(const double V) const /** PolyBase multiplication @param V :: Value multiplication @return (*this*V); */ { PolyBase kSum(*this); return kSum *= V; }
PolyBase PolyBase::operator-(const double V) const /** PolyBase substractr @param V :: Value substract @return (*this-V); */ { PolyBase kSum(*this); return kSum -= V; }
PolyBase PolyBase::operator+(const double V) const /** PolyBase addition @param V :: Value Addtion @return (*this+V); */ { PolyBase kSum(*this); return kSum += V; }
PolyBase PolyBase::operator*(const PolyBase &A) const /** PolyBase multiplication @param A :: PolyBase multiplication @return (*this*A); */ { PolyBase kSum(*this); return kSum *= A; }
PolyBase PolyBase::operator-(const PolyBase &A) const /** PolyBase subtraction @param A :: PolyBase addition @return (*this-A); */ { PolyBase kSum(*this); return kSum -= A; }
PolyBase PolyBase::operator+(const PolyBase &A) const /** PolyBase addition @param A :: PolyBase addition @return (*this+A); */ { PolyBase kSum(*this); return kSum += A; }
void kSum(vector<int> &a, int target, int k, int index, vector<int> &helper) { int sz = a.size(); if(k > sz - index) return; if(k == 1) { int pos = binarySearch(a, index, sz - 1, target); if(pos != -1) { helper.push_back(a[pos]); vv.push_back(helper); helper.pop_back(); } return; } for(int i = index; i <= sz - k; ++i) { if(i != index && a[i] == a[i-1]) continue; helper.push_back(a[i]); kSum(a, target - a[i], k - 1, i + 1, helper); helper.pop_back(); } }