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
  • 相关阅读:
    Session共享的解决方案
    用IIS配置反向代理
    authorization配置
    git之https或http方式设置记住用户名和密码的方法
    微信分享接口
    为你的Visual Studio单独设置代理服务器
    HTTP错误404.13
    MVC5的AuthorizeAttribute详解
    【MVC5】画面多按钮提交
    PetaPoco dynamic
  • 原文地址:https://www.cnblogs.com/CherryTab/p/12623951.html
Copyright © 2011-2022 走看看