[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 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:
Nwill be an integer in the range[1, 30].Kwill 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]的更多相关文章
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- [链表]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, ...
- [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 ...
- Java实现 LeetCode 779 第K个语法符号(递归)
779. 第K个语法符号 在第一行我们写上一个 0.接下来的每一行,将前一行中的0替换为01,1替换为10. 给定行数 N 和序数 K,返回第 N 行中第 K个字符.(K从1开始) 例子: 输入: N ...
- 779. K-th Symbol in Grammar
class Solution { public: int kthGrammar(int N, int K) { return helper(N, K, false); } int helper(int ...
- 【leetcode】K-th Symbol in Grammar
题目如下: 解题思路:直接把每行的数据计算出来肯定是不行的,因为N最大是30,那个第N行长度就是2^30次方,这显然不可取.那么就只能找规律了,我采取的是倒推法.例如假如我们要求出第四行第七个元素的值 ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [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 ...
- [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 ...
随机推荐
- mysql 一看就会 基本语法
创建表 create table <表名>( <字段名> 类型(长度) not null primary key auto_increment, **主键 name char ...
- 洛谷P4525 【模板】自适应辛普森法1(simpson积分)
题目描述 计算积分 结果保留至小数点后6位. 数据保证计算过程中分母不为0且积分能够收敛. 输入输出格式 输入格式: 一行,包含6个实数a,b,c,d,L,R 输出格式: 一行,积分值,保留至小数点后 ...
- wps for linux 安装后系统缺失字体安装配置
错误提示: 解决方法: 从http://bbs.wps.cn/thread-22355435-1-1.html下载字体库,离线版本:(链接: https://pan.baidu.com/s/1i5dz ...
- 解决MacOS升级后出现xcrun: error: invalid active developer path, missing xcrun的问题
升级了系统 命令行不能用了 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), mis ...
- eclipse官方下载地址
下载地址: https://www.eclipse.org/downloads/packages/release/Luna/SR2 选择不同版本
- app后端api设计【转】
博客:https://blog.csdn.net/newjueqi/article/details/44037011 app和后端的交互,一般都是通过后端提供的api实现.api的设计,估计很多刚进入 ...
- 关于Quartz 2D绘图的简单使用
Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境,Quartz 2D的API可以实现许多功能,如:基于路径的绘图.透明度.阴影.颜色管理.反锯齿.PDF文档生成和PDF元 ...
- vue-知乎日志
1.项目API来源 2.项目地址 3.截图 4.功能 首页 轮播图 动态消息 下拉刷新 动态 ...
- xshell安装教程
Xshell安装使用教程 Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议.Xshell 通过互联网到远程主机 ...
- 【Java】集合遍历--List和Map的多种遍历方式
1. List的两种遍历方式 package com.nova.test; import java.util.ArrayList; import java.util.Iterator; import ...