当前位置:网站首页>Leetcode 829. 连续整数求和
Leetcode 829. 连续整数求和
2022-06-10 13:17:00 【joseri】
Leetcode 829. 连续整数求和
原题链接:https://leetcode.cn/problems/consecutive-numbers-sum/
难度等级:困难
感觉这题算不上一个困难题,思路想清楚之后代码也比较短。
首先我们考虑从l+(l+1)+...+r 这个过程,如果用S来代表他们的和,那么
S = ( l + r ) ∗ ( r − l + 1 ) / 2 S = (l+r)*(r-l+1)/2 S=(l+r)∗(r−l+1)/2
我们将其参数转化。令r'=r+1/2l'=l-1/2 ,就可以得(l'+r')*(r'-l')=2*S,也就转化成了平方和公式,我们便将原问题转化为了如何将2*S这个数分解成两个因数。
下面考察两个因数a和b的奇偶性:
当a和b奇偶性相同时,我们把a和b分别带进参数方程中,a*b = 2 * S
有 l'+r' = a && r'-l'= b => r' = (a+b)*1.0/2, l'= (a-b)*1.0/2如果a和b奇偶性相同,a+b和a-b都是偶数,那么
r'和l'就都是整数,不要忘了我们还要反代回l和r要加减1/2,所以不满足l和r都是整数的题干。舍弃掉当a和b奇偶性不同时,
r'和l'都是1/2+c(c代指常整数),反代回l和r就都是整数了,满足题意。
分析之后,一句话来概括,我们要找的就是两个数乘积等于2*n且两个数的奇偶性相同的组数。
直接上代码
class Solution {
public:
int consecutiveNumbersSum(int n) {
int cnt = 0;
int tn = 2 * n;
for(int i = 1; i < tn/i; i++) {
if(tn%i==0) {
int a = tn / i, b = i;
if((a&1)==(b&1)) continue;
else cnt++;
}
}
return cnt;
}
};
十分钟解决一道困难题多是一件美事啊!
边栏推荐
- buuctf [Discuz]wooyun-2010-080723
- Google Earth Engine(GEE)——基于s2影像的实时全球10米土地利用/土地覆盖(LULC)数据集
- 13、 System call and shell (freesanding shell, terminal and job control)
- Error:top-left corner pixel must be either opaque white or transparent.
- Student status management system
- 汇编语言入门-总结
- Timeline and logistics information. You don't need stepview at all
- Buuctf [glassfish] arbitrary file reading
- Example of full page sliding screen at mobile terminal (sliding the whole screen up and down) (sorting)
- CF894C Marco and GCD Sequence
猜你喜欢
![[NLP] NLP full path learning recommendation](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[NLP] NLP full path learning recommendation
![buuctf [GlassFish]任意文件读取](/img/37/e3c127f2f2ba97c5ca0b6cf01cf9ab.png)
buuctf [GlassFish]任意文件读取

Z-Wave ecosystem status report in 2022

13、 System call and shell (freesanding shell, terminal and job control)

2022 ciscn preliminary satool

32、树莓派的简单测试串口通信和超声波模块测距

什么是基因的p值?

3. web page development tool vs Code
10 competitive airpods Pro products worth your choice

apache atlas 快速入门
随机推荐
Qt: 访问其他窗体中的控件
WT2003H4-16S 语音芯片按键录音及播放应用解析
深度神经网络每秒分类近20亿张图像,新型类脑光学分类器芯片登上Nature
Ten easy-to-use cross browser testing tools to share, good things worth collecting
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
出海企业遇瓶颈 茄子科技(SHAREit Group)有话说
Development trend of Web Development
解决VMWareStation安装 tools 时 D:\setup.exe 找不到的问题
buuctf [Discuz]wooyun-2010-080723
解决VMware Workstation安装VMware Tools显示灰色的办法
Lecture on linear dynamic programming
Introduction to assembly language - Summary
[Netease Yunxin] in depth analysis of the design of "circle group" relationship system | series of articles on "circle group" technology
Neuron Newsletter 2022-05|新增 2 個南向驅動和 1 個北向應用、Modbus TCP 實現定制擴展
Example of full page sliding screen at mobile terminal (sliding the whole screen up and down) (sorting)
Multithreading killer ---countdownlatch & cyclicbarrier
Google Earth Engine(GEE)——利用MODIS 的叶面积指数影像掩膜dem批量下载
[yellow code] SVN version control tutorial
Shell Encyclopedia
13、 System call and shell (freesanding shell, terminal and job control)