File tree Expand file tree Collapse file tree 6 files changed +123
-2
lines changed
solution/0700-0799/0779.K-th Symbol in Grammar Expand file tree Collapse file tree 6 files changed +123
-2
lines changed Original file line number Diff line number Diff line change 63
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
64
65
65
``` python
66
-
66
+ class Solution :
67
+ def kthGrammar (self , n : int , k : int ) -> int :
68
+ if n == 1 :
69
+ return 0
70
+ if k <= (1 << (n - 2 )):
71
+ return self .kthGrammar(n - 1 , k)
72
+ return self .kthGrammar(n - 1 , k - (1 << (n - 2 ))) ^ 1
67
73
```
68
74
69
75
### ** Java**
70
76
71
77
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
78
73
79
``` java
80
+ class Solution {
81
+ public int kthGrammar (int n , int k ) {
82
+ if (n == 1 ) {
83
+ return 0 ;
84
+ }
85
+ if (k <= (1 << (n - 2 ))) {
86
+ return kthGrammar(n - 1 , k);
87
+ }
88
+ return kthGrammar(n - 1 , k - (1 << (n - 2 ))) ^ 1 ;
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### ** C++**
94
+
95
+ ``` cpp
96
+ class Solution {
97
+ public:
98
+ int kthGrammar(int n, int k) {
99
+ if (n == 1) return 0;
100
+ if (k <= (1 << (n - 2))) return kthGrammar(n - 1, k);
101
+ return kthGrammar(n - 1, k - (1 << (n - 2))) ^ 1;
102
+ }
103
+ };
104
+ ```
74
105
106
+ ### **Go**
107
+
108
+ ```go
109
+ func kthGrammar(n int, k int) int {
110
+ if n == 1 {
111
+ return 0
112
+ }
113
+ if k <= (1 << (n - 2)) {
114
+ return kthGrammar(n-1, k)
115
+ }
116
+ return kthGrammar(n-1, k-(1<<(n-2))) ^ 1
117
+ }
75
118
```
76
119
77
120
### ** ...**
Original file line number Diff line number Diff line change @@ -56,13 +56,56 @@ row 2: 0<u>1</u>
56
56
### ** Python3**
57
57
58
58
``` python
59
-
59
+ class Solution :
60
+ def kthGrammar (self , n : int , k : int ) -> int :
61
+ if n == 1 :
62
+ return 0
63
+ if k <= (1 << (n - 2 )):
64
+ return self .kthGrammar(n - 1 , k)
65
+ return self .kthGrammar(n - 1 , k - (1 << (n - 2 ))) ^ 1
60
66
```
61
67
62
68
### ** Java**
63
69
64
70
``` java
71
+ class Solution {
72
+ public int kthGrammar (int n , int k ) {
73
+ if (n == 1 ) {
74
+ return 0 ;
75
+ }
76
+ if (k <= (1 << (n - 2 ))) {
77
+ return kthGrammar(n - 1 , k);
78
+ }
79
+ return kthGrammar(n - 1 , k - (1 << (n - 2 ))) ^ 1 ;
80
+ }
81
+ }
82
+ ```
83
+
84
+ ### ** C++**
85
+
86
+ ``` cpp
87
+ class Solution {
88
+ public:
89
+ int kthGrammar(int n, int k) {
90
+ if (n == 1) return 0;
91
+ if (k <= (1 << (n - 2))) return kthGrammar(n - 1, k);
92
+ return kthGrammar(n - 1, k - (1 << (n - 2))) ^ 1;
93
+ }
94
+ };
95
+ ```
65
96
97
+ ### **Go**
98
+
99
+ ```go
100
+ func kthGrammar(n int, k int) int {
101
+ if n == 1 {
102
+ return 0
103
+ }
104
+ if k <= (1 << (n - 2)) {
105
+ return kthGrammar(n-1, k)
106
+ }
107
+ return kthGrammar(n-1, k-(1<<(n-2))) ^ 1
108
+ }
66
109
```
67
110
68
111
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int kthGrammar (int n, int k) {
4
+ if (n == 1 ) return 0 ;
5
+ if (k <= (1 << (n - 2 ))) return kthGrammar (n - 1 , k);
6
+ return kthGrammar (n - 1 , k - (1 << (n - 2 ))) ^ 1 ;
7
+ }
8
+ };
Original file line number Diff line number Diff line change
1
+ func kthGrammar (n int , k int ) int {
2
+ if n == 1 {
3
+ return 0
4
+ }
5
+ if k <= (1 << (n - 2 )) {
6
+ return kthGrammar (n - 1 , k )
7
+ }
8
+ return kthGrammar (n - 1 , k - (1 << (n - 2 ))) ^ 1
9
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int kthGrammar (int n , int k ) {
3
+ if (n == 1 ) {
4
+ return 0 ;
5
+ }
6
+ if (k <= (1 << (n - 2 ))) {
7
+ return kthGrammar (n - 1 , k );
8
+ }
9
+ return kthGrammar (n - 1 , k - (1 << (n - 2 ))) ^ 1 ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def kthGrammar (self , n : int , k : int ) -> int :
3
+ if n == 1 :
4
+ return 0
5
+ if k <= (1 << (n - 2 )):
6
+ return self .kthGrammar (n - 1 , k )
7
+ return self .kthGrammar (n - 1 , k - (1 << (n - 2 ))) ^ 1
You can’t perform that action at this time.
0 commit comments