Skip to content

Commit 1b96a5f

Browse files
committed
Add solution and test-cases for problem 600
1 parent d569e0c commit 1b96a5f

File tree

3 files changed

+76
-22
lines changed

3 files changed

+76
-22
lines changed

leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [600.Non-negative Integers without Consecutive Ones][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a positive integer `n`, return the number of the integers in the range `[0, n]` whose binary representations **do not** contain consecutive ones.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: n = 5
10+
Output: 5
11+
Explanation:
12+
Here are the non-negative integers <= 5 with their corresponding binary representations:
13+
0 : 0
14+
1 : 1
15+
2 : 10
16+
3 : 11
17+
4 : 100
18+
5 : 101
19+
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
1320
```
1421

15-
## 题意
16-
> ...
17-
18-
## 题解
22+
**Example 2:**
1923

20-
### 思路1
21-
> ...
22-
Non-negative Integers without Consecutive Ones
23-
```go
24+
```
25+
Input: n = 1
26+
Output: 2
2427
```
2528

29+
**Example 3:**
30+
31+
```
32+
Input: n = 2
33+
Output: 3
34+
```
2635

2736
## 结语
2837

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func nBit(n int) ([]int, int) {
4+
a := make([]int, 32)
5+
i := 31
6+
for n > 0 {
7+
a[i] = n & 1
8+
n >>= 1
9+
i--
10+
}
11+
return a, i + 1
12+
}
13+
14+
func dfsBit(preBit, index int, less bool, array []int) int {
15+
if index == 32 {
16+
return 1
17+
}
18+
if preBit == 1 {
19+
if array[index] == 1 {
20+
return dfsBit(0, index+1, true, array)
21+
}
22+
return dfsBit(0, index+1, less, array)
23+
}
24+
if array[index] == 1 {
25+
return dfsBit(1, index+1, less, array) + dfsBit(0, index+1, true, array)
26+
}
27+
a := dfsBit(0, index+1, less, array)
28+
if less {
29+
a += dfsBit(1, index+1, less, array)
30+
}
31+
return a
32+
}
33+
34+
func Solution(n int) int {
35+
if n <= 2 {
36+
return n + 1
37+
}
38+
bits, startIndex := nBit(n)
39+
cnt := 0
40+
zero, one := 1, 1
41+
for i := 30; i > startIndex; i-- {
42+
tmpZero := zero + one
43+
tmpOne := zero
44+
zero, one = tmpZero, tmpOne
45+
}
46+
cnt += zero + one
47+
48+
return cnt + dfsBit(1, startIndex+1, false, bits)
549
}

leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution_test.go

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

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)