码迷,mamicode.com
首页 > 其他好文 > 详细

779. K-th Symbol in Grammar

时间:2018-05-06 14:52:17      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:root   else   leetcode   div   ret   ble   tps   turn   symbol   

https://leetcode.com/problems/k-th-symbol-in-grammar/description/

class Solution {
public:
    int kthGrammar(int N, int K, bool rootZero = true) {
        if (N == 1)     return rootZero ? 0 : 1;
        int lastCnt = 1 << (N-2);
        if (K <= lastCnt) {
            return kthGrammar(N-1, K, rootZero);
        }
        else {
            return kthGrammar(N-1, K-lastCnt, !rootZero);
        }
    }
};

 

779. K-th Symbol in Grammar

标签:root   else   leetcode   div   ret   ble   tps   turn   symbol   

原文地址:https://www.cnblogs.com/JTechRoad/p/8997921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!