Skip to content

Add solution and test-cases for problem 600 #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [600.Non-negative Integers without Consecutive Ones][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given a positive integer `n`, return the number of the integers in the range `[0, n]` whose binary representations **do not** contain consecutive ones.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 5
Output: 5
Explanation:
Here are the non-negative integers <= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Non-negative Integers without Consecutive Ones
```go
```
Input: n = 1
Output: 2
```

**Example 3:**

```
Input: n = 2
Output: 3
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
package Solution

func Solution(x bool) bool {
return x
func nBit(n int) ([]int, int) {
a := make([]int, 32)
i := 31
for n > 0 {
a[i] = n & 1
n >>= 1
i--
}
return a, i + 1
}

func dfsBit(preBit, index int, less bool, array []int) int {
if index == 32 {
return 1
}
if preBit == 1 {
if array[index] == 1 {
return dfsBit(0, index+1, true, array)
}
return dfsBit(0, index+1, less, array)
}
if array[index] == 1 {
return dfsBit(1, index+1, less, array) + dfsBit(0, index+1, true, array)
}
a := dfsBit(0, index+1, less, array)
if less {
a += dfsBit(1, index+1, less, array)
}
return a
}

func Solution(n int) int {
if n <= 2 {
return n + 1
}
bits, startIndex := nBit(n)
cnt := 0
zero, one := 1, 1
for i := 30; i > startIndex; i-- {
tmpZero := zero + one
tmpOne := zero
zero, one = tmpZero, tmpOne
}
cnt += zero + one

return cnt + dfsBit(1, startIndex+1, false, bits)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 5, 5},
{"TestCase2", 1, 2},
{"TestCase3", 2, 3},
{"TestCase4", 999999995, 2178309},
}

// 开始测试
Expand All @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading