LeetCode刷题记录——day9

https://leetcode.cn/problems/game-of-life/?envType=study-plan-v2&envId=2024-spring-sprint-100 先创建一个数组,让它比原数组大一圈,然后将其全设为0,在原数组中每有一个1出现,就将其对应位置的新数组的周围全部加一,最后新数组中对应位置的数字就是其周围有多少个活细胞

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int l1=board.size(),l2=board[0].size();
        int temp_boear[l1+2][l2+2];

        memset(temp_boear,0,sizeof(temp_boear));

        for(int i=1;i<=l1;i++){
            for(int j=1;j<=l2;j++){
                if(board[i-1][j-1]==1){
                    temp_boear[i-1][j-1]++;
                    temp_boear[i][j-1]++;
                    temp_boear[i-1][j]++;
                    temp_boear[i+1][j+1]++;
                    temp_boear[i+1][j]++;
                    temp_boear[i][j+1]++;
                    temp_boear[i+1][j-1]++;
                    temp_boear[i-1][j+1]++;
                }
            }
        }


        for(int i=1;i<=l1;i++){
            for(int j=1;j<=l2;j++){
                if(temp_boear[i][j]<2){
                    board[i-1][j-1]=0;
                }
                if(temp_boear[i][j]>3){
                    board[i-1][j-1]=0;
                }
                if(temp_boear[i][j]==3){
                    board[i-1][j-1]=1;
                }
            }
        }
    }
};

本文由博客一文多发平台 OpenWrite 发布!

相关推荐

  1. LeetCode记录——day9

    2024-03-31 17:16:03       38 阅读
  2. LeetCode记录——day7

    2024-03-31 17:16:03       39 阅读
  3. LeetCode记录——day8

    2024-03-31 17:16:03       43 阅读
  4. leetcode记录 day21

    2024-03-31 17:16:03       146 阅读
  5. wy的leetcode记录_Day76

    2024-03-31 17:16:03       59 阅读
  6. LeetCode -- Day 5

    2024-03-31 17:16:03       32 阅读
  7. LeetCode -- Day 7

    2024-03-31 17:16:03       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 17:16:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 17:16:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 17:16:03       87 阅读
  4. Python语言-面向对象

    2024-03-31 17:16:03       96 阅读

热门阅读

  1. Redis(二)

    2024-03-31 17:16:03       34 阅读
  2. JVM原理

    JVM原理

    2024-03-31 17:16:03      30 阅读
  3. CPU Cache

    2024-03-31 17:16:03       36 阅读
  4. 专升本-数字媒体

    2024-03-31 17:16:03       39 阅读
  5. List转换为Map

    2024-03-31 17:16:03       30 阅读
  6. Mysql数据库——阻塞语句查询与分析

    2024-03-31 17:16:03       44 阅读
  7. 100. 相同的树

    2024-03-31 17:16:03       43 阅读