首页 > 其他 > 详细

829. Consecutive Numbers Sum

时间:2020-09-17 23:35:43      阅读:63      评论:0      收藏:0      [点我收藏+]

Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers?

Example 1:

Input: 5
Output: 2
Explanation: 5 = 5 = 2 + 3

Example 2:

Input: 9
Output: 3
Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4

Example 3:

Input: 15
Output: 4
Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5

Note: 1 <= N <= 10 ^ 9.

class Solution {
    public int consecutiveNumbersSum(int N) {
        int res = 0;
        for(int i = 1; (i - 1) * i / 2 < N; i++) {
            if((N - (i - 1) * i / 2) % i == 0) res++;
        }
        return res;
    }
}

技术分享图片

 

 exo me?

829. Consecutive Numbers Sum

原文:https://www.cnblogs.com/wentiliangkaihua/p/13688415.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!