Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ nums 的数字和是 1 + 2 + 3 + 4 = 10 。

### 方法一:模拟

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

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

<!-- tabs:start -->

Expand All @@ -82,27 +82,28 @@ nums 的数字和是 1 + 2 + 3 + 4 = 10 。
```python
class Solution:
def differenceOfSum(self, nums: List[int]) -> int:
a, b = sum(nums), 0
for x in nums:
while x:
b += x % 10
x //= 10
return abs(a - b)
x = y = 0
for v in nums:
x += v
while v:
y += v % 10
v //= 10
return x - y
```

#### Java

```java
class Solution {
public int differenceOfSum(int[] nums) {
int a = 0, b = 0;
for (int x : nums) {
a += x;
for (; x > 0; x /= 10) {
b += x % 10;
int x = 0, y = 0;
for (int v : nums) {
x += v;
for (; v > 0; v /= 10) {
y += v % 10;
}
}
return Math.abs(a - b);
return x - y;
}
}
```
Expand All @@ -113,14 +114,14 @@ class Solution {
class Solution {
public:
int differenceOfSum(vector<int>& nums) {
int a = 0, b = 0;
for (int x : nums) {
a += x;
for (; x; x /= 10) {
b += x % 10;
int x = 0, y = 0;
for (int v : nums) {
x += v;
for (; v; v /= 10) {
y += v % 10;
}
}
return abs(a - b);
return x - y;
}
};
```
Expand All @@ -129,36 +130,29 @@ public:
```go
func differenceOfSum(nums []int) int {
a, b := 0, 0
for _, x := range nums {
a += x
for ; x > 0; x /= 10 {
b += x % 10
var x, y int
for _, v := range nums {
x += v
for ; v > 0; v /= 10 {
y += v % 10
}
}
return abs(a - b)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
return x - y
}
```

#### TypeScript

```ts
function differenceOfSum(nums: number[]): number {
return nums.reduce((r, v) => {
r += v;
while (v !== 0) {
r -= v % 10;
v = Math.floor(v / 10);
let [x, y] = [0, 0];
for (let v of nums) {
x += v;
for (; v; v = Math.floor(v / 10)) {
y += v % 10;
}
return r;
}, 0);
}
return x - y;
}
```

Expand All @@ -167,16 +161,19 @@ function differenceOfSum(nums: number[]): number {
```rust
impl Solution {
pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
let mut ans = 0;
for &num in nums.iter() {
let mut num = num;
ans += num;
while num != 0 {
ans -= num % 10;
let mut x = 0;
let mut y = 0;

for &v in &nums {
x += v;
let mut num = v;
while num > 0 {
y += num % 10;
num /= 10;
}
}
ans

x - y
}
}
```
Expand All @@ -185,45 +182,16 @@ impl Solution {

```c
int differenceOfSum(int* nums, int numsSize) {
int ans = 0;
int x = 0, y = 0;
for (int i = 0; i < numsSize; i++) {
ans += nums[i];
while (nums[i]) {
ans -= nums[i] % 10;
nums[i] /= 10;
int v = nums[i];
x += v;
while (v > 0) {
y += v % 10;
v /= 10;
}
}
return ans;
}
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- solution:start -->
### 方法二
<!-- tabs:start -->
#### Rust
```rust
impl Solution {
pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
let a: i32 = nums.iter().sum();
let b: i32 = nums
.iter()
.map(|&n| {
n.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap() as i32)
.sum::<i32>()
})
.sum();
(a - b).abs()
}
return x - y;
}
```
Expand Down
Loading
Loading