码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode289

时间:2018-10-04 12:22:42      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:div   tco   span   ++   amp   r++   row   tin   col   

public class Solution
    {
        public void GameOfLife(int[][] board)
        {
            var row = board.GetLength(0) - 1;
            var col = board[0].GetLength(0) - 1;
            var list = new List<List<int>>();
            for (int r = 0; r <= row; r++)
            {
                var l = new List<int>();
                for (int c = 0; c <= col; c++)
                {
                    l.Add(board[r][c]);
                }
                list.Add(l);
            }

            for (int r = 0; r <= row; r++)
            {
                for (int c = 0; c <= col; c++)
                {
                    int livecount = 0;
                    for (int i = -1; i <= 1; i++)
                    {
                        for (int j = -1; j <= 1; j++)
                        {
                            if (i == 0 && j == 0)
                            {
                                continue;
                            }
                            var newx = r + i;
                            var newy = c + j;
                            if (newx < 0 || newx > row || newy < 0 || newy > col)
                            {
                                continue;
                            }
                            if (board[newx][newy] == 1)
                            {
                                livecount++;//活节点
                            }
                        }
                    }
                    if (board[r][c] == 1)//当前节点是活节点
                    {
                        if (livecount == 2 || livecount == 3)
                        {
                            list[r][c] = 1;
                        }
                        else
                        {
                            list[r][c] = 0;
                        }
                    }
                    else
                    {
                        if (livecount == 3)
                        {
                            list[r][c] = 1;
                        }
                    }
                }
            }

            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < list[0].Count; j++)
                {
                    board[i][j] = list[i][j];
                }
            }
        }
    }

 

leetcode289

标签:div   tco   span   ++   amp   r++   row   tin   col   

原文地址:https://www.cnblogs.com/asenyang/p/9741914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!