【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/consecutive-numbers-sum/
题目描述
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,有多少种把它展开成连续正整数之和的方案。
解题方法
数学方法
有些题会让人陷入常规做法不能自拔,比如这个题就可能想成回溯或者O(M*N)的循环。事实上可以通过数学方法进行优化。
因为是连续正整数之和,所以可以使用数列求和公式直接求和,把O(M*N)的方案降到O(M)。
由N = a + (a + 1) + (a + 2) + (a + 3) + ... + (a + i)
得,
N = a * (i + 1) + (i * (i + 1)) / 2
,其中,a > 0
并且i>= 0
。
所以,我们先求出prod = (i * (i + 1)) / 2 , 如果这个数字大于N了,就立刻结束。否则,remain = N - prod ==> a * (i + 1). 所以,remain应该能够整除(i + 1),而且余数为a > 0。
这个题用python代码会超时,改用C++就能通过。
C++代码如下:
class Solution {
public:
int consecutiveNumbersSum(int N) {
int res = 0;
for (int i = 0; i < N; i++) {
int prod = (i + 1) * i / 2;
if (prod > N) break;
int remain = N - prod;
if (remain % (i + 1) != 0)
continue;
if (remain / (i + 1) > 0)
res ++;
}
return res;
}
};
日期
2019 年 1 月 5 日 —— 美好的周末又开始了
【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)的更多相关文章
- [LeetCode] 829. Consecutive Numbers Sum 连续数字之和
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 829. Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- 【LeetCode】39. Combination Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...
- 【LeetCode】494. Target Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
随机推荐
- Linux之crond任务调度
1. 示意图 2. 基本语法 crontab [选项] # -e : 编辑crontab定时任务 # -l : 查询crontab # -r : 删除当前用户所有的crontab任务 # 例子: # ...
- EXCEL——排序函数RANK,6种花式使用技巧
我们在实际工作中,常常把RANK函数用于对一列数据的基本排序,即从大到小的排序方法,那你还知道它的其他什么用法吗? 今天就给大家系统的分享下RANK函数的用法,分享的内容主要为以下这6种技巧. 1.升 ...
- 16-4SUM
4.30更新,已经AC 18. 4Sum My Submissions QuestionEditorial Solution Total Accepted: 71102 Total Submissio ...
- 重新整理 .net core 实践篇——— endpoint[四十七]
前言 简单整理一些endpoint的一些东西,主要是介绍一个这个endpoint是什么. 正文 endpoint 从表面意思是端点的意思,也就是说比如客户端的某一个action 是一个点,那么服务端的 ...
- 在Idea上用JDBC连接mysql数据库
一.前言 本次操作建立在idea中java环境已配置的基础上 二.操作步骤 1.建立Web项目后,添加驱动包 mysql-connector-java-5.0.8-bin.jar (1)下载mysql ...
- 巩固javaweb的第二十五天
常用的验证 1. 非空验证 // 验证是否是空 function isNull(str) { if(str.length==0) return true; else return false; } 2 ...
- c学习 - 算法
简介: 一个程序包括两方面内容:数据结构.算法 数据结构:对数据的描述,包括数据的类型和数据的组织形式 算法:对操作的描述,即操作步骤 (程序=算法+数据结构) 算法是灵魂,数据结构是加工对象,语言是 ...
- java加密方式
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...
- Linux学习 - 分区与文件系统
一.分区类型 1 主分区:总共最多只能分四个 2 扩展分区:只能有一个(主分区中的一个分区),不能存储数据和格式化,必须再划分成逻辑分区 才 ...
- android 调用相机拍照及相册
调用系统相机拍照: private Button btnDyxj; private ImageView img1; private File tempFile; btnDyxj = (Button) ...