【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 个人 ...
随机推荐
- 二进制免编译My SQL
一 下载 MySQL 安装包教程 https://blog.csdn.net/zhan107876/article/details/100701135 ll -h mysql-5.6.47-linux ...
- C#表格,表格信息、GridView使用。
page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="test1.a ...
- JDBC01 获取数据库连接
概述 Java Database Connectivity(JDBC)直接访问数据库,通用的SQL数据库存取和操作的公共接口,定义访问数据库的标准java类库(java.sql,javax.sql) ...
- ORACLE 查询sql和存储性能思路
1.确定session id 如果是存储过程,在程序开头设置客户端标识.并根据标识获取session id. DBMS_SESSION.set_identifier('XXX'); select * ...
- HTML样式 背景
当浏览器读到一个样式表,就会按照这个格式表来对文档进行格式化.有以下三种方式来插入样式表: 1.外部样式表 当样式需要用到很多页面的时候,外部样式是理想的选择.使用外部样式表,就可以听过更改一个文件来 ...
- Linux基础命令---uptime
uptime uptime指令用来显示系统运行多长时间.有多少用户登录.系统负载情况. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.SUSE.openSUSE. ...
- CentOS6+nginx+uwsgi+mysql+django1.6.6+python2.6.6
1.配置网关 #vi /etc/sysconfig/network NETWORKING=yes(表示系统是否使用网络,一般设置为yes.如果设为no,则不能使用网络,而且很多系统服务程序将无法启动) ...
- vue2.x入门学习
vue安装 # 最新稳定版本 $ npm install vue # 最新稳定 CSP 兼容版本 $ npm install vue@csp 引包 cd /d/vue/demo cnpm instal ...
- rust方法集
随机数.数字对比.控制台输入 use std::io; use std::cmp::Ordering; use rand::Rng; fn main() { println!("please ...
- Linux:$i 和 ${i}区别
例如你要把有个变量的值和其他字符串连接起来,就需要用到{},以明示{}中的是一个变量. 例如: export var1=ABC export var2=var1=${var1} echo $var2 ...