[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 ...
随机推荐
- java读取xml文件的四种方法
Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> ...
- MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题
参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...
- 字符编码ascii、unicode、utf-‐8、gbk 的关系
ASIIC码: 计算机是美国人发明和最早使用的,他们为了解决计算机处理字符串的问题,就将数字字母和一些常用的符号做成了一套编码,这个编码就是ASIIC码.ASIIC码包括数字大小写字母和常用符号,一共 ...
- vscode调试html文件
1. vscode调试html文件 1.1. 使用Debugger for Chrome进行调试 1.1.1. 基于本地file配置方式调试 1.1.2. 基于服务端配置方式调试 1.1.2.1. 启 ...
- JavaScript 时间对象 date()
getYear() 获得的是距离1900年过了多少年 var d = new Date(); document.write(d+"<br />"); document. ...
- 万恶的a标签
相信很多人碰见过这些问题吧 给某个a标签套的元素中添加点击事件 在外面就能获取到但是点击事件不生效把 或者在页面中点击一个a标签元素发现页面返回了最顶端 然后就开始郁闷了 哈哈 其实这些看似神奇的 ...
- vscode vue 项目保存运行lint进行代码修正
{ "editor.tabSize": 2, "files.associations": { "*.vue": "vue" ...
- wdcp v3 pureftpd 无法登录问题解决
wdcp v3 新建站点和ftp账号 单位无法登录ftp 在端口中也确实可以看到有进行在登录状态 错误原因: 防火墙端口没有开启该端口范围 20000-30000 这时候发现 改端口为20078 ...
- 关于NPOI导出excel文件(xls和xlsx两种格式)提示格式不符的问题
这两天在做导出excel文件的时候遇到这个问题 本来我导出的格式是xlsx格式的,但是下载得到的文件格式变成了xls, 一开始以为是返回的contenttype设置错了 return File(ms, ...
- 使用TryParse()来执行数值转换
static void Main() { var ageText = "25"; if (int.TryParse(ageText,out int age)) { Console. ...