【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/k-th-symbol-in-grammar/description/
题目描述:
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。求第N行的第K个数是什么。注意K是按照1开始数的。
解题方法
恩,数据空间这么大,一看就是找规律的题目,没想到这么简单就过了。
把题目样例再多写一行:
row 1: 0
row 2: 01
row 3: 0110
row 4: 01101001
row 5: 0110100110010110
基本可以看出规律了:每一行前面一半是和上一行完全一样的,后一半是和上一行完全相反的。
所以,求解的方法就是计算第K个数是在第N行的前面一半还是后面一半,计算方式是K和2^(N - 2)比较,即K <= (1 << (N - 2))
。如果在前半部分,那么在上一行中寻找第K个数;如果在后半部分,那么在上一行中寻找第K - (1 << (N - 2))个数。
用递归实现的终止条件是当K == 1,或者N == 1,返回0.
时间复杂度是O(N),空间复杂度是O(1)的变量空间,O(N)的栈空间.
代码如下:
class Solution(object):
def kthGrammar(self, N, K):
"""
:type N: int
:type K: int
:rtype: int
"""
if K == 1:
return 0
if K <= (1 << (N - 2)):
return self.kthGrammar(N - 1, K)
else:
return 1 - self.kthGrammar(N - 1, K - (1 << (N - 2)))
参考资料:
日期
2018 年 9 月 26 日 —— 美好的一周又快要过去了。。
【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)的更多相关文章
- LeetCode 973 K Closest Points to Origin 解题报告
题目要求 We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
随机推荐
- .net与java建立WebService再互相调用
A: .net建立WebService,在java中调用. 1.在vs中新建web 简单修改一下Service.cs的[WebMethod]代码: using System; using System ...
- Phoenix二级索引
Phoenix Hbase适合存储大量的对关系运算要求低的NOSQL数据,受Hbase 设计上的限制不能直接使用原生的API执行在关系数据库中普遍使用的条件判断和聚合等操作.Hbase很优秀,一些团队 ...
- idea数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
通过idea操作数据库,进行数据的增加,运行时报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 原因:没有导入mysql-connec ...
- day17 阶段测验
题目 1.找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写 有以下几种方法: [root@localhost ~]# grep -iE "^s" /pro ...
- Hadoop、Hive【LZO压缩配置和使用】
目录 一.编译 二.相关配置 三.为LZO文件创建索引 四.Hive为LZO文件建立索引 1.hive创建的lzo压缩的分区表 2.给.lzo压缩文件建立索引index 3.读取Lzo文件的注意事项( ...
- API测试最佳实践 - 身份验证
适用等级:高级 1. 概况 身份验证通常被定义为是对某个资源的身份的确认的活动,这里面资源的身份指代的是API的消费者(或者说是调用者).一旦一个用户的身份验证通过了,他将被授权访问那些期待访问的资源 ...
- JavaBean的命名规则
JavaBean的命名规则Sun 推荐的命名规范1 ,类名要首字母大写,后面的单词首字母大写2 ,方法名的第一个单词小写,后面的单词首字母大写3 ,变量名的第一个单词小写,后面的单词首字母大写为了使 ...
- Java易错小结
String 相关运算 String使用是注意是否初始化,未初始化的全部为null.不要轻易使用 string.isEmpty()等,首先确保string非空. 推荐使用StringUtils.isN ...
- 解决Spring MVC @ResponseBody出现问号乱码问题
原因是SpringMVC的@ResponseBody使用的默认处理字符串编码为ISO-8859-1,而我们前台或者客户端的编码一般是UTF-8或者GBK.现将解决方法分享如下! 第一种方法: 对于需要 ...
- 【分布式技术专题】「OSS中间件系列」Minio的文件服务的存储模型及整合Java客户端访问的实战指南
Minio的元数据 数据存储 MinIO对象存储系统没有元数据数据库,所有的操作都是对象级别的粒度的,这种做法的优势是: 个别对象的失效,不会溢出为更大级别的系统失效. 便于实现"强一致性& ...