LeetCode题解(0957):N天后的牢房(Python)
发布日期:2021-06-29 20:09:36
浏览次数:2
分类:技术文章
本文共 876 字,大约阅读时间需要 2 分钟。
题目:(中等)
标签:哈希表
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( 2 8 ) O(2^8) O(28) | O ( 2 8 ) O(2^8) O(28) | 52ms (59.18%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution: def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]: count = { "".join(str(c) for c in cells): 0} lst = [cells] i = 0 while True: i += 1 # 计算调换 new = [0] * 8 for j in range(1, 7): if cells[j - 1] == cells[j + 1]: new[j] = 1 else: new[j] = 0 cells = new lst.append(cells) s = "".join(str(c) for c in cells) if s in count: start = count[s] circle = i - count[s] # 循环周期 break else: count[s] = i return lst[(N - start) % circle + start]
转载地址:https://dataartist.blog.csdn.net/article/details/109840465 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月26日 13时35分48秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
数据结构学习_树(4)
2019-04-30
数组与矩阵(1)_矩阵相乘
2019-04-30
数据结构学习_图(1)深度优先搜索、广度优先搜索和最小生成树
2019-04-30
数据结构学习_图(1)拓扑排序
2019-04-30
利用Strategy模式方便的增加新定义的操作
2019-04-30
command模式(c 语言实现)
2019-04-30
state 模式(c++ 实现)
2019-04-30
打印图形
2019-04-30
新的开始
2019-04-30
dos下常用网络命令
2019-04-30
powerpc汇编指令分析
2019-04-30
欢迎使用CSDN-markdown编辑器
2019-04-30
文章标题
2019-04-30
算法-动态规划
2019-04-30
算法-3个水桶8升水
2019-04-30
算法-稳定匹配
2019-04-30
管道堵住问题的定位
2019-04-30
ssh问题
2021-07-03
struct union and endian
2021-07-03
a bug related with extern
2021-07-03