Number of Islands
2016-02-23 10:46
288 查看
Given a 2d grid map of
Example 1:
Answer: 1
Example 2:
Answer: 3
Analyse: As I saw the problem, I feel that I can use dfs to search all continuous '1's and mark them. Scanning row by row, from column to column, when meeting a '1', then mark all its continuous block of '1's. After that, all of its connecting '1's are labeled. Then find the next '1', and mark its connecting '1's, until the right bottom item.
So I add a visited vector vector to label the visited items. But it exceeded time limite.
View Code
This version deletes the visited vector vector because we can simply replace '1' by any non-0 or non-1 value. It passed with 8ms.
'1's (land) and
'0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110 11010 11000 00000
Answer: 1
Example 2:
11000 11000 00100 00011
Answer: 3
Analyse: As I saw the problem, I feel that I can use dfs to search all continuous '1's and mark them. Scanning row by row, from column to column, when meeting a '1', then mark all its continuous block of '1's. After that, all of its connecting '1's are labeled. Then find the next '1', and mark its connecting '1's, until the right bottom item.
So I add a visited vector vector to label the visited items. But it exceeded time limite.
class Solution { public: int numIslands(vector<vector<char>>& grid) { int result; if(grid.empty() || grid[0].empty()) return result; int row = grid.size(), col = grid[0].size(); vector<vector<bool> > visited(row, vector<bool>(col, false)); for(int i = 0; i < row; i++){ for(int j = 0; j < col; j++){ if(grid[i][j] == '1' && !visited[i][j]){ helper(grid, visited, i, j); result++; } } } return result; } void helper(vector<vector<char> > grid, vector<vector<bool> >& visited, int currentX, int currentY){ if(currentX >= grid.size() || currentX < 0 || currentY >= grid[0].size() || currentY < 0 || grid[currentX][currentY] == '0' || visited[currentX][currentY]) return; visited[currentX][currentY] = true; helper(grid, visited, currentX + 1, currentY); helper(grid, visited, currentX - 1, currentY); helper(grid, visited, currentX, currentY + 1); helper(grid, visited, currentX, currentY - 1); } };
View Code
This version deletes the visited vector vector because we can simply replace '1' by any non-0 or non-1 value. It passed with 8ms.
class Solution { public: int numIslands(vector<vector<char>>& grid) { int result; if(grid.empty() || grid[0].empty()) return result; int row = grid.size(), col = grid[0].size(); for(int i = 0; i < row; i++){ for(int j = 0; j < col; j++){ if(grid[i][j] == '1'){ helper(grid, i, j); result++; } } } return result; } void helper(vector<vector<char> > &grid, int x, int y){ if(x >= grid.size() || x < 0 || y >= grid[0].size() || y < 0 || grid[x][y] != '1') return; grid[x][y] = '2'; helper(grid, x + 1, y); helper(grid, x - 1, y); helper(grid, x, y + 1); helper(grid, x, y - 1); } };
相关文章推荐
- JVM学习(2)——技术文章里常说的堆,栈,堆栈到底是什么,从os的角度总结
- HTML代码书写规范指南
- 【C++】构造函数和析构函数
- 堆
- SI2302低压MOS管- 场效应管SI2302低压管
- PHP基于数组的分页函数(核心函数array_slice())
- JVM学习(1)——通过实例总结Java虚拟机的运行机制
- jQuery监听文本框值改变触发事件(propertychange)
- 翻翻git之---一个简单的标签控件 LabelView (随手发了两张小宝宝的玩耍照)
- 记录一个简单的ASM程序破解过程
- eerTyraniBdecnalaB.110
- Android 蓝牙的使用(待续)
- Mac 下自带的中文输入法不显示汉字提示问题
- 项目部署,版本迭代个人心得
- mysql数据库时间查询相关
- 5-策略模式
- 翻翻git之---一个简单的标签控件 LabelView (随手发了两张小宝宝的玩耍照)
- Engine中如何截取线上指定两点间的线段?
- 彻底理解js中this的指向
- 多线程GCD(二)