首先看200题:
Given a 2d grid map of '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
DFS:
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if(grid.empty())
return 0;
m = grid.size();
n = grid[0].size();
int counter = 0;
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(grid[i][j] == '1'){
dfs_mark(grid, i, j);
++counter;
}
}
}
return counter;
}
void dfs_mark(vector<vector<char>>& grid, int x, int y){
if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != '1')
return;
grid[x][y] = '0';
dfs_mark(grid, x-1, y);
dfs_mark(grid, x+1, y);
dfs_mark(grid, x, y-1);
dfs_mark(grid, x, y+1);
}
private:
int m;
int n;
};
并查集版本:
class Solution {
public:
class uf_set {
public:
uf_set(const vector<vector<char>>& grid, int m, int n)
: father_(new int[m*n]), rnk_(new int[m*n]), counter_(0){
fill(rnk_, rnk_+m*n, 0);
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(grid[i][j] == '1'){
int id = i * n + j;
father_[id] = id;
++counter_;
}
}
}
}
~uf_set(){
delete []father_;
delete []rnk_;
}
public:
int find(int x){
int root = x;
while(root != father_[root])
root = father_[root];
while(x != father_[x]){
int y = father_[x];
father_[x] = root;
x = y;
}
return root;
}
void un(int x, int y){
x = find(x), y = find(y);
if(x == y) return ;
if(rnk_[x] > rnk_[y]) father_[y] = x;
else father_[x] = y, rnk_[y] += rnk_[x] == rnk_[y];
--counter_;
}
int counter() const { return counter_; }
private:
int* father_;
int* rnk_;
int counter_;
};
public:
vector<vector<char>> direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int numIslands(vector<vector<char>>& grid) {
if(grid.empty())
return 0;
int m = grid.size(), n = grid[0].size();
uf_set us(grid, m, n);
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(grid[i][j] == '1'){
for(auto d: direction){
int x = i + d[0];
int y = j + d[1];
if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1'){
int id1 = i * n + j;
int id2 = x * n + y;
us.un(id1, id2);
}
}
}
}
}
int res = us.counter();
return res;
}
};
做上面这道题时,我强迫症发作,所以每个成员变量后面都加了后缀。后了一想,这样写项目很有用,写算法就是反而很鸡肋。于是我决定,以后写算法不在成员变量后面加后缀下划线了。
看130题。
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
Subscribe to see which companies asked this question.
首先我直接用DFS:
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.empty())
return ;
int m = board.size();
int n = board[0].size();
for(int i=0; i<m; ++i)
if(board[i][0] == 'O') dfs_mark(board, m, n, i, 0);
for(int j=1; j<n; ++j)
if(board[0][j] == 'O') dfs_mark(board, m, n, 0, j);
for(int i=1; i<m; ++i)
if(board[i][n-1] == 'O') dfs_mark(board, m, n, i, n-1);
for(int j=1; j<n-1; ++j)
if(board[m-1][j] == 'O') dfs_mark(board, m, n, m-1, j);
for(int i=1; i<m-1; ++i){
for(int j=1; j<n-1; ++j)
if(board[i][j] != 'N') board[i][j] = 'X';
}
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j)
if(board[i][j] == 'N') board[i][j] = 'O';
}
}
void dfs_mark(vector<vector<char>>& board, int m, int n, int x, int y){
if(x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'O') return;
board[x][y] = 'N';
dfs_mark(board, m, n, x-1, y);
dfs_mark(board, m, n, x+1, y);
dfs_mark(board, m, n, x, y-1);
dfs_mark(board, m, n, x, y+1);
}
};
然后超时了。但是我在网上搜这道题,在别的OJ上用这份代码通过了。所以leetcode要求还是蛮严格的。
然后是并查集版本:
class Solution {
public:
vector<vector<char>> direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void solve(vector<vector<char>>& board) {
if(board.empty())
return;
int m = board.size();
int n = board[0].size();
uf_set us(m*n+1);
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if((i == 0 || i == m-1 || j == 0 || j == n-1) && board[i][j] == 'O')
us.union_set(i*n+j, m*n);
else if(board[i][j] == 'O'){
for(auto d : direction){
int x = i + d[0];
int y = j + d[1];
if(board[x][y] == 'O')
us.union_set(i*n+j, x*n+y);
}
}
}
}
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(us.find_set(i*n+j) != us.find_set(n*m))
board[i][j] = 'X';
}
}
}
private:
class uf_set {
public:
uf_set(int sz) : father(new int[sz]), rank(new int[sz]) {
for(int i=0; i<sz; ++i){
father[i] = i;
rank[i] = 0;
}
}
int find_set(int x){
int root = x;
while(root != father[root])
root = father[root];
while(x != father[x]){
int y = father[x];
father[x] = root;
x = y;
}
return root;
}
void union_set(int x, int y){
x = find_set(x), y = find_set(y);
if(x == y) return;
if(rank[x] > rank[y])
father[y] = x;
else
father[x] = y, rank[y] += rank[x] == rank[y];
}
private:
int* father;
int* rank;
};
};
秒杀之。