K-th Symbol in Grammar——LeetCode进阶路
原题链接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进阶路的更多相关文章
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- [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 ...
- [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 ...
- 【leetcode】K-th Symbol in Grammar
题目如下: 解题思路:直接把每行的数据计算出来肯定是不行的,因为N最大是30,那个第N行长度就是2^30次方,这显然不可取.那么就只能找规律了,我采取的是倒推法.例如假如我们要求出第四行第七个元素的值 ...
- [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 ...
- 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa
原文地址:http://www.cnblogs.com/GXZlegend/p/6801470.html 题目描述 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总 ...
- 阿里巴巴 web前端性能优化进阶路
Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...
- 洛谷P2045 K方格取数(算竞进阶习题)
费用流 又是一道网络流的模型,对于这种费用与经过次数有关的边,我们经常把边拆成多条,比如这个题,第一次费用是x,第二次是0,我们就可以先把点拆成入点和出点,入点和出点又连两条边,第一条容量为1,费用为 ...
- 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 ...
- 779. K-th Symbol in Grammar
class Solution { public: int kthGrammar(int N, int K) { return helper(N, K, false); } int helper(int ...
随机推荐
- mybatis - [04] mapper文件详解
Mybatis的Mapper文件(通常是以.xml为扩展名的文件)主要用于定义SQL语句和它们与Java接口方法之间的映射关系.以下是Mapper文件中一些常用的配置元素和属性. 一.mapper ...
- 【攻防世界】BadProgrammer
BadProgrammer(原型链污染) 题目来源 攻防世界 NO.GFSJ0986 题目描述 打开网址页面如下,没有什么有用信息 用dirsearch扫一下目录,发现/static../(用御剑扫不 ...
- 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
#div_digg { float: right; font-size: 12px; margin: 10px; text-align: center; width: 120px; position: ...
- glib-2.60在win64,msys2下编译
前阵子,工作原因,需要在win7 64下的msys2来编译glib,下面是一些踩过的坑: 事先声明一下,这些个解决方式及纯粹是为了编译通过,可能有些做法不太适合一些需要正常使用的场合,烦请各位注意下. ...
- go cobra实例讲解
概述 cobra 库是 golang 的一个开源第三方库,能够快速便捷的建立命令行应用程序. 优势:cobra 可以快速建立CLI程序,使我们更专注于命令需要处理的具体的业务逻辑. 举两个例子: hu ...
- 【离线地图】地图瓦片css复杂滤镜线段绘制
需求: 目前已经对地图瓦片做了复杂滤镜的黑夜展示,现在又要在这个图片上绘制新的线段等内容,且不能被这个复杂滤镜影响,变成奇奇怪怪的颜色. 同时因为框架限制,只能在这个img上绘制 思考: 1.既然不想 ...
- 变异凯撒-python脚本调整ascii码转字符串
题目: 加密密文:afZ_r9VYfScOeO_UL^RWUc 格式:flag{ } 结合题目变异凯撒,第一个字符a到f加了5,第二个字符f到l加了6,推断每个字符都在前一个字符基础上+1. 编写py ...
- 堆排序(topk 问题)(NB)
博客地址:https://www.cnblogs.com/zylyehuo/ # _*_coding:utf-8_*_ # 比较排序 import random def sift(li, low, h ...
- 04 详解”死亡ReLU“问题
本篇文章尝试通过具体的神经网络例子,来深入探讨"死亡ReLU"的问题. 很多资料都会提到神经元"永久性死亡"这种说法,我认为这会对我们的理解产生一定的误解.事实 ...
- Delphi Richedit代码语法加亮显示
procedure CodeColors(Form : TForm;Style : String; RichE : TRichedit;InVisible : Boolean); const // s ...