LeetCode "Game of Life"
2015-10-04 10:31
411 查看
A coding practice.
View Code
class Solution { public: // encoding: 2 - 1 to 0; -1 - 0 to live void gameOfLife(vector<vector<int>>& board) { int h = board.size(); int w = board[0].size(); // Pass 1 for(int y = 0; y < h; y ++) for(int x = 0; x < w; x ++) { int cnt = 0; for(int dx = -1; dx < 2; dx ++) for(int dy = -1; dy < 2; dy ++) { int xx = x + dx, yy = y + dy; if (xx >=0 && xx < w && yy >= 0 && yy < h && !(xx==x && yy == y)) { cnt += board[yy][xx] > 0; } } if(board[y][x]) { if (!(cnt == 2 || cnt == 3)) { board[y][x] = 2; } } else { if(cnt == 3) board[y][x] = -1; } } // Pass 2 for(int y = 0; y < h; y ++) for(int x = 0; x < w; x ++) { if (board[y][x] == 2) board[y][x] = 0; else if (board[y][x] == -1) board[y][x] = 1; } } };
View Code
相关文章推荐
- jQuery插件Timelinr 实现时间轴特效
- 自定义tableview Header和Footer的样式
- META
- 剑指offer—二叉搜索树与双向链表
- jquery 实现输入邮箱时自动补全下拉提示功能
- React Native 开始
- HTML5学习之二:HTML5中的表单1
- ubuntu下安装node
- html5 自定义标签属性取值为undefined 笔记随记
- namenode无法启动,不报错
- js定时器timer
- 使用Ajax中的Json传输数据时,从后台查询的字段过滤方法。
- 【bzoj1016】【JSOI2008】【最小生成树计数】【dfs+最小生成树】
- The ratio of reported blocks 0.0000 has not reached the threshold 0.9990. Safe mode will be turned o
- Hadoop 报错be replicated to 0 nodes, instead of 1
- Could not buffer record
- ByteBuffer写入数组BufferUnderflowException异常
- A configuration error occurred during startup.Please verify the preference filed with the prompt
- hadoop异常“could only be replicated to 0 nodes, instead of 1” 解决
- javascript类型转化加法就不会直接字符相加了