# [路飞]_程序员必刷力扣题: 779. 第K个语法符号 ### 779. 第K个语法符号

``````输入: N = 1, K = 1

1. `N` 的范围 `[1, 30]`.
2. `K` 的范围 `[1, 2^(N-1)]`.

### 获取操作栈

• 暴力求解
• 按照题目要要求，生成每一行的字符串，然后直接获取第k个字符

• 获取操作栈

......

``````var kthGrammar = function (n, k) {
if (n === 1 && k === 1) return '0'
var treeStack = []
while (n > 1) {
var pos = k % 2 === 0 ? 1 : 0
var opt = [n, k, pos]
k = Math.ceil(k / 2)
treeStack.push(opt)
n--
}
var curr = '0'
while (treeStack.length) {
var item = treeStack.pop()
var newCurr = curr === '1' ? '10' : '01'
var index = item
curr = newCurr[index]
}
return curr
};
{"ft_lang_label":"__label__zh","ft_lang_prob":0.8573917,"math_prob":0.9979645,"size":1544,"snap":"2022-05-2022-21","text_gpt3_token_len":1205,"char_repetition_ratio":0.086363636,"word_repetition_ratio":0.07821229,"special_character_ratio":0.34067357,"punctuation_ratio":0.1827957,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9961697,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-22T23:34:15Z\",\"WARC-Record-ID\":\"<urn:uuid:ee4d4e57-60f7-4362-aaeb-70f51ac18ed8>\",\"Content-Length\":\"114851\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:37d6ca77-d52a-4eab-969c-7fda7b59caac>\",\"WARC-Concurrent-To\":\"<urn:uuid:98971d3b-1c47-43fe-810e-c6080f964bb9>\",\"WARC-IP-Address\":\"8.45.176.230\",\"WARC-Target-URI\":\"https://juejin.cn/post/7041594069830598669\",\"WARC-Payload-Digest\":\"sha1:GZX565WN6UUNYAP4CKIHOQIFKNGJ57SN\",\"WARC-Block-Digest\":\"sha1:AUHB5GMDFZSFL2ATVZFVGETUUGG2QLJS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662550298.31_warc_CC-MAIN-20220522220714-20220523010714-00333.warc.gz\"}"}