Skip to content

Commit f4e3924

Browse files
authored
feat: update solutions to lc problem: No.2535 (doocs#3563)
No.2535.Difference Between Element Sum and Digit Sum of an Array
1 parent 742a88c commit f4e3924

File tree

14 files changed

+368
-238
lines changed

14 files changed

+368
-238
lines changed

solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README.md

+51-83
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ nums 的数字和是 1 + 2 + 3 + 4 = 10 。
7171

7272
### 方法一:模拟
7373

74-
我们遍历数组 $nums$,计算元素和 $a$ 与数字和 $b$,最后返回 $|a - b|$ 即可。
74+
我们遍历数组 $\textit{nums}$,计算元素和 $x$ 和数字和 $y$,最后返回 $|x - y|$ 即可。由于 $x$ 一定大于等于 $y$,所以我们也可以直接返回 $x - y$
7575

76-
时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。
76+
时间复杂度 $O(n \times \log_{10} M)$,其中 $n$ 和 $M$ 分别是数组 $\textit{nums}$ 的长度和数组中元素的最大值。空间复杂度 $O(1)$。
7777

7878
<!-- tabs:start -->
7979

@@ -82,27 +82,28 @@ nums 的数字和是 1 + 2 + 3 + 4 = 10 。
8282
```python
8383
class Solution:
8484
def differenceOfSum(self, nums: List[int]) -> int:
85-
a, b = sum(nums), 0
86-
for x in nums:
87-
while x:
88-
b += x % 10
89-
x //= 10
90-
return abs(a - b)
85+
x = y = 0
86+
for v in nums:
87+
x += v
88+
while v:
89+
y += v % 10
90+
v //= 10
91+
return x - y
9192
```
9293

9394
#### Java
9495

9596
```java
9697
class Solution {
9798
public int differenceOfSum(int[] nums) {
98-
int a = 0, b = 0;
99-
for (int x : nums) {
100-
a += x;
101-
for (; x > 0; x /= 10) {
102-
b += x % 10;
99+
int x = 0, y = 0;
100+
for (int v : nums) {
101+
x += v;
102+
for (; v > 0; v /= 10) {
103+
y += v % 10;
103104
}
104105
}
105-
return Math.abs(a - b);
106+
return x - y;
106107
}
107108
}
108109
```
@@ -113,14 +114,14 @@ class Solution {
113114
class Solution {
114115
public:
115116
int differenceOfSum(vector<int>& nums) {
116-
int a = 0, b = 0;
117-
for (int x : nums) {
118-
a += x;
119-
for (; x; x /= 10) {
120-
b += x % 10;
117+
int x = 0, y = 0;
118+
for (int v : nums) {
119+
x += v;
120+
for (; v; v /= 10) {
121+
y += v % 10;
121122
}
122123
}
123-
return abs(a - b);
124+
return x - y;
124125
}
125126
};
126127
```
@@ -129,36 +130,29 @@ public:
129130
130131
```go
131132
func differenceOfSum(nums []int) int {
132-
a, b := 0, 0
133-
for _, x := range nums {
134-
a += x
135-
for ; x > 0; x /= 10 {
136-
b += x % 10
133+
var x, y int
134+
for _, v := range nums {
135+
x += v
136+
for ; v > 0; v /= 10 {
137+
y += v % 10
137138
}
138139
}
139-
return abs(a - b)
140-
}
141-
142-
func abs(x int) int {
143-
if x < 0 {
144-
return -x
145-
}
146-
return x
140+
return x - y
147141
}
148142
```
149143

150144
#### TypeScript
151145

152146
```ts
153147
function differenceOfSum(nums: number[]): number {
154-
return nums.reduce((r, v) => {
155-
r += v;
156-
while (v !== 0) {
157-
r -= v % 10;
158-
v = Math.floor(v / 10);
148+
let [x, y] = [0, 0];
149+
for (let v of nums) {
150+
x += v;
151+
for (; v; v = Math.floor(v / 10)) {
152+
y += v % 10;
159153
}
160-
return r;
161-
}, 0);
154+
}
155+
return x - y;
162156
}
163157
```
164158

@@ -167,16 +161,19 @@ function differenceOfSum(nums: number[]): number {
167161
```rust
168162
impl Solution {
169163
pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
170-
let mut ans = 0;
171-
for &num in nums.iter() {
172-
let mut num = num;
173-
ans += num;
174-
while num != 0 {
175-
ans -= num % 10;
164+
let mut x = 0;
165+
let mut y = 0;
166+
167+
for &v in &nums {
168+
x += v;
169+
let mut num = v;
170+
while num > 0 {
171+
y += num % 10;
176172
num /= 10;
177173
}
178174
}
179-
ans
175+
176+
x - y
180177
}
181178
}
182179
```
@@ -185,45 +182,16 @@ impl Solution {
185182

186183
```c
187184
int differenceOfSum(int* nums, int numsSize) {
188-
int ans = 0;
185+
int x = 0, y = 0;
189186
for (int i = 0; i < numsSize; i++) {
190-
ans += nums[i];
191-
while (nums[i]) {
192-
ans -= nums[i] % 10;
193-
nums[i] /= 10;
187+
int v = nums[i];
188+
x += v;
189+
while (v > 0) {
190+
y += v % 10;
191+
v /= 10;
194192
}
195193
}
196-
return ans;
197-
}
198-
```
199-
200-
<!-- tabs:end -->
201-
202-
<!-- solution:end -->
203-
204-
<!-- solution:start -->
205-
206-
### 方法二
207-
208-
<!-- tabs:start -->
209-
210-
#### Rust
211-
212-
```rust
213-
impl Solution {
214-
pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
215-
let a: i32 = nums.iter().sum();
216-
let b: i32 = nums
217-
.iter()
218-
.map(|&n| {
219-
n.to_string()
220-
.chars()
221-
.map(|c| c.to_digit(10).unwrap() as i32)
222-
.sum::<i32>()
223-
})
224-
.sum();
225-
(a - b).abs()
226-
}
194+
return x - y;
227195
}
228196
```
229197

0 commit comments

Comments
 (0)