当前位置:网站首页>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;
}
};
边栏推荐
- 详解Spark运行模式(local+standalone+yarn)
- Basic concepts of database
- GCC usage, makefile summary
- Stop saying that you can't solve the "cross domain" problem
- LeetCode 128最长连续序列(哈希set)
- Golang多图生成gif
- Keil5中如何做到 0 Error(s), 0 Warning(s).
- Feign remote call and getaway gateway
- Take you through a circuit board, from design to production (dry goods)
- Force buckle - sum of two numbers
猜你喜欢
随机推荐
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
Common interview questions for performance test
Force buckle - sum of two numbers
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
About the application of MySQL
Cookie&Session
Nacos
JS日常开发小技巧(持续更新)
Error accessing URL 404
Analyze datahub, a new generation metadata platform of 4.7K star
C#实现图的深度优先遍历--非递归代码
ASGNet论文和代码解读2
ECMAScript 6.0
详解Spark运行模式(local+standalone+yarn)
Gorilla/mux framework (RK boot): RPC error code design
二叉树神级遍历:Morris遍历
8 pits of redis distributed lock
Pathmeasure implements loading animation
EtherCAT简介
How do I use Google Chrome 11's Upload Folder feature in my own code?









