diff --git a/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/README.md b/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/README.md index 3a6e9d653..89dd3dfbc 100644 --- a/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/README.md +++ b/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution.go b/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution.go index d115ccf5e..f4dcaadc8 100644 --- a/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution.go +++ b/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution.go @@ -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) } diff --git a/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution_test.go b/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution_test.go index 14ff50eb4..905ea4f10 100644 --- a/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution_test.go +++ b/leetcode/501-600/0600.Non-negative-Integers-without-Consecutive-Ones/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }