Skip to content

Commit 132a326

Browse files
authored
feat: add weekly contest 477 (#4857)
1 parent 8f9780d commit 132a326

File tree

26 files changed

+1906
-2
lines changed

26 files changed

+1906
-2
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3754.Concatenate%20Non-Zero%20Digits%20and%20Multiply%20by%20Sum%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3754. 连接非零数字并乘以其数字和 I](https://leetcode.cn/problems/concatenate-non-zero-digits-and-multiply-by-sum-i)
10+
11+
[English Version](/solution/3700-3799/3754.Concatenate%20Non-Zero%20Digits%20and%20Multiply%20by%20Sum%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数 <code>n</code>。</p>
18+
19+
<p>将 <code>n</code> 中所有的&nbsp;<strong>非零数字&nbsp;</strong>按照它们的原始顺序连接起来,形成一个新的整数 <code>x</code>。如果不存在&nbsp;<strong>非零数字&nbsp;</strong>,则 <code>x = 0</code>。</p>
20+
21+
<p><code>sum</code> 为 <code>x</code> 中所有数字的&nbsp;<strong>数字和&nbsp;</strong>。</p>
22+
23+
<p>返回一个整数,表示 <code>x * sum</code> 的值。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>输入:</strong> <span class="example-io">n = 10203004</span></p>
31+
32+
<p><strong>输出:</strong> <span class="example-io">12340</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<ul>
37+
<li>非零数字是 1、2、3 和 4。因此,<code>x = 1234</code>。</li>
38+
<li>数字和为 <code>sum = 1 + 2 + 3 + 4 = 10</code>。</li>
39+
<li>因此,答案是 <code>x * sum = 1234 * 10 = 12340</code>。</li>
40+
</ul>
41+
</div>
42+
43+
<p><strong class="example">示例 2:</strong></p>
44+
45+
<div class="example-block">
46+
<p><strong>输入:</strong> <span class="example-io">n = 1000</span></p>
47+
48+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
49+
50+
<p><strong>解释:</strong></p>
51+
52+
<ul>
53+
<li>非零数字是 1,因此 <code>x = 1</code> 且 <code>sum = 1</code>。</li>
54+
<li>因此,答案是 <code>x * sum = 1 * 1 = 1</code>。</li>
55+
</ul>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong>提示:</strong></p>
61+
62+
<ul>
63+
<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
64+
</ul>
65+
66+
<!-- description:end -->
67+
68+
## 解法
69+
70+
<!-- solution:start -->
71+
72+
### 方法一:模拟
73+
74+
我们可以通过对数字逐位处理来模拟题目要求的操作。在处理每一位数字时,我们将非零数字连接起来形成新的整数 $x$,同时计算数字和 $s$,最后返回 $x \times s$ 即可。
75+
76+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。
77+
78+
<!-- tabs:start -->
79+
80+
#### Python3
81+
82+
```python
83+
class Solution:
84+
def sumAndMultiply(self, n: int) -> int:
85+
p = 1
86+
x = s = 0
87+
while n:
88+
v = n % 10
89+
s += v
90+
if v:
91+
x += p * v
92+
p *= 10
93+
n //= 10
94+
return x * s
95+
```
96+
97+
#### Java
98+
99+
```java
100+
class Solution {
101+
public long sumAndMultiply(int n) {
102+
int p = 1;
103+
int x = 0, s = 0;
104+
for (; n > 0; n /= 10) {
105+
int v = n % 10;
106+
s += v;
107+
if (v != 0) {
108+
x += p * v;
109+
p *= 10;
110+
}
111+
}
112+
return 1L * x * s;
113+
}
114+
}
115+
```
116+
117+
#### C++
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
long long sumAndMultiply(int n) {
123+
int p = 1;
124+
int x = 0, s = 0;
125+
for (; n > 0; n /= 10) {
126+
int v = n % 10;
127+
s += v;
128+
if (v != 0) {
129+
x += p * v;
130+
p *= 10;
131+
}
132+
}
133+
return 1LL * x * s;
134+
}
135+
};
136+
```
137+
138+
#### Go
139+
140+
```go
141+
func sumAndMultiply(n int) int64 {
142+
p := 1
143+
x := 0
144+
s := 0
145+
for n > 0 {
146+
v := n % 10
147+
s += v
148+
if v != 0 {
149+
x += p * v
150+
p *= 10
151+
}
152+
n /= 10
153+
}
154+
return int64(x) * int64(s)
155+
}
156+
```
157+
158+
#### TypeScript
159+
160+
```ts
161+
function sumAndMultiply(n: number): number {
162+
let p = 1;
163+
let x = 0;
164+
let s = 0;
165+
166+
while (n > 0) {
167+
const v = n % 10;
168+
s += v;
169+
if (v !== 0) {
170+
x += p * v;
171+
p *= 10;
172+
}
173+
n = Math.floor(n / 10);
174+
}
175+
176+
return x * s;
177+
}
178+
```
179+
180+
<!-- tabs:end -->
181+
182+
<!-- solution:end -->
183+
184+
<!-- problem:end -->
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3754.Concatenate%20Non-Zero%20Digits%20and%20Multiply%20by%20Sum%20I/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3754. Concatenate Non-Zero Digits and Multiply by Sum I](https://leetcode.com/problems/concatenate-non-zero-digits-and-multiply-by-sum-i)
10+
11+
[中文文档](/solution/3700-3799/3754.Concatenate%20Non-Zero%20Digits%20and%20Multiply%20by%20Sum%20I/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer <code>n</code>.</p>
18+
19+
<p>Form a new integer <code>x</code> by concatenating all the <strong>non-zero digits</strong> of <code>n</code> in their original order. If there are no <strong>non-zero</strong> digits, <code>x = 0</code>.</p>
20+
21+
<p>Let <code>sum</code> be the <strong>sum of digits</strong> in <code>x</code>.</p>
22+
23+
<p>Return an integer representing the value of <code>x * sum</code>.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">n = 10203004</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">12340</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<ul>
36+
<li>The non-zero digits are 1, 2, 3, and 4. Thus, <code>x = 1234</code>.</li>
37+
<li>The sum of digits is <code>sum = 1 + 2 + 3 + 4 = 10</code>.</li>
38+
<li>Therefore, the answer is <code>x * sum = 1234 * 10 = 12340</code>.</li>
39+
</ul>
40+
</div>
41+
42+
<p><strong class="example">Example 2:</strong></p>
43+
44+
<div class="example-block">
45+
<p><strong>Input:</strong> <span class="example-io">n = 1000</span></p>
46+
47+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
48+
49+
<p><strong>Explanation:</strong></p>
50+
51+
<ul>
52+
<li>The non-zero digit is 1, so <code>x = 1</code> and <code>sum = 1</code>.</li>
53+
<li>Therefore, the answer is <code>x * sum = 1 * 1 = 1</code>.</li>
54+
</ul>
55+
</div>
56+
57+
<p>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
62+
</ul>
63+
64+
<!-- description:end -->
65+
66+
## Solutions
67+
68+
<!-- solution:start -->
69+
70+
### Solution 1: Simulation
71+
72+
We can simulate the required operation by processing the number digit by digit. While processing each digit, we concatenate non-zero digits to form a new integer $x$ and calculate the digit sum $s$. Finally, we return $x \times s$.
73+
74+
The time complexity is $O(\log n)$ and the space complexity is $O(1)$.
75+
76+
<!-- tabs:start -->
77+
78+
#### Python3
79+
80+
```python
81+
class Solution:
82+
def sumAndMultiply(self, n: int) -> int:
83+
p = 1
84+
x = s = 0
85+
while n:
86+
v = n % 10
87+
s += v
88+
if v:
89+
x += p * v
90+
p *= 10
91+
n //= 10
92+
return x * s
93+
```
94+
95+
#### Java
96+
97+
```java
98+
class Solution {
99+
public long sumAndMultiply(int n) {
100+
int p = 1;
101+
int x = 0, s = 0;
102+
for (; n > 0; n /= 10) {
103+
int v = n % 10;
104+
s += v;
105+
if (v != 0) {
106+
x += p * v;
107+
p *= 10;
108+
}
109+
}
110+
return 1L * x * s;
111+
}
112+
}
113+
```
114+
115+
#### C++
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
long long sumAndMultiply(int n) {
121+
int p = 1;
122+
int x = 0, s = 0;
123+
for (; n > 0; n /= 10) {
124+
int v = n % 10;
125+
s += v;
126+
if (v != 0) {
127+
x += p * v;
128+
p *= 10;
129+
}
130+
}
131+
return 1LL * x * s;
132+
}
133+
};
134+
```
135+
136+
#### Go
137+
138+
```go
139+
func sumAndMultiply(n int) int64 {
140+
p := 1
141+
x := 0
142+
s := 0
143+
for n > 0 {
144+
v := n % 10
145+
s += v
146+
if v != 0 {
147+
x += p * v
148+
p *= 10
149+
}
150+
n /= 10
151+
}
152+
return int64(x) * int64(s)
153+
}
154+
```
155+
156+
#### TypeScript
157+
158+
```ts
159+
function sumAndMultiply(n: number): number {
160+
let p = 1;
161+
let x = 0;
162+
let s = 0;
163+
164+
while (n > 0) {
165+
const v = n % 10;
166+
s += v;
167+
if (v !== 0) {
168+
x += p * v;
169+
p *= 10;
170+
}
171+
n = Math.floor(n / 10);
172+
}
173+
174+
return x * s;
175+
}
176+
```
177+
178+
<!-- tabs:end -->
179+
180+
<!-- solution:end -->
181+
182+
<!-- problem:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
long long sumAndMultiply(int n) {
4+
int p = 1;
5+
int x = 0, s = 0;
6+
for (; n > 0; n /= 10) {
7+
int v = n % 10;
8+
s += v;
9+
if (v != 0) {
10+
x += p * v;
11+
p *= 10;
12+
}
13+
}
14+
return 1LL * x * s;
15+
}
16+
};

0 commit comments

Comments
 (0)