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.

这道题给了一个正整数N,问N能写成多少种连续正整数之和,比如9可以写成 4+5,或者 2+3+4。这道题其实不好做,因为没有固定的算法可以套,而更多的考察是数学知识,而且比较难想。由于要写成连续正整数之和,则肯定是一个等差数列,并且差值为1,这个等差数列不必从1开始,假设其是从x开始的,且个数共有k个,则可以写出这个等差数列为:

x, x+1, x+2, ..., x+k-1

其和为N,根据等差数列的求和公式,可以写出下列等式:

kx + (k-1)k / 2 = N

变形后可得到:

kx = N - (k-1)k / 2

这样,只要对于任意一个k值,x能得到正整数解,就表示一定会有一个对应的等差数列和为N。下面要来求k的范围,由于k是等差数列的长度,首先肯定是要大于0的,这是下限。求上限还是要利用上面的那个式子,由于x也必须是正整数,可以得到不等式:

N - (k-1)k / 2 > 0

从而得到近似解:

k < sqrt(2N)

有了k的范围就可以开始遍历了,首先数字N本身也是符合题意的,可以看作是长度为1的等差数列,则 res 可以初始化为1,然后i从2遍历到 sqrt(2N),对于每个i值,只要 (N - i(i-1)/2) 能整除i,就表示存在长度为i的等差数列和为N,结果 res 自增1,这样就可以求出所有符合题意的等差数列的个数,参见代码如下:

解法一:

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

还可以换一种写法,核心思路还是跟上面的解法相同,要找是否存在和为N的等差数列,根据上面的分析,需要看等差数列的起始值x是否为整数,若这个等差数列每个数字都减去一个 x-1,就变成了一个从1开始的差值为1的等差数列,那就让i从1开始遍历,用一个变量 sum,每次都加上i值,这样就相当于计算了这个等差数列的和,然后每次看 N-sum 是否能整除i,能的话就表明存在长度为i的等差数列和为N,参见代码如下:


解法二:

class Solution {
public:
int consecutiveNumbersSum(int N) {
int res = 0, sum = 0;
for (int i = 1; sum < N; ++i) {
sum += i;
if ((N - sum) % i == 0) ++res;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/829

参考资料:

https://leetcode.com/problems/consecutive-numbers-sum/

https://leetcode.com/problems/consecutive-numbers-sum/discuss/129227/JAVA-easy-4-lines-O(n0.5)

https://leetcode.com/problems/consecutive-numbers-sum/discuss/128959/5-line-O(N-0.5)-Java-code-Math-method

https://leetcode.com/problems/consecutive-numbers-sum/discuss/129015/5-lines-C%2B%2B-solution-with-detailed-mathematical-explanation.

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 829. Consecutive Numbers Sum 连续数字之和的更多相关文章

  1. 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...

  2. 829. Consecutive Numbers Sum

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

  3. [Swift]LeetCode829. 连续整数求和 | Consecutive Numbers Sum

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

  4. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  5. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  7. [LeetCode] Max Consecutive Ones 最大连续1的个数

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  8. [LeetCode#180]Consecutive Numbers

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  9. Consecutive Numbers Sum

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

随机推荐

  1. IDEA帮助文档快捷键ctrl+q 查看类 方法 变量 帮助文档 注释 快捷键

    IDEA查看类 成员变量  局部变量注释快捷键,Ctrl +Q 查看帮助文档 实际项目中,通常一个类中的代码都不少,而且有很多的变量 那么如何快速知道这个变量的一些信息,比如类型,定义? 比如在第50 ...

  2. 【UOJ#386】【UNR#3】鸽子固定器(贪心)

    [UOJ#386][UNR#3]鸽子固定器(贪心) 题面 UOJ 题解 一个不难想到的暴力做法是把东西按照\(s\)排序,这样子我们枚举极大值和极小值,那么我们选择的一定是这一段之间\(v\)最大的那 ...

  3. 【redis】redis异常-MISCONF Redis is configured to save RDB snapshots

    使用redis报错: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persis ...

  4. C# 刷遍 Leetcode 面试题系列连载(3): No.728 - 自除数

    前文传送门: C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 系列教程索引 传送门:https://enjoy2 ...

  5. C#刷遍Leetcode系列连载 索引

    C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...

  6. 基于log4net的日志组件扩展封装,实现自动记录交互日志 XYH.Log4Net.Extend(微服务监控)

    背景: 随着公司的项目不断的完善,功能越来越复杂,服务也越来越多(微服务),公司迫切需要对整个系统的每一个程序的运行情况进行监控,并且能够实现对自动记录不同服务间的程序调用的交互日志,以及通一个服务或 ...

  7. NeatUpload .NetFromWork4.0 config配置

    NeatUpload使用---config配置(可进行大文件传输) configuration> 下增加: <configSections> <sectionGroup nam ...

  8. 解决FastCGI 进程超过了配置的活动超时时限的问题

    近日,需要满足测试需求,进行大数据并发测试时,报出[HTTP 错误 500.0 - Internal Server Error E:\PHP\php-cgi.exe - FastCGI 进程超过了配置 ...

  9. Java自学-集合框架 Collections

    Java集合框架 工具类Collections Collections是一个类,容器的工具类,就如同Arrays是数组的工具类 步骤 1 : 反转 reverse 使List中的数据发生翻转 pack ...

  10. 在vue项目中通过iframe引入jquery项目

    最近公司因为原来的jq框架存在的问题太多,所以要进行主题框架的重新搭建,我使用的vue进行的主题框架的重新搭建,但是原来的页面已经完成很多了,而且都是使用的jquery进行开发的 在vue中引入jqu ...