python C++遞迴函數解Leetcode779 K th Symbol in Grammar
[code on Leetcode]https://leetcode.com/problems/k-th-symbol-in-grammar/solutions/4205351/c-c-python-recursion-independent-of-n-one-line-0ms-beats-100/
Python code請看
class Solution:
def kthGrammar(self, n: int, k: int) -> int:
def f(k):
if k==1: return 0
b=(int)(math.log2(k)) # same as b=k.bit_length()-1
if k==1<<b: return b%2
else: return 1-f(k-(1<<b))
return f(k)
沒有留言:
張貼留言