#779

K-th Symbol in Grammar

specialist · 905 · lc medium +31 · verified · 48.1% accepted · 4,140 likes · top 34%

Description

A grammar table is built row by row: the first row is "0", and each subsequent row is produced by replacing every 0 with "01" and every 1 with "10". For example, row 3 is "0110".

Given integers n and k (1-indexed), return the kth character in the nth row.

Example 1:

Input: n = 1, k = 1
Output: 0
Explanation: row 1: 0

Example 2:

Input: n = 2, k = 1
Output: 0
Explanation:
row 1: 0
row 2: 01

Example 3:

Input: n = 2, k = 2
Output: 1
Explanation:
row 1: 0
row 2: 01

Code

1
2
3