【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:

  1. N will be an integer in the range [1, 30].
  2. 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)的更多相关文章

  1. 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, ...

  2. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  4. 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  5. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  6. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  7. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  8. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

随机推荐

  1. 【蛋白质基因组】Proteogenomics方法介绍及分析思路

    概念 利用蛋白质组学数据,结合基因组数据(DNA).转录组数据(RNA)来研究基因组注释问题,被称为蛋白质基因组学."蛋白质基因组学"一词由Jaffe 等于2004 年首次提出,作 ...

  2. memset初始化值的效率秒杀for循环

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. sed 修改文件

    总结 正确的修改进文件命令(替换文件内容):sed -i "s#machangwei#mcw#g" mcw.txt 正确的修改追加进文件命令(追加文件内容):sed -i &quo ...

  4. 巩固javaweb第十四天

    巩固内容: 单行文本框: 单行文本框的基本语法格式如下: < input type="text"  name="输入信息的字"  value=" ...

  5. 日常Java 2021/10/20

    Java提供了一套实现Collection接口的标准集合类 bstractCollection 实现了大部分的集合接口. AbstractList 继承于AbstractCollection并且实现了 ...

  6. keybd_event模拟键盘按键,mouse_event怎么用

    从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...

  7. A Child's History of England.30

    CHAPTER 10 ENGLAND UNDER HENRY THE FIRST, CALLED FINE-SCHOLAR Fine-scholar, on hearing of the Red Ki ...

  8. RocketMQ集群搭建方式

    各角色介绍 Producer:消息的发送者:举例:发信者 Consumer:消息接收者:举例:收信者 Broker:暂存和传输消息:举例:邮局 NameServer:管理Broker:举例:各个邮局的 ...

  9. Linux系统中安装软件方法总结

    Linux系统中安装软件方法总结 [1]Linux系统中安装软件的几种方式 [2] Linux配置yum源(本地源和网络源) [3] SuSE下zypper源配置 [4] SUSE zypper 本地 ...

  10. OpenStack之十: 安装dashboard

    官网地址 https://docs.openstack.org/horizon/stein/install/install-rdo.html #:安装包 [root@cobbler ~]# yum i ...