On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.

Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).

Examples:
Input: N = 1, K = 1
Output: 0 Input: N = 2, K = 1
Output: 0 Input: N = 2, K = 2
Output: 1 Input: N = 4, K = 5
Output: 1 Explanation:
row 1: 0
row 2: 01
row 3: 0110
row 4: 01101001

Note:

  1. N will be an integer in the range [1, 30].
  2. K will be an integer in the range [1, 2^(N-1)].

思路:

递归即可,判断当前的k是奇数还是偶数,如果是偶数,那么再查看上一行是1还是0即可。k是奇数同理。

int kthGrammar(int N, int K) {
if(N == ) return ;
if(K % == ) return (kthGrammar(N - , K / ) == ) ? : ;
else return (kthGrammar(N - , (K+) / ) == ) ? : ;
}

[leetcode-779-K-th Symbol in Grammar]的更多相关文章

  1. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  2. [链表]LeetCode 25 K组一个翻转链表

    LeetCode 25 k组一个翻转链表 TITLE 示例 1: 输入:head = [1,2,3,4,5], k = 2 输出:[2,1,4,3,5] 示例 2: 输入:head = [1,2,3, ...

  3. [LeetCode] K-th Symbol in Grammar 语法中的第K个符号

    On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace ...

  4. [Swift]LeetCode779. 第K个语法符号 | K-th Symbol in Grammar

    On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace ...

  5. Java实现 LeetCode 779 第K个语法符号(递归)

    779. 第K个语法符号 在第一行我们写上一个 0.接下来的每一行,将前一行中的0替换为01,1替换为10. 给定行数 N 和序数 K,返回第 N 行中第 K个字符.(K从1开始) 例子: 输入: N ...

  6. 779. K-th Symbol in Grammar

    class Solution { public: int kthGrammar(int N, int K) { return helper(N, K, false); } int helper(int ...

  7. 【leetcode】K-th Symbol in Grammar

    题目如下: 解题思路:直接把每行的数据计算出来肯定是不行的,因为N最大是30,那个第N行长度就是2^30次方,这显然不可取.那么就只能找规律了,我采取的是倒推法.例如假如我们要求出第四行第七个元素的值 ...

  8. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  9. [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  10. [LeetCode] Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

随机推荐

  1. 【转载】JavaScript导出Excel

    [转载]JavaScript导出Excel 原文地址 如果没有用到前端插件,也没有用到后台poi导出的话,用js导出也是一种方式.亲测可用. /** * 导出excel */ var idTmr; f ...

  2. 3. HTML中的容器标签

    什么是容器标签?在HTML开发中我们常常会使用一类标签作为容器放置一些内容,我们把这类标签称之为容器标签,可以作为容器标签的包括列表标签.表格标签.框架标签.布局标签,在这里我们就来总结下这些内容. ...

  3. Redis总导航目录

    NoSQL入门和概述 NoSQL入门概述 3V + 3高 当下的NoSQL经典应用 NoSQL数据模型简介 NoSQL数据库的四大分类 在分布式数据库中CAP原理CAP+BASE Redis入门介绍 ...

  4. IAP笔记

    1)   首先是IAP BootLoader程序设置:根据common.h里面的宏定义,设置BootLoader所占用的Flash空间. 2)   设置IAP UserApp程序设置:该型号Flash ...

  5. docker 操作命令

    sudo docker images   查看所有原有docker sudo docker ps  查看已启动的docker sudo docker start 3003 (stop , docker ...

  6. 北京Uber优步司机奖励政策(1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. 成都Uber优步司机奖励政策(3月22日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 聊聊WS-Federation

    本文来自网易云社区 单点登录(Single Sign On),简称为 SSO,目前已经被大家所熟知.简单的说, 就是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 举例: 我们 ...

  9. thinkphp5

    分页: thinkphp5分页默认只带page参数 在使用form表单method='get'传递关键字来筛选: 保证每次刷新依旧带上筛选参数 但遇到分页时,下面的分页默认自带page,没有之前筛选的 ...

  10. C#新特性记录

    C#6.0新特性笔记 Getter专属赋值 可以在构造函数中,给只有get的属性赋初始值. class Point { public int x { get; } public Point() { x ...