Skip to content

Commit a9f90ad

Browse files
Merge branch 'main' into main
2 parents 49fd147 + a69bbc0 commit a9f90ad

File tree

19 files changed

+843
-41
lines changed

19 files changed

+843
-41
lines changed

solution/2200-2299/2243.Calculate Digit Sum of a String/README.md

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ tags:
3939
<strong>输出:</strong>"135"
4040
<strong>解释:</strong>
4141
- 第一轮,将 s 分成:"111"、"112"、"222" 和 "23" 。
42-
接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。
42+
接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。
4343
&nbsp; 这样,s 在第一轮之后变成 "3" + "4" + "6" + "5" = "3465" 。
4444
- 第二轮,将 s 分成:"346" 和 "5" 。
4545
&nbsp; 接着,计算每一组的数字和:3 + 4 + 6 = 13 、5 = 5 。
46-
&nbsp; 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
46+
&nbsp; 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
4747
现在,s.length &lt;= k ,所以返回 "135" 作为答案。
4848
</pre>
4949

@@ -53,7 +53,7 @@ tags:
5353
<strong>输出:</strong>"000"
5454
<strong>解释:</strong>
5555
将 "000", "000", and "00".
56-
接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。
56+
接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。
5757
s 变为 "0" + "0" + "0" = "000" ,其长度等于 k ,所以返回 "000" 。
5858
</pre>
5959

@@ -167,19 +167,65 @@ func digitSum(s string, k int) string {
167167

168168
```ts
169169
function digitSum(s: string, k: number): string {
170-
let ans = [];
171170
while (s.length > k) {
171+
const t: number[] = [];
172172
for (let i = 0; i < s.length; i += k) {
173-
let cur = s.slice(i, i + k);
174-
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
173+
const x = s
174+
.slice(i, i + k)
175+
.split('')
176+
.reduce((a, b) => a + +b, 0);
177+
t.push(x);
175178
}
176-
s = ans.join('');
177-
ans = [];
179+
s = t.join('');
178180
}
179181
return s;
180182
}
181183
```
182184

185+
#### Rust
186+
187+
```rust
188+
impl Solution {
189+
pub fn digit_sum(s: String, k: i32) -> String {
190+
let mut s = s;
191+
let k = k as usize;
192+
while s.len() > k {
193+
let mut t = Vec::new();
194+
for chunk in s.as_bytes().chunks(k) {
195+
let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum();
196+
t.push(sum.to_string());
197+
}
198+
s = t.join("");
199+
}
200+
s
201+
}
202+
}
203+
```
204+
205+
#### JavaScript
206+
207+
```js
208+
/**
209+
* @param {string} s
210+
* @param {number} k
211+
* @return {string}
212+
*/
213+
var digitSum = function (s, k) {
214+
while (s.length > k) {
215+
const t = [];
216+
for (let i = 0; i < s.length; i += k) {
217+
const x = s
218+
.slice(i, i + k)
219+
.split('')
220+
.reduce((a, b) => a + +b, 0);
221+
t.push(x);
222+
}
223+
s = t.join('');
224+
}
225+
return s;
226+
};
227+
```
228+
183229
<!-- tabs:end -->
184230

185231
<!-- solution:end -->

solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> s = &quot;11111222223&quot;, k = 3
3939
<strong>Output:</strong> &quot;135&quot;
40-
<strong>Explanation:</strong>
40+
<strong>Explanation:</strong>
4141
- For the first round, we divide s into groups of size 3: &quot;111&quot;, &quot;112&quot;, &quot;222&quot;, and &quot;23&quot;.
42-
​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
42+
​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
4343
&nbsp; So, s becomes &quot;3&quot; + &quot;4&quot; + &quot;6&quot; + &quot;5&quot; = &quot;3465&quot; after the first round.
4444
- For the second round, we divide s into &quot;346&quot; and &quot;5&quot;.
45-
&nbsp; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
46-
&nbsp; So, s becomes &quot;13&quot; + &quot;5&quot; = &quot;135&quot; after second round.
45+
&nbsp; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
46+
&nbsp; So, s becomes &quot;13&quot; + &quot;5&quot; = &quot;135&quot; after second round.
4747
Now, s.length &lt;= k, so we return &quot;135&quot; as the answer.
4848
</pre>
4949

@@ -52,9 +52,9 @@ Now, s.length &lt;= k, so we return &quot;135&quot; as the answer.
5252
<pre>
5353
<strong>Input:</strong> s = &quot;00000000&quot;, k = 3
5454
<strong>Output:</strong> &quot;000&quot;
55-
<strong>Explanation:</strong>
55+
<strong>Explanation:</strong>
5656
We divide s into &quot;000&quot;, &quot;000&quot;, and &quot;00&quot;.
57-
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
57+
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
5858
s becomes &quot;0&quot; + &quot;0&quot; + &quot;0&quot; = &quot;000&quot;, whose length is equal to k, so we return &quot;000&quot;.
5959
</pre>
6060

@@ -73,7 +73,11 @@ s becomes &quot;0&quot; + &quot;0&quot; + &quot;0&quot; = &quot;000&quot;, whose
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Simulation
77+
78+
According to the problem statement, we can simulate the operations described in the problem until the length of the string is less than or equal to $k$. Finally, return the string.
79+
80+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
7781

7882
<!-- tabs:start -->
7983

@@ -163,19 +167,65 @@ func digitSum(s string, k int) string {
163167

164168
```ts
165169
function digitSum(s: string, k: number): string {
166-
let ans = [];
167170
while (s.length > k) {
171+
const t: number[] = [];
168172
for (let i = 0; i < s.length; i += k) {
169-
let cur = s.slice(i, i + k);
170-
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
173+
const x = s
174+
.slice(i, i + k)
175+
.split('')
176+
.reduce((a, b) => a + +b, 0);
177+
t.push(x);
171178
}
172-
s = ans.join('');
173-
ans = [];
179+
s = t.join('');
174180
}
175181
return s;
176182
}
177183
```
178184

185+
#### Rust
186+
187+
```rust
188+
impl Solution {
189+
pub fn digit_sum(s: String, k: i32) -> String {
190+
let mut s = s;
191+
let k = k as usize;
192+
while s.len() > k {
193+
let mut t = Vec::new();
194+
for chunk in s.as_bytes().chunks(k) {
195+
let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum();
196+
t.push(sum.to_string());
197+
}
198+
s = t.join("");
199+
}
200+
s
201+
}
202+
}
203+
```
204+
205+
#### JavaScript
206+
207+
```js
208+
/**
209+
* @param {string} s
210+
* @param {number} k
211+
* @return {string}
212+
*/
213+
var digitSum = function (s, k) {
214+
while (s.length > k) {
215+
const t = [];
216+
for (let i = 0; i < s.length; i += k) {
217+
const x = s
218+
.slice(i, i + k)
219+
.split('')
220+
.reduce((a, b) => a + +b, 0);
221+
t.push(x);
222+
}
223+
s = t.join('');
224+
}
225+
return s;
226+
};
227+
```
228+
179229
<!-- tabs:end -->
180230

181231
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {string}
5+
*/
6+
var digitSum = function (s, k) {
7+
while (s.length > k) {
8+
const t = [];
9+
for (let i = 0; i < s.length; i += k) {
10+
const x = s
11+
.slice(i, i + k)
12+
.split('')
13+
.reduce((a, b) => a + +b, 0);
14+
t.push(x);
15+
}
16+
s = t.join('');
17+
}
18+
return s;
19+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn digit_sum(s: String, k: i32) -> String {
3+
let mut s = s;
4+
let k = k as usize;
5+
while s.len() > k {
6+
let mut t = Vec::new();
7+
for chunk in s.as_bytes().chunks(k) {
8+
let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum();
9+
t.push(sum.to_string());
10+
}
11+
s = t.join("");
12+
}
13+
s
14+
}
15+
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
function digitSum(s: string, k: number): string {
2-
let ans = [];
32
while (s.length > k) {
3+
const t: number[] = [];
44
for (let i = 0; i < s.length; i += k) {
5-
let cur = s.slice(i, i + k);
6-
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
5+
const x = s
6+
.slice(i, i + k)
7+
.split('')
8+
.reduce((a, b) => a + +b, 0);
9+
t.push(x);
710
}
8-
s = ans.join('');
9-
ans = [];
11+
s = t.join('');
1012
}
1113
return s;
1214
}

solution/3400-3499/3477.Fruits Into Baskets II/README.md

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,118 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3477.Fr
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:模拟
84+
85+
我们用一个长度为 $n$ 的布尔数组 $\textit{vis}$ 记录已经被使用的篮子,用一个答案变量 $\textit{ans}$ 记录所有未被放置的水果,初始时 $\textit{ans} = n$。
86+
87+
接下来,我们遍历每一种水果 $x$,对于当前水果,我们遍历所有的篮子,找出第一个未被使用,且容量大于等于 $x$ 的篮子 $i$。如果找到了,那么答案 $\textit{ans}$ 减 $1$。
88+
89+
遍历结束后,返回答案即可。
90+
91+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{fruits}$ 的长度。
8492

8593
<!-- tabs:start -->
8694

8795
#### Python3
8896

8997
```python
90-
98+
class Solution:
99+
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
100+
n = len(fruits)
101+
vis = [False] * n
102+
ans = n
103+
for x in fruits:
104+
for i, y in enumerate(baskets):
105+
if y >= x and not vis[i]:
106+
vis[i] = True
107+
ans -= 1
108+
break
109+
return ans
91110
```
92111

93112
#### Java
94113

95114
```java
96-
115+
class Solution {
116+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
117+
int n = fruits.length;
118+
boolean[] vis = new boolean[n];
119+
int ans = n;
120+
for (int x : fruits) {
121+
for (int i = 0; i < n; ++i) {
122+
if (baskets[i] >= x && !vis[i]) {
123+
vis[i] = true;
124+
--ans;
125+
break;
126+
}
127+
}
128+
}
129+
return ans;
130+
}
131+
}
97132
```
98133

99134
#### C++
100135

101136
```cpp
102-
137+
class Solution {
138+
public:
139+
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
140+
int n = fruits.size();
141+
vector<bool> vis(n);
142+
int ans = n;
143+
for (int x : fruits) {
144+
for (int i = 0; i < n; ++i) {
145+
if (baskets[i] >= x && !vis[i]) {
146+
vis[i] = true;
147+
--ans;
148+
break;
149+
}
150+
}
151+
}
152+
return ans;
153+
}
154+
};
103155
```
104156
105157
#### Go
106158
107159
```go
160+
func numOfUnplacedFruits(fruits []int, baskets []int) int {
161+
n := len(fruits)
162+
ans := n
163+
vis := make([]bool, n)
164+
for _, x := range fruits {
165+
for i, y := range baskets {
166+
if y >= x && !vis[i] {
167+
vis[i] = true
168+
ans--
169+
break
170+
}
171+
}
172+
}
173+
return ans
174+
}
175+
```
108176

177+
#### TypeScript
178+
179+
```ts
180+
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
181+
const n = fruits.length;
182+
const vis: boolean[] = Array(n).fill(false);
183+
let ans = n;
184+
for (const x of fruits) {
185+
for (let i = 0; i < n; ++i) {
186+
if (baskets[i] >= x && !vis[i]) {
187+
vis[i] = true;
188+
--ans;
189+
break;
190+
}
191+
}
192+
}
193+
return ans;
194+
}
109195
```
110196

111197
<!-- tabs:end -->

0 commit comments

Comments
 (0)