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
分析:https://leetcode.com/problems/consecutive-numbers-sum/discuss/209317/topic
这道题的要求的另一种说法: 把N表示成一个等差数列(公差为1)的和
我们不妨设这个数列的首项是x,项数为m,则这个数列的和就是[x + (x + (m-1))]m / 2 = mx + m(m-1)/2 = N
接下来,一个很自然的想法就是,枚举m,通过上式判断对于相应的m是否存在合法的x。
x = ((N - m(m-1)/2)) / m
显然枚举的复杂度是O(sqrt(N))。因为m能取到的最大值显然是sqrt(n)数量级的
class Solution {
int consecutiveNumbersSum(int N) {
int ans = ;
for (int m = ; ; m++) {
int mx = N - m * (m-) / ;
if (mx <= )
break;
if (mx % m == )
ans++;
}
return ans;
}
}
Consecutive Numbers Sum的更多相关文章
- 829. Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- [Swift]LeetCode829. 连续整数求和 | Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- [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】829. Consecutive Numbers Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...
- leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)
一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...
- LeetCode Database: Consecutive Numbers
Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers
题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...
- [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers
SQL架构: Create table If Not Exists Logs (Id int, Num int) Truncate table Logs insert into Logs (Id, N ...
随机推荐
- CSS 图片自适应容器
https://www.jb51.net/css/660677.html 经常有这样一个场景,需要让图片自适应容器的大小. 1.img标签的方式 我们马上就能想到,把width.height 设置为1 ...
- EMS命令
Tibco EMS 初级使用方法小结 http://blog.csdn.net/bincavin/article/details/8290905
- JAVA图形小动画之简单行星运动
//第一个java文件thread_star.java: package solarsystem; import java.awt.Color;import java.awt.Graphics;imp ...
- 科大讯飞语音转文字,ffmpeg将wav转成pcm二进制音频文件
百度和讯飞和其他厂都提供了语音识别的接口,这里使用讯飞的识别将本地手机录的音频文件转成文字 以下注意事项: 1.X_Param 参数开始的时候带了空格导致验证不过,原因是讯飞将字符串做了repelce ...
- Codeforces 126B. Password(KMP,DP)
Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...
- JS各循环的差别
1.最普通的for循环: for(var i=0;i<arr.length;i++){ } 特点:只能针对数组循环,不能引用于非数组对象 2.for(var i in obj){ } 特点:用于 ...
- No hash for parcel CDH-XXX.parcel.torrent
在安装 CDH 时,到 Install Parcels 这一步,分发 Parcels 一直过不去,界面一直报 Failure due to stall on seeded torrent,查看日志一直 ...
- 括号序列模型--序列dp--U86873 小Y的精灵国机房之旅
括号序列模型及解法 >Codeforces314E◦给定一个长度为n的仅包含左右括号和问号的字符串,将问号变成左括号或右括号使得该括号序列合法,求方案总数.◦例如(())与()()都是合法的括号 ...
- Python - 默认参数传参陷阱
def extend_list(v, li=[]): li.append(v) return li list1 = extend_list(10) print(list1) # [10] list2 ...
- python之scrapy模块pipelines
1.知识点 """" pipelines使用: 1.在spiders里面使用yield生成器 list_li = response.xpath("// ...