[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 5.7组复制技术(Group Replication)
Group Replication is know as an up to date HA(High Availablity) solution which is supported in ...
- 前端String转json
1.data = eval("("+data+")");2.JSON.parse(data);
- WEB中需求分析应该考虑的问题
一. 针对用户群体要考虑因素 1.用户年龄 2.选择素材 3.网站布局 4.颜色搭配 5. 用户体验及动效 6.功能便捷 用户需求.用户兴趣爱好.性格.职业.教育水平高低.消费观念.PC端和移动端哪一 ...
- Mysql 关于处理NULL值的相关函数和操作符
操作符 <=> NULL-safe equal. This operator performs an equality comparison like the = operator, bu ...
- Java : Spring基础 IOC
使用 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" ...
- [原创]利用python发送伪造的ARP请求
#!/usr/bin/env python import socket s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) s.bind((&qu ...
- 汇编中resb这样的指令是什么意思?
转载下来,方便以后查看 原作网址:http://blog.csdn.net/m1j2t3/article/details/5681657 汇编中resb这样的指令是什么意思? 还有我在汇编程序中看到这 ...
- nodejs module/require
1. wrap up a module using module.exports, name the file to a.js var fun1=function(){ var stylus = re ...
- 北京Uber优步司机奖励政策(4月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 如何理解 UL94HB , UL94-V0 , UL94-V1 , UL94-V2
塑料阻燃等级由HB,V-2,V-1向V-0逐级递增: UL94V0,V1,V2是不同的阻燃等级,其等级不同,耐燃的测试方法亦不同,测试判定的标准也不同. V0的测试方法是将测试物倾斜45度,用酒精灯点 ...