【leetcode】957. Prison Cells After N Days
题目如下:
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
- If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
- Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way:
cells[i] == 1if thei-th cell is occupied, elsecells[i] == 0.Given the initial state of the prison, return the state of the prison after
Ndays (andNsuch changes described above.)Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]Note:
cells.length == 8cells[i]is in{0, 1}1 <= N <= 10^9
解题思路:当我看到第二个用例中 N = 1000000000 后,直觉告诉我变换结果中应该存在周期性的循环。所以我就测试了前100天结果,发现这个周期是14天。这也是是一种取巧的方法了。
代码如下:
class Solution(object):
def prisonAfterNDays(self, cells, N):
"""
:type cells: List[int]
:type N: int
:rtype: List[int]
"""
if N != 0:
N = N % 14 if N % 14 != 0 else 14
while N > 0:
tl = [0]
for i in range(1,len(cells)-1):
if cells[i-1] == cells[i+1]:
tl.append(1)
else:
tl.append(0)
tl.append(0)
cells = tl[:]
N -= 1
return cells
【leetcode】957. Prison Cells After N Days的更多相关文章
- 【LeetCode】957. Prison Cells After N Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 周期是14 日期 题目地址:https://leet ...
- 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【leetcode】1030. Matrix Cells in Distance Order
题目如下: We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), whe ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
随机推荐
- C++在windows平台下不存在strptime函数,可以绕过该函数
https://blog.csdn.net/u011077672/article/details/50524469?utm_source=blogxgwz1
- 【CSS3】transform-origin以原点进行旋转 (转)
话不多说, 以左上角为原点 -moz-transform-origin: 0 0; -webkit-transform-origin:0 0; -o-transform-origin:0 0; 以右上 ...
- typedef 函数指针的使用(含例子)
C代码 //#include<iostream.h> #include<stdio.h> typedef int (*FP_CALC)(int, int); ...
- qt 学习(六) 数据库注册用户
做什么: 1 登陆按钮按下出现注册页面, 2 输入账号 判断是否可用 查询数据库,用户名是否已经注册 3 输入密码 判断密码格式 4 输入邮箱 判断邮箱格式 查询数据库,邮箱是否已经注册 ...
- centos下安装java jdk1.8
---恢复内容开始--- mysql密码修改了,发现还没装jdk,那就一起记录下来吧.虽然网上好多,但自己想查更方便了. 查看有没有装jdk #java -version显示下面信息,不是oracle ...
- 剑指offer---3、按之字形顺序打印二叉树
剑指offer---3.按之字形顺序打印二叉树 一.总结 一句话总结: |||-begin 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照 ...
- [STemWin教程入门篇]第二期:emWin5.xx的详细移植步骤
转自:http://bbs.armfly.com/read.php?tid=1545 重要说明:(0)由于这个移植教程是去年过年的时候做的,用的是5.16,这就不再做个5.20的移植了,方法是一样的. ...
- java入门经验分享——记面向对象先导课程学习感想
选择在暑期学习面向对象先导课程的初衷是为大二下学期面向对象课程做一些铺垫,接触入门java语言.在接触java的过程中,就编程语言的学习方法而言,我从刚入学时的手慌脚乱四处寻求帮助到现在慢慢养成了自己 ...
- layui 下拉框 动态获取数据
$(function(){var grade=$("#grade");grade.append("一年级"); //添加下拉列表grade.append(&qu ...
- 慎用margin系列2---ie6双倍边距问题
IE6下有一个著名的margin双倍bug 一.什么是双边距Bug? 先来看图: 我们要让绿色盒模型在蓝色盒模型之内向左浮动,并且距蓝色盒模型左侧100像素.这个例子很常见,比如在网页布局中,侧边栏靠 ...