Skip to content

Commit 2602b80

Browse files
authored
feat: add solutions for lc No.1404 (#5051)
1 parent 3c35e90 commit 2602b80

5 files changed

Lines changed: 234 additions & 10 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - `双指针`
5353
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - `位运算``lowbit`
5454
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - `区间合并`
55-
<!-- 排序算法、待补充 -->
5655

5756
### 2. 数据结构
5857

solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/README.md

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ tags:
4444
Step 1) 13 是奇数,加 1 得到 14&nbsp;
4545
Step 2) 14 是偶数,除 2 得到 7
4646
Step 3) 7 是奇数,加 1 得到 8
47-
Step 4) 8 是偶数,除 2 得到 4&nbsp;
47+
Step 4) 8 是偶数,除 2 得到 4&nbsp;
4848
Step 5) 4 是偶数,除 2 得到 2&nbsp;
49-
Step 6) 2 是偶数,除 2 得到 1&nbsp;
49+
Step 6) 2 是偶数,除 2 得到 1&nbsp;
5050
</pre>
5151

5252
<p><strong>示例 2:</strong></p>
@@ -55,7 +55,7 @@ Step 6) 2 是偶数,除 2 得到 1&nbsp;
5555
<strong>输入:</strong>s = "10"
5656
<strong>输出:</strong>1
5757
<strong>解释:</strong>"10" 表示十进制数 2 。
58-
Step 1) 2 是偶数,除 2 得到 1
58+
Step 1) 2 是偶数,除 2 得到 1
5959
</pre>
6060

6161
<p><strong>示例 3:</strong></p>
@@ -81,9 +81,17 @@ Step 1) 2 是偶数,除 2 得到 1
8181

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

84-
### 方法一:模拟操作
84+
### 方法一:模拟
8585

86-
我们模拟操作 $1$ 和 $2$,同时用 carry 记录进位。
86+
我们模拟操作 $1$ 和 $2$,同时维护一个进位 $\textit{carry}$ 来表示当前是否有进位,初始时 $\textit{carry} = \text{false}$。
87+
88+
我们从字符串 $s$ 的末尾开始向前遍历:
89+
90+
- 如果 $\textit{carry}$ 为 $\text{true}$,则当前位 $c$ 需要加 $1$,如果 $c$ 是 $0$,则加 $1$ 后变为 $1$,同时 $\textit{carry}$ 变为 $\text{false}$;如果 $c$ 是 $1$,则加 $1$ 后变为 $0$,同时 $\textit{carry}$ 保持为 $\text{true}$。
91+
- 如果 $c$ 是 $1$,则需要执行操作 $1$,即加 $1$,同时 $\textit{carry}$ 变为 $\text{true}$。
92+
- 此时 $c$ 是 $0$,则需要执行操作 $2$,即除以 $2$。
93+
94+
当遍历结束后,如果 $\textit{carry}$ 仍然为 $\text{true}$,则需要再执行一次操作 $1$。
8795

8896
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
8997

@@ -201,6 +209,79 @@ func numSteps(s string) int {
201209
}
202210
```
203211

212+
#### TypeScript
213+
214+
```ts
215+
function numSteps(s: string): number {
216+
let ans = 0;
217+
let carry = false;
218+
219+
for (let i = s.length - 1; i > 0; i--) {
220+
let c = s[i];
221+
222+
if (carry) {
223+
if (c === '0') {
224+
c = '1';
225+
carry = false;
226+
} else {
227+
c = '0';
228+
}
229+
}
230+
231+
if (c === '1') {
232+
ans++;
233+
carry = true;
234+
}
235+
236+
ans++;
237+
}
238+
239+
if (carry) {
240+
ans++;
241+
}
242+
243+
return ans;
244+
}
245+
```
246+
247+
#### Rust
248+
249+
```rust
250+
impl Solution {
251+
pub fn num_steps(s: String) -> i32 {
252+
let bytes = s.as_bytes();
253+
let mut ans: i32 = 0;
254+
let mut carry = false;
255+
256+
for i in (1..bytes.len()).rev() {
257+
let mut c = bytes[i];
258+
259+
if carry {
260+
if c == b'0' {
261+
c = b'1';
262+
carry = false;
263+
} else {
264+
c = b'0';
265+
}
266+
}
267+
268+
if c == b'1' {
269+
ans += 1;
270+
carry = true;
271+
}
272+
273+
ans += 1;
274+
}
275+
276+
if carry {
277+
ans += 1;
278+
}
279+
280+
ans
281+
}
282+
}
283+
```
284+
204285
<!-- tabs:end -->
205286

206287
<!-- solution:end -->

solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/README_EN.md

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ tags:
4343
Step 1) 13 is odd, add 1 and obtain 14.&nbsp;
4444
Step 2) 14 is even, divide by 2 and obtain 7.
4545
Step 3) 7 is odd, add 1 and obtain 8.
46-
Step 4) 8 is even, divide by 2 and obtain 4.&nbsp;
46+
Step 4) 8 is even, divide by 2 and obtain 4.&nbsp;
4747
Step 5) 4 is even, divide by 2 and obtain 2.&nbsp;
48-
Step 6) 2 is even, divide by 2 and obtain 1.&nbsp;
48+
Step 6) 2 is even, divide by 2 and obtain 1.&nbsp;
4949
</pre>
5050

5151
<p><strong class="example">Example 2:</strong></p>
@@ -54,7 +54,7 @@ Step 6) 2 is even, divide by 2 and obtain 1.&nbsp;
5454
<strong>Input:</strong> s = &quot;10&quot;
5555
<strong>Output:</strong> 1
5656
<strong>Explanation:</strong> &quot;10&quot; corresponds to number 2 in their decimal representation.
57-
Step 1) 2 is even, divide by 2 and obtain 1.&nbsp;
57+
Step 1) 2 is even, divide by 2 and obtain 1.&nbsp;
5858
</pre>
5959

6060
<p><strong class="example">Example 3:</strong></p>
@@ -81,7 +81,15 @@ Step 1) 2 is even, divide by 2 and obtain 1.&nbsp;
8181

8282
### Solution 1: Simulation
8383

84-
We simulate operations $1$ and $2$, while using `carry` to record the carry-over.
84+
We simulate operations $1$ and $2$, while maintaining a carry $\textit{carry}$ to indicate whether there is a current carry. Initially, $\textit{carry} = \text{false}$.
85+
86+
We traverse the string $s$ from the end toward the beginning:
87+
88+
- If $\textit{carry}$ is $\text{true}$, the current bit $c$ needs to be incremented by $1$. If $c$ is $0$, it becomes $1$ after adding $1$, and $\textit{carry}$ becomes $\text{false}$; if $c$ is $1$, it becomes $0$ after adding $1$, and $\textit{carry}$ remains $\text{true}$.
89+
- If $c$ is $1$, we need to perform operation $1$, i.e., add $1$, and $\textit{carry}$ becomes $\text{true}$.
90+
- At this point $c$ is $0$, so we need to perform operation $2$, i.e., divide by $2$.
91+
92+
After the traversal, if $\textit{carry}$ is still $\text{true}$, we need to perform operation $1$ one more time.
8593

8694
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
8795

@@ -199,6 +207,79 @@ func numSteps(s string) int {
199207
}
200208
```
201209

210+
#### TypeScript
211+
212+
```ts
213+
function numSteps(s: string): number {
214+
let ans = 0;
215+
let carry = false;
216+
217+
for (let i = s.length - 1; i > 0; i--) {
218+
let c = s[i];
219+
220+
if (carry) {
221+
if (c === '0') {
222+
c = '1';
223+
carry = false;
224+
} else {
225+
c = '0';
226+
}
227+
}
228+
229+
if (c === '1') {
230+
ans++;
231+
carry = true;
232+
}
233+
234+
ans++;
235+
}
236+
237+
if (carry) {
238+
ans++;
239+
}
240+
241+
return ans;
242+
}
243+
```
244+
245+
#### Rust
246+
247+
```rust
248+
impl Solution {
249+
pub fn num_steps(s: String) -> i32 {
250+
let bytes = s.as_bytes();
251+
let mut ans: i32 = 0;
252+
let mut carry = false;
253+
254+
for i in (1..bytes.len()).rev() {
255+
let mut c = bytes[i];
256+
257+
if carry {
258+
if c == b'0' {
259+
c = b'1';
260+
carry = false;
261+
} else {
262+
c = b'0';
263+
}
264+
}
265+
266+
if c == b'1' {
267+
ans += 1;
268+
carry = true;
269+
}
270+
271+
ans += 1;
272+
}
273+
274+
if carry {
275+
ans += 1;
276+
}
277+
278+
ans
279+
}
280+
}
281+
```
282+
202283
<!-- tabs:end -->
203284

204285
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn num_steps(s: String) -> i32 {
3+
let bytes = s.as_bytes();
4+
let mut ans: i32 = 0;
5+
let mut carry = false;
6+
7+
for i in (1..bytes.len()).rev() {
8+
let mut c = bytes[i];
9+
10+
if carry {
11+
if c == b'0' {
12+
c = b'1';
13+
carry = false;
14+
} else {
15+
c = b'0';
16+
}
17+
}
18+
19+
if c == b'1' {
20+
ans += 1;
21+
carry = true;
22+
}
23+
24+
ans += 1;
25+
}
26+
27+
if carry {
28+
ans += 1;
29+
}
30+
31+
ans
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function numSteps(s: string): number {
2+
let ans = 0;
3+
let carry = false;
4+
5+
for (let i = s.length - 1; i > 0; i--) {
6+
let c = s[i];
7+
8+
if (carry) {
9+
if (c === '0') {
10+
c = '1';
11+
carry = false;
12+
} else {
13+
c = '0';
14+
}
15+
}
16+
17+
if (c === '1') {
18+
ans++;
19+
carry = true;
20+
}
21+
22+
ans++;
23+
}
24+
25+
if (carry) {
26+
ans++;
27+
}
28+
29+
return ans;
30+
}

0 commit comments

Comments
 (0)