Skip to content

Commit 7212fed

Browse files
authored
Merge pull request #889 from 0xff-dev/1404
Add solution and test-cases for problem 1404
2 parents 31150d9 + 7d8171d commit 7212fed

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [1404.Number of Steps to Reduce a Number in Binary Representation to One][title]
2+
3+
## Description
4+
Given the binary representation of an integer as a string `s`, return the number of steps to reduce it to `1` under the following rules:
5+
6+
- If the current number is even, you have to divide it by `2`.
7+
8+
- If the current number is odd, you have to add `1` to it.
9+
10+
It is guaranteed that you can always reach one for all test cases.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: s = "1101"
16+
Output: 6
17+
Explanation: "1101" corressponds to number 13 in their decimal representation.
18+
Step 1) 13 is odd, add 1 and obtain 14.
19+
Step 2) 14 is even, divide by 2 and obtain 7.
20+
Step 3) 7 is odd, add 1 and obtain 8.
21+
Step 4) 8 is even, divide by 2 and obtain 4.
22+
Step 5) 4 is even, divide by 2 and obtain 2.
23+
Step 6) 2 is even, divide by 2 and obtain 1.
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: s = "10"
30+
Output: 1
31+
Explanation: "10" corressponds to number 2 in their decimal representation.
32+
Step 1) 2 is even, divide by 2 and obtain 1.
33+
```
34+
35+
**Example 3:**
36+
37+
```
38+
Input: s = "1"
39+
Output: 0
40+
```
41+
42+
## 结语
43+
44+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
45+
46+
[title]: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one
47+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
bs := []byte(s)
5+
steps := 0
6+
idx := len(s) - 1
7+
for idx >= 0 {
8+
if string(bs[:idx+1]) == "1" {
9+
break
10+
}
11+
if bs[idx] == '0' {
12+
idx--
13+
steps++
14+
continue
15+
}
16+
steps += 1
17+
for ; idx >= 0 && bs[idx] != '0'; idx-- {
18+
steps++
19+
}
20+
if idx >= 0 {
21+
bs[idx] = '1'
22+
}
23+
}
24+
25+
return steps
526
}

leetcode/1401-1500/1404.Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "1101", 6},
17+
{"TestCase2", "10", 1},
18+
{"TestCase3", "1", 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)