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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(0974):和可被K整除的子数组(Python)
下一篇:LeetCode题解(0954):二倍数对数组(Python)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月26日 13时35分48秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章