【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 ...
随机推荐
- list,string,tuple,dictionary之间的转换
list,string,tuple,dictionary之间的转换 类型 String List tuple dictionary String - list(str), str.split() tu ...
- 手把手教你搭建一个 Elasticsearch 集群
为何要搭建 Elasticsearch 集群 凡事都要讲究个为什么.在搭建集群之前,我们首先先问一句,为什么我们需要搭建集群?它有什么优势呢? 高可用性 Elasticsearch 作为一个搜索引擎, ...
- 一个JSON解析器
来源 <JavaScript语言精粹(修订版)> 代码 <!DOCTYPE html> <html> <head> <meta charset=& ...
- The kth great number
The kth great number Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. I ...
- Skiing POJ 3037 很奇怪的最短路问题
Skiing POJ 3037 很奇怪的最短路问题 题意 题意:你在一个R*C网格的左上角,现在问你从左上角走到右下角需要的最少时间.其中网格中的任意两点的时间花费可以计算出来. 解题思路 这个需要发 ...
- linux 进程1
一. 进程的开始和结束 1.1. main函数的调用 a. 编译链接时的引导代码.操作系统下的应用程序其实在main执行前也需要先执行一段引导代码才能去执行main,我们写应用程序时不用考虑引导代码的 ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- PHP之简单工厂模式(二)
定义 简单工厂模式,通过定义一个工厂类,负责完成类实例的创建,根据参数的不同返回不同的类实例.对外部来讲,只需传入一个正常的参数就可以获得想要的对象,而不必需要具体创建细节.创建类实例的方法通常为静态 ...
- HNUSTOJ-1675 Morse Code(DFS+字典序搜索)
1675: Morse Code 时间限制: 2 Sec 内存限制: 128 MB提交: 73 解决: 33[提交][状态][讨论版] 题目描述 摩尔斯电码(英语:Morse Code)是一种时通 ...
- HTTP报文结构和内容(转)
HTTP请求报文格式就如下图所示: 1. 请求报文 一个HTTP请求报文由请求行(request line).请求头部(header).空行和请求数据4个部分组成. 大致结构是这样的: <req ...