取一半计算

和为S的连续正数序列

http://www.nowcoder.com/questionTerminal/c451a3fd84b64cb19485dad758a55ebe

暴力呗 取一半就行

import java.util.ArrayList;
public class Solution {
    public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) {
        ArrayList<ArrayList<Integer> > result = new ArrayList<ArrayList<Integer> >();
        if (sum<3) {
            return result;
        }
        for(int i =1;i<(sum+1)/2;i++) {
            ArrayList<Integer> temp = new ArrayList<Integer>();
            int cur = 0;
            int j = i;
            while(cur<sum) {
                temp.add(j);
                cur+=j;
                j+=1;
            }
            if(cur ==sum) {
                result.add(temp);
            }

        }
        return result;
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务