标签:-- for swap return res sequence back == 正确答案
输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
class Solution { public: vector<vector<int> > FindContinuousSequence(int sum) { vector<vector<int>> allRes; int phigh=2,plow=1; while(phigh > plow) { int cur=(phigh +plow)*(phigh-plow +1)/2; if(cur<sum) phigh++; if(cur==sum){ vector<int> res; for(int i=plow;i<=phigh;i++) res.push_back(i); allRes.push_back(res); plow++; } if(cur>sum) plow++; } return allRes; } };
typedef vector<int> vi; typedef vector<vi> vvi; class Solution { public: vvi FindContinuousSequence(int sum) { vvi res; sum <<= 1; for(int i = 2; i * i <= sum; ++i) if(sum % i == 0){ int j = sum / i, t = (j - i + 1); if(!(t & 1)){ res.push_back(vi()); vi& v = res[res.size() - 1]; t >>= 1; for(int a = t; a <= j - t; ++a) v.push_back(a); } } for(int i = 0, j = int(res.size()) - 1; i < j; ++i, --j) swap(res[i], res[j]); return res; } };
标签:-- for swap return res sequence back == 正确答案
原文地址:http://www.cnblogs.com/dd2hm/p/7403549.html