当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
Hello World generation
CX5120控制汇川IS620N伺服报错E15解决方案
不用加减乘除实现加法
Take you through a circuit board, from design to production (dry goods)
POI exports excel and displays hierarchically according to parent-child nodes
Test function in pychram
用小程序的技术优势发展产业互联网
Keil5中如何做到 0 Error(s), 0 Warning(s).
Cookie&Session
别再说不会解决 “跨域“ 问题啦
随机推荐
串口接收数据方案设计
ES6解构语法详解
leetcode 1482 猜猜看啊,这道题目怎么二分?
Leetcode:829. 连续整数求和
Feature pyramid networks for object detection
ECMAScript 6.0
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
POI exports excel and displays hierarchically according to parent-child nodes
Ridge regression and lasso regression
Redis tutorial
Feign remote call and getaway gateway
File upload and download
leetcode 1818 绝对值,排序,二分法,最大值
Md5sum operation
Latest interface automation interview questions
pytorch训练深度学习网络设置cuda指定的GPU可见
Chapter 03_ User and authority management
Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
pytorch nn.AdaptiveAvgPool2d(1)
岭回归和lasso回归