题目

Java/957. Prison Cells After N Days N天后的牢房

Java/957. Prison Cells After N Days N天后的牢房


Java/957. Prison Cells After N Days N天后的牢房

Java/957. Prison Cells After N Days N天后的牢房

 

 

 

 

题目不难,很容易找到思路,比较坑的是找循环节。(一个小技巧,给的Example1中有七个数据,从这个数字开始到双倍、三倍)一般题目不会无端给这么多测试数据的

 

代码部分一(12ms 85.61%)

class Solution {
    public int[] prisonAfterNDays(int[] cells, int N) {
        if(N <= 0) return cells;
        if(N%14 == 0){
            N = 14;
        }else {
            N = N%14;
        }
        while(N > 0){
            int[] res = new int[8];
            res[0] = 0;
            res[7] = 0;
            for(int i = 1; i < 7; i++){
                 res[i] = (cells[i-1] ^ cells[i+1]) ^ 1;
            }
            cells = res;
            N--;
        }
        
        
        return cells;
    }
}

 

代码部分二(11ms 100%)

class Solution {
    public int[] prisonAfterNDays(int[] cells, int N) {
        if(N = 0) return cells;
        if(N%14 == 0){
            N = 14;
        }else {
            N = N%14;
        }
        while(N  0){
            int[] res = new int[8];
            res[0] = 0;
            res[7] = 0;
            for(int i = 1; i  7; i++){
                if(cells[i-1] == cells[i+1]) res[i] = 1;
                else res[i] = 0;
            }
            cells = res;
            N--;
        }
        
        
        return cells;
    }
}

 

相关文章:

  • 2022-03-03
  • 2022-12-23
  • 2021-12-20
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-23
  • 2022-12-23
  • 2022-01-28
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案