当前位置:网站首页>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;
    }
};
原网站

版权声明
本文为[A man of many ages]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221226033478.html