【leetcode】K-th Symbol in Grammar
题目如下:

解题思路:直接把每行的数据计算出来肯定是不行的,因为N最大是30,那个第N行长度就是2^30次方,这显然不可取。那么就只能找规律了,我采取的是倒推法。例如假如我们要求出第四行第七个元素的值,记为[4,7],显然[4,7]是从第三行的第四个元素计算得来的[3,4],依次类推接下来是[2,2] ,[1,1] 。这样就形成了一个[4,7] -> [3,4] ->[2,2] -> [1,1]递推链。 而[1,1]的值是确定的就是0,那么[2,2]就是0转换成01的第二个元素1,[3,4]就是1转换成10的第二个元素0,[4,7]就是0转换后01的第一个元素0。
完整代码如下:
class Solution(object):
def kthGrammar(self, N, K):
"""
:type N: int
:type K: int
:rtype: int
"""
chain = []
t = N
p = K
while t >= 1:
chain.insert(0,p)
p = p /2 + p%2
t -= 1
print chain value0 = [0,1]
value1 = [1,0]
v = 0
for i in range(1,len(chain)):
if chain[i] % 2 == 0 and v == 0:
v = value0[1]
elif chain[i] % 2 == 1 and v == 0:
v = value0[0]
elif chain[i] % 2 == 0 and v == 1:
v = value1[1]
elif chain[i] % 2 == 1 and v == 1:
v = value1[0]
return v
【leetcode】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】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
随机推荐
- 阶段3 1.Mybatis_11.Mybatis的缓存_4 mybatis一对多实现延迟加载
改成单表查询 首先配置的是select.他需要配置的值是accountDao中的方法,查询所有的账户,但是必须有条件.根据用户的id column配置的是id.因为要用user表的id去关联查询 Ac ...
- Golang http post error : http: ContentLength=355 with Body length 0
参考:https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0 问 ...
- Scala的to和until
object test03 { def main(args: Array[String]): Unit = { //to 每次迭代为1 val to1= to print("to1" ...
- vue--综合组件间的通信
二.综合组件之间的通信 实现一个ToDoList. ①完成所有的组件的创建和使用 ②add点击add按钮时候,将用户输入的内容(todoinput),显示在(todolist) 核心代码:兄弟组件间通 ...
- Django-DRF组件学习-路由学习
1.路由router 对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提 ...
- idea中dbug模式的使用
1:进入断点状态时候每个按钮的用途如图: 2:当运行过得代码需要查看输出内容时候,可以选中需要查看的代码进行运行如图:会在控制台输出选中代码执行的结果
- 【Qt开发】Qt测试计算时间
方法1 利用QTime,其精度为ms级 </pre><pre code_snippet_id="1852215" snippet_file_name=" ...
- 20191110 Spring Boot官方文档学习(4.1)
4. Spring Boot功能 4.1.Spring应用 便捷的启动方式: public static void main(String[] args) { SpringApplication.ru ...
- 深入分析微博密码加密and百万级数据采集分享(登录篇)
一.前言 此文章主要是对微博微博关键词的检索结果进行采集,但是微博的采集需要登陆,所以此程序分为登陆程序和爬虫程序两部分: 微博要实现规模性数据采集自然少不了大量账号,这就需购买大量账号以及批量登陆, ...
- windows系统IO性能测试
关键词:sql server io测试 下载链接:http://www.onlinedown.net/soft/57364.htm CrystalDiskMark(硬盘检测工具),一个测试你的硬盘或者 ...