当前位置:网站首页>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;
}
};
边栏推荐
- Server rendering technology JSP
- FCN全卷积网络理解及代码实现(来自pytorch官方实现)
- [reading notes] copywriting realization -- four golden steps for writing effective copywriting
- How to achieve 0 error (s) and 0 warning (s) in keil5
- Druid监控统计数据源
- Edge Drawing: A combined real-time edge and segment detector 翻译
- shell脚本使用两个横杠接收外部参数
- 深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
- EtherCAT原理概述
- Research on target recognition and tracking based on 3D laser point cloud
猜你喜欢
随机推荐
Server rendering technology JSP
C#实现图的深度优先遍历--非递归代码
Feign remote call and getaway gateway
Pyramid Scene Parsing Network【PSPNet】论文阅读
JS daily development tips (continuous update)
Go tool cli for command line implementation
MySQL knowledge points
Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
ASGNet论文和代码解读2
Keil5中如何做到 0 Error(s), 0 Warning(s).
A few lines of transaction codes cost me 160000 yuan
线程数据共享和安全 -ThreadLocal
8 pits of redis distributed lock
Md5sum operation
Latest interface automation interview questions
C language EXECL function
Edge drawing: a combined real-time edge and segment detector
Kmeans
Hal library setting STM32 interrupt
Design of serial port receiving data scheme









