# 2894. Divisible and Non-divisible Sums Difference [2894. Divisible and Non-divisible Sums Difference](https://leetcode.com/problems/divisible-and-non-divisible-sums-difference/) (<font color="#00AF9B"> Easy</font> 通過率: 89.0%) ## 限制條件 <ul> <li>1 <= n, m <= 1000</li> </ul> ### 解法 1 照著題意解就好 - 時間複雜度: O(n) - 空間複雜度: O(1) ```cpp!= class Solution { public: int differenceOfSums(int n, int m) { int num1 = 0, num2 = 0; for (int i = 1; i <= n; i++) { if (i % m) num1 += i; else num2 += i; } return num1 - num2; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up