题解 | #和为S的连续正数序列#
和为S的连续正数序列
http://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe
class Solution {
vector<vector<int >> res;
vector<int> path;
if(sum==0||sum==1){
return res;
}
for(int i=1;i<sum-1;i++){
int count=0;
for(int j=i;j<sum;j++){
count+=j;
path.push_back(j);
if(count>sum){
path.clear();//清空path数组
break;
}
else if(count==sum){
if(path.size()>=2){
res.push_back(path);
path.clear();
break;
}
else{
path.clear();
break;
}
}
}
}
return res;
}
};
public:
//穷举无任何优化
vector<vector<int> > FindContinuousSequence(int sum) {vector<vector<int >> res;
vector<int> path;
if(sum==0||sum==1){
return res;
}
for(int i=1;i<sum-1;i++){
int count=0;
for(int j=i;j<sum;j++){
count+=j;
path.push_back(j);
if(count>sum){
path.clear();//清空path数组
break;
}
else if(count==sum){
if(path.size()>=2){
res.push_back(path);
path.clear();
break;
}
else{
path.clear();
break;
}
}
}
}
return res;
}
};