当前位置:网站首页>leetcode 829. Sum of continuous integers
leetcode 829. Sum of continuous integers
2022-06-22 13:18:00 【A man of many ages】
Title Description :
Given a positive integer n, return Continuous positive integers satisfy that the sum of all numbers is n Number of groups .
Example 1:
Input : n = 5
Output : 2
explain : 5 = 2 + 3, There are two sets of consecutive integers ([5],[2,3]) The sum is 5.
Example 2:
Input : n = 9
Output : 3
explain : 9 = 4 + 5 = 2 + 3 + 4
Example 3:
Input : n = 15
Output : 4
explain : 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
Tips :
1 <= n <= 109
analysis
today leetcode My daily question , The difficulty of the topic is marked as hard It completely misleads us to think about complicated places ,ac Then I read the next few questions , The answer to this question is hard Difficult topics are discussed in categories , Though it is simple, I still want to write down the solutions and share my ideas .
The sum of consecutive integers equals n, Most people's first reaction is to sum up the sequence of equal differences , But once the formula is written, the problem becomes complicated , The most important thing to solve a mathematical problem is to find the law first , Instead of thinking about n It can be divided into the sum of several consecutive positive integers , Let's think about continuity k What are the characteristics of the sum of integers .
continuity 1 Sum of positive integers , As long as it is a positive integer .
continuity 2 Sum of positive integers , It must be odd and even , as long as n > 2 also n Is an odd number .
continuity 3 Sum of positive integers , such as 4 + 5 + 6, The numbers on both sides must add up to twice the middle number , So you just need to n > 5 also n Can be 3 Can be divided exactly by .
There seems to be no general rule for enumerating to this , But it's continuous 4 The law of the sum of integers is enough for us to kill the problem , Let's look at the sum of the first four consecutive positive integers 10 = 1 + 2 + 3 + 4, The second is 14 = 2 + 3 + 4 + 5, If we find the law according to the previous thought of summation , Must be thinking 2 + 5 = 3 + 4, As long as the sum is twice what number , With other conditions, it can be expressed as continuous 4 The sum of two integers . Consider 10 and 14 The relationship between , Because it is expressed as 14 Four integers of , Each integer ratio is expressed as 10 More than four integers of 1, That's why there are so many 4, The sum of the third consecutive four positive integers is 18, It can be noted that , as long as n - 10 Can be 4 to be divisible by ,n I can write it as 4 The sum of consecutive positive integers , This is the general rule .
For positive integers n for , If the first consecutive k The sum of the numbers is s, as long as (n - s) Can be k to be divisible by , that n It must be expressed as k Sum of integers . So what we need to do is that enumerations can be expressed as continuous 1,2,3,… What is the first integer of the sum of positive integers , The first one can be expressed as continuous 1 The sum of positive integers is 1, The first one can be expressed as continuous 2 The sum of positive integers is 1 + 2 = 3, The first one can be expressed as continuous 3 The sum of positive integers is 1 + 2 + 3 = 6, The first one can be expressed as continuous 4 The sum of positive integers is 1 + 2 + 3 + 4 = 10, So we can enumerate k While judging n Can it be expressed as k The sum of positive integers .
This topic easy The difficulty code is as follows :
Code
class Solution {
public:
int consecutiveNumbersSum(int n) {
int s = 0,t = 1;
int ans = 0;
while(s < n) {
s += t;
if((n - s) % t == 0) ans++;
t++;
}
return ans;
}
};
边栏推荐
- leetcode 11. 盛最多水的容器
- Sap-abap- how to transfer material master data, supplier master data, work orders, purchase orders and other information to external systems in real time - implicit enhancement.
- Using Sqlalchemy for combined paging queries
- AcWing第54场周赛
- 2017年度总结
- 461. Hamming Distance
- Secondary development of robotframework - real time log
- 阿里云磁盘性能分析
- Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
猜你喜欢

MySQL notes

769. Max Chunks To Make Sorted

CVPR 2022 | visual language model pre training for scene text detection

基於SSM的小區垃圾分類和運輸管理系統,高質量畢業論文範例(可直接使用),源碼,數據庫脚本,項目導入運行視頻教程,論文撰寫教程

6月《中国数据库行业分析报告》发布!智能风起,列存更生

Sap-abap- how to transfer material master data, supplier master data, work orders, purchase orders and other information to external systems in real time - implicit enhancement.

Leetcode 297 match de la semaine

基于SSM的小区垃圾分类和运输管理系统,高质量毕业论文范例(可直接使用),源码,数据库脚本,项目导入运行视频教程,论文撰写教程

MySQL笔记

SNC processing failed SAP Router证书重新生成
随机推荐
JAXB元素详解
MySQL notes
448. Find All Numbers Disappeared in an Array
PHP deserialization & Magic method
SAP system license viewing application and import
天坑专业学IC设计自学的话有公司会要吗
leetcode 第 297 場周賽
268. Missing Number
通过 postgis 制作 按照米制的矩形边框
RobotFramework二次开发——实时日志
AcWing第54场周赛
AcWing第52场周赛
leetcode LCP 10. 二叉树任务调度
Redis active / standby configuration dockercompose version
MySQL 5.7 + Navicat 下载安装教程(附安装包)
Alicloud disk performance analysis
redis主备配置dockercompose版
6月《中国数据库行业分析报告》发布!智能风起,列存更生
MySQL笔记
If Tiankeng majors learn IC design by themselves, will any company want it