原题链接https://leetcode.com/problems/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 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:

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

思路分析

第一行给定为0,之后行依照把0换为01,1换为10的规律

  • 利用大一数据结构学的二叉树,可把规律理解为0的左右子树分别为0和1,1的子树分别为1和0
  • all in all,
    • 判断所求的节点是左子节点还是右子节点
    • 判断他的父节点是0还是1
      //这是一道爆炸友善的题哇ヾ(✿゚▽゚)ノ

AC解

class Solution {
public int kthGrammar(int N, int K) {
if(K == 1)
{
return 0;
}
if(N == 1)
{
return 0;
} if(K % 2 == 0)
{
return kthGrammar(N-1,K/2)==0 ? 1 : 0;
}
else
{
return kthGrammar(N-1,(K+1)/2)==0 ? 0 : 1;
}
}
}

K-th Symbol in Grammar——LeetCode进阶路的更多相关文章

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

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

  2. [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 ...

  3. [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 ...

  4. 【leetcode】K-th Symbol in Grammar

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

  5. [leetcode-779-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 ...

  6. 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801470.html 题目描述 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总 ...

  7. 阿里巴巴 web前端性能优化进阶路

    Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...

  8. 洛谷P2045 K方格取数(算竞进阶习题)

    费用流 又是一道网络流的模型,对于这种费用与经过次数有关的边,我们经常把边拆成多条,比如这个题,第一次费用是x,第二次是0,我们就可以先把点拆成入点和出点,入点和出点又连两条边,第一条容量为1,费用为 ...

  9. Find K Pairs with Smallest Sums -- LeetCode

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

  10. 779. K-th Symbol in Grammar

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

随机推荐

  1. MySQL - [10] 时间处理函数

    题记部分 (1)获取当前日期时间:select current_date; (2)获取当前时间戳:select current_timestamp; (3)返回日期中的年/季度/月/日/时/分/秒 s ...

  2. Halcon2DMeasure常用算子

    1.create_metrology_model()  create_metrology_model( : : : MetrologyHandle)  函数说明: 创建测量几何图形所需的数据结构/模型 ...

  3. php用token做登录认证

    https://blog.csdn.net/qq_20869933/article/details/133201967 作用: PHP 使用token验证可有效的防止非法来源数据提交访问,增加数据操作 ...

  4. Unable to Connect: sPort: 0 C# ServiceStack.Redis 访问 redis

    需求:  对数据库中的不断抓取的文章进行缓存,因此需要定时访问数据,写入缓存中 在捕获到的异常日志发现错误:Unable to Connect: sPort: 0 使用的访问方式是线程池的方式:Poo ...

  5. pycharm clone GitHub 提示 OpenSSL SSL_read: Connection was reset, errno 10054

    配置界面 错误提示 原因分析 clone的时候需要安全认证,当你在配置页面勾选上ssh ,就会报错 解决方案 在cmd里输入命令,然后再clone git config --global http.s ...

  6. 【由技及道】量子跃迁部署术:docker+jenkins+Harbor+SSH的十一维交付矩阵【人工智障AI2077的开发日志011】

    摘要: SSH密钥对构建的十一维安全通道 × Harbor镜像星门 × 错误吞噬者语法糖 = 在CI/CD的量子观测中实现熵减永动机,使容器在部署前保持开发与生产维度的叠加态 量子纠缠现状(技术背景) ...

  7. jquery的radio的change事件

    一.用的jquery的radio的change事件:当元素的值发生改变时,会发生 change 事件,radio选择不同name值选项的时候恰巧是值发生改变 表单单选框 <input type= ...

  8. 【Ubuntu】在Ubuntu上安装IDEA

    [Ubuntu]在Ubuntu上安装IDEA 零.前言 最近换了Ubuntu系统,但是还得是要写代码,这样就不可避免地用到IDEA,接下来介绍一下如何在Ubuntu上安装IDEA. 壹.下载 这一步应 ...

  9. 【网络攻防】ARP欺骗实验

    实验概述 ARP欺骗是一类地址欺骗类病毒,属于木马病毒,自身不具备主动传播的特性,不会自我复制.但是由于其发作的时候会不断向全网发送伪造的ARP数据包,导致网络无法正常运行,严重的甚至可能带来整个网络 ...

  10. Debian 下安装 Nginx

    Debian 下安装 Nginx 非常容易. apt update apt install nginx 输入以下命令查看是否可以正常访问, 顺便验证下安装是否成功. curl -I 127.0.0.1 ...