美文网首页
779. K-th Symbol in Grammar

779. K-th Symbol in Grammar

作者: chaowwwww | 来源:发表于2018-04-09 15:16 被阅读0次

开始想到用模拟的方法去解题,很容易超时。
换了递归后解决了问题。

class Solution:
    def kthGrammar(self, N, K):
        """
        :type N: int
        :type K: int
        :rtype: int
        """
        if N == 1:
            return 0
        pro = self.kthGrammar(N - 1, (K - 1) // 2 + 1)
        odd = K % 2
        if pro == 0:
            if odd == 1:
                ans = 0
            else:
                ans = 1
        else:
            if odd == 1:
                ans = 1
            else:
                ans = 0
        #print(N, K, ans)
        return ans

相关文章

网友评论

      本文标题:779. K-th Symbol in Grammar

      本文链接:https://www.haomeiwen.com/subject/ztlihftx.html