当前位置:网站首页>Leetcode:829. Sum of continuous integers
Leetcode:829. Sum of continuous integers
2022-07-01 03:32:00 【Re:fused】
subject :829. Sum consecutive integers
The question :
Given a positive integer n, return Continuous positive integers satisfy that the sum of all numbers is n Number of groups .
Answer key :
I thought of the equal deviation as 1 Equal difference sequence of , So there's a formula m a 1 + ( m − 1 ) m / 2 = m ( m a 1 + ( m − 1 ) / 2 ) = n ma_1+(m-1)m/2=m(ma_1+(m-1)/2) = n ma1+(m−1)m/2=m(ma1+(m−1)/2)=n, It can be seen that m Must be n My silver , also m-1 Even number , To ensure a 1 a_1 a1 Integers .
Code :
class Solution {
public:
int consecutiveNumbersSum(int n) {
int half = sqrt(n);
int cn = 0;
for(int i = 1; i <= half; i++){
if(i == half && i*i == n){
if((i-1)%2 == 0)cn++;
}
else{
if(n % i == 0){
if((i -1)%2 == 0)cn++;
int other = n / i;
if((other-1)%2 == 0)cn++;
}
}
}
return cn;
}
};
边栏推荐
猜你喜欢
Detailed list of errors related to twincat3 ads of Beifu
倍福TwinCAT3 Ads相关错误详细列表
家居网购项目
服务器渲染技术jsp
Elk elegant management server log
Pyramid Scene Parsing Network【PSPNet】论文阅读
C语言多线程编程入门学习笔记
EtherCAT原理概述
Thread data sharing and security -threadlocal
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
随机推荐
多元线性回归
pytorch nn.AdaptiveAvgPool2d(1)
Test function in pychram
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
Chapter 03_ User and authority management
How do I use Google Chrome 11's Upload Folder feature in my own code?
Filter
二叉树神级遍历:Morris遍历
第03章_用戶與權限管理
监听器 Listener
数据库中COMMENT关键字的使用
Hal library setting STM32 interrupt
gcc使用、Makefile总结
Stop saying that you can't solve the "cross domain" problem
Go tool cli for command line implementation
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
BluePrism注册下载并安装-RPA第一章
实现pow(x,n)函数
ES6解构语法详解
Latest interface automation interview questions