zoukankan      html  css  js  c++  java
  • leetcode-289

    主要的问题是我们需要用上一个状态来判断当前状态。因为可能因为你变1之后影响其他的。

    Perhaps that’s been the story of life

    func gameOfLife(board [][]int) {
        temp := make([][]int, len(board))
        for i := 0; i < len(board); i++ {
            temp[i] = make([]int, len(board[i]))
        }
        for i := 0; i < len(board); i++ {
            for j := 0; j < len(board[0]); j++ {
                nums := 0
                // 上面
                if i - 1 >= 0 {
                    nums += board[i-1][j]
                }
                // 左面
                if j - 1 >= 0 {
                    nums += board[i][j-1]
                }
                // 下面
                if i + 1 < len(board) {
                    nums += board[i+1][j]
                }
                // 右面
                if j + 1 < len(board[i]) {
                    nums += board[i][j+1]
                }
                // 左上
                if i - 1 >= 0 && j - 1 >= 0 {
                    nums += board[i-1][j-1]
                }
                // 右上
                if i - 1 >= 0 && j + 1 < len(board[i]) {
                    nums += board[i-1][j+1]
                }
                // 左下
                if i + 1 < len(board) && j - 1 >= 0  {
                    nums += board[i+1][j-1]
                }
                // 右下
                if j + 1 < len(board[i]) && i + 1 < len(board) {
                    nums += board[i+1][j+1]
                }
                temp[i][j] = board[i][j]
                switch {
                case nums < 2:
                    temp[i][j] = 0
                case nums == 3 && temp[i][j] == 0:
                    temp[i][j] = 1
                case nums > 3:
                    temp[i][j] = 0
                }
            }
        }
        copy(board, temp)
    }

    end

    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    元数据管理
    sqoop 安装
    postgres 索引
    postgres 表和库等信息大小统计
    Perl基础语法
    Perl 认识简介
    Oracle层次查询start with connect by
    jquery.cookie.js 的使用指南
    JavaScript中cookie使用
    CSS实现垂直居中的4种思路
  • 原文地址:https://www.cnblogs.com/CherryTab/p/12623951.html
Copyright © 2011-2022 走看看