Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6934f92

Browse files
update: 779
1 parent 2049b89 commit 6934f92

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,4 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
144144
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [JavaScript](./src/continuous-subarray-sum/res.js)|Medium|
145145
|539|[Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/) | [JavaScript](./src/minimum-time-difference/res.js)|Medium|
146146
|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | [JavaScript](./src/longest-univalue-path/res.js)|Easy|
147+
|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/) | [JavaScript](./src/k-th-symbol-in-grammar/res.js)|Medium|

‎src/k-th-symbol-in-grammar/res.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} N
3+
* @param {number} K
4+
* @return {number}
5+
*/
6+
var kthGrammar = function(N, K) {
7+
if (N === 1) return 0;
8+
if (N === 2) return K - 1;
9+
10+
const index = Math.pow(2, N - 1);
11+
12+
if (K <= index / 2) {
13+
return kthGrammar(N - 1, K);
14+
} else {
15+
return 1 - kthGrammar(N - 1, K - index / 2);
16+
}
17+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /