File tree Expand file tree Collapse file tree 3 files changed +77
-9
lines changed
leetcode/1401-1500/1404.Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One Expand file tree Collapse file tree 3 files changed +77
-9
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 1
1
package Solution
2
2
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
5
26
}
Original file line number Diff line number Diff line change @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
10
10
// 测试用例
11
11
cases := []struct {
12
12
name string
13
- inputs bool
14
- expect bool
13
+ inputs string
14
+ expect int
15
15
}{
16
- {"TestCase " , true , true },
17
- {"TestCase " , true , true },
18
- {"TestCase " , false , false },
16
+ {"TestCase1 " , "1101" , 6 },
17
+ {"TestCase2 " , "10" , 1 },
18
+ {"TestCase3 " , "1" , 0 },
19
19
}
20
20
21
21
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
30
30
}
31
31
}
32
32
33
- // 压力测试
33
+ // 压力测试
34
34
func BenchmarkSolution (b * testing.B ) {
35
35
}
36
36
37
- // 使用案列
37
+ // 使用案列
38
38
func ExampleSolution () {
39
39
}
You can’t perform that action at this time.
0 commit comments