分类求和并作差
难度:
标签:
题目描述
You are given positive integers n
and m
.
Define two integers, num1
and num2
, as follows:
num1
: The sum of all integers in the range[1, n]
that are not divisible bym
.num2
: The sum of all integers in the range[1, n]
that are divisible bym
.
Return the integer num1 - num2
.
Example 1:
Input: n = 10, m = 3 Output: 19 Explanation: In the given example: - Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37. - Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18. We return 37 - 18 = 19 as the answer.
Example 2:
Input: n = 5, m = 6 Output: 15 Explanation: In the given example: - Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15. - Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0. We return 15 - 0 = 15 as the answer.
Example 3:
Input: n = 5, m = 1 Output: -15 Explanation: In the given example: - Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0. - Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15. We return 0 - 15 = -15 as the answer.
Constraints:
1 <= n, m <= 1000
代码结果
运行时间: 22 ms, 内存: 16.0 MB
/*
* Problem: Given two positive integers n and m, define two integers num1 and num2 as follows:
* num1: The sum of all integers in the range [1, n] that are not divisible by m.
* num2: The sum of all integers in the range [1, n] that are divisible by m.
* Return the integer num1 - num2.
*/
import java.util.stream.IntStream;
public class Solution {
public int differenceOfSums(int n, int m) {
int num1 = IntStream.rangeClosed(1, n)
.filter(i -> i % m != 0)
.sum();
int num2 = IntStream.rangeClosed(1, n)
.filter(i -> i % m == 0)
.sum();
return num1 - num2;
}
}
解释
方法:
该题解首先计算1到n之间所有整数的总和,这可以通过等差数列求和公式 (1 + n) * n / 2 来完成。接着,作者使用一个循环来求解1到n之间所有能被m整除的整数之和。循环变量cur从m开始,每次增加m,直到超过n为止。每次循环,将cur加到一个累加器diff中。最后,题解返回所有数的总和减去能被m整除的数的总和的两倍,得到的就是所有不能被m整除的数的总和减去能被m整除的数的总和。
时间复杂度:
O(n/m)
空间复杂度:
O(1)
代码细节讲解
🦆
为什么在计算无法被m整除的整数之和时,使用了返回总和减去被m整除的数的总和的两倍的方法?
▷🦆
在循环中计算能被m整除的数的和时,是否有更高效的算法,比如直接计算而不是逐个累加?
▷🦆
如何处理当 m 大于 n 的情况,即没有任何一个数能被 m 整除的特殊情况?
▷