Number of Islands Solutions in C++
Number 200
Difficulty Medium
Acceptance 46.9%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/number-of-islands/// Author : Hao Chen// Date : 2015-06-08#include <iostream>#include <vector>using namespace std;void mark(vector<vector<char> >& grid, int r, int c){if ( r<0 || r>=grid.size() ||c<0 || c>=grid[0].size() ||grid[r][c] != '1' ) {return;}grid[r][c] = 'M';mark(grid, r, c+1); //leftmark(grid, r, c-1); //rightmark(grid, r-1, c); //upmark(grid, r+1, c); //down}int numIslands(vector<vector<char> >& grid) {int result = 0;for (int r=0; r<grid.size(); r++) {for (int c=0; c<grid[r].size(); c++) {if (grid[r][c] == '1') {result++;mark(grid, r, c);}}}return result;}void initGrid( string g[], int len, vector<vector<char> >& grid ){for (int i=0; i<len; i++){grid.push_back(vector<char>(g[i].begin(), g[i].end()));}}int main(void){vector< vector<char> > grid;grid.push_back( vector<char>(1, '1') );cout << numIslands(grid) << endl;grid.clear();string g1[] = { "11110","11010","11000","00000" };initGrid(g1, 4, grid);cout << numIslands(grid) << endl;grid.clear();string g2[] = { "11000","11000","00100","00011" };initGrid(g2, 4, grid);cout << numIslands(grid) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2017-11-18#include <iostream>#include <vector>#include <cassert>using namespace std;/// Floodfill - DFS/// Recursion implementation////// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class Solution {private:int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int m, n;vector<vector<bool>> visited;bool inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}void dfs(vector<vector<char>>& grid, int x, int y){//assert(inArea(x,y));visited[x][y] = true;for(int i = 0; i < 4; i ++){int newx = x + d[i][0];int newy = y + d[i][1];if(inArea(newx, newy) && !visited[newx][newy] && grid[newx][newy] == '1')dfs(grid, newx, newy);}return;}public:int numIslands(vector<vector<char>>& grid) {m = grid.size();if(m == 0)return 0;n = grid[0].size();if(n == 0)return 0;for(int i = 0 ; i < m ; i ++)visited.push_back(vector<bool>(n, false));int res = 0;for(int i = 0 ; i < m ; i ++)for(int j = 0 ; j < n ; j ++)if(grid[i][j] == '1' && !visited[i][j]){dfs(grid, i, j);res ++;}return res;}};int main() {char g1[4][5] = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};vector<vector<char>> grid1;for(int i = 0 ; i < 4 ; i ++)grid1.push_back( vector<char>(g1[i], g1[i] + sizeof( g1[i])/sizeof(char)));cout << Solution().numIslands(grid1) << endl;// 1// ---char g2[4][5] = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};vector<vector<char>> grid2;for(int i = 0 ; i < 4 ; i ++)grid2.push_back(vector<char>(g2[i], g2[i] + sizeof( g2[i])/sizeof(char)));cout << Solution().numIslands(grid2) << endl;// 2return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2018-08-29#include <iostream>#include <vector>#include <cassert>#include <stack>using namespace std;/// Floodfill - DFS/// Non-recursion implementation////// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class Solution {private:int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int m, n;public:int numIslands(vector<vector<char>>& grid) {m = grid.size();if(m == 0)return 0;n = grid[0].size();if(n == 0)return 0;vector<vector<bool>> visited(m, vector<bool>(n, false));int res = 0;for(int i = 0 ; i < m ; i ++)for(int j = 0 ; j < n ; j ++)if(grid[i][j] == '1' && !visited[i][j]){dfs(grid, i, j, visited);res ++;}return res;}private:void dfs(vector<vector<char>>& grid, int x, int y, vector<vector<bool>>& visited){stack<pair<int, int>> q;q.push(make_pair(x, y));visited[x][y] = true;while(!q.empty()){int curx = q.top().first;int cury = q.top().second;q.pop();for(int i = 0; i < 4; i ++){int newX = curx + d[i][0];int newY = cury + d[i][1];if(inArea(newX, newY) && !visited[newX][newY] && grid[newX][newY] == '1'){q.push(make_pair(newX, newY));visited[newX][newY] = true;}}}return;}bool inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}};int main() {vector<vector<char>> grid1 = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};cout << Solution().numIslands(grid1) << endl;// 1// ---vector<vector<char>> grid2 = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};cout << Solution().numIslands(grid2) << endl;// 3return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2018-08-25#include <iostream>#include <vector>#include <cassert>#include <queue>using namespace std;/// Floodfill - BFS/// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class Solution {private:int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int m, n;public:int numIslands(vector<vector<char>>& grid) {m = grid.size();if(m == 0)return 0;n = grid[0].size();if(n == 0)return 0;vector<vector<bool>> visited(m, vector<bool>(n, false));int res = 0;for(int i = 0 ; i < m ; i ++)for(int j = 0 ; j < n ; j ++)if(grid[i][j] == '1' && !visited[i][j]){bfs(grid, i, j, visited);res ++;}return res;}private:void bfs(vector<vector<char>>& grid, int x, int y, vector<vector<bool>>& visited){queue<pair<int, int>> q;q.push(make_pair(x, y));visited[x][y] = true;while(!q.empty()){int curx = q.front().first;int cury = q.front().second;q.pop();for(int i = 0; i < 4; i ++){int newX = curx + d[i][0];int newY = cury + d[i][1];if(inArea(newX, newY) && !visited[newX][newY] && grid[newX][newY] == '1'){q.push(make_pair(newX, newY));visited[newX][newY] = true;}}}return;}bool inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}};int main() {vector<vector<char>> grid1 = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};cout << Solution().numIslands(grid1) << endl;// 1// ---vector<vector<char>> grid2 = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};cout << Solution().numIslands(grid2) << endl;// 3return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2018-08-26#include <iostream>#include <vector>#include <cassert>#include <unordered_set>using namespace std;/// Using union-find/// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class UnionFind{private:vector<int> rank, parent;public:UnionFind(int n){rank.clear();parent.clear();for( int i = 0 ; i < n ; i ++ ){parent.push_back(i);rank.push_back(1);}}int find(int p){while(p != parent[p]){parent[p] = parent[parent[p]];p = parent[p];}return p;}bool isConnected(int p , int q){return find(p) == find(q);}void unionElements(int p, int q){int pRoot = find(p);int qRoot = find(q);if(pRoot == qRoot)return;if(rank[pRoot] < rank[qRoot])parent[pRoot] = qRoot;else if(rank[qRoot] < rank[pRoot])parent[qRoot] = pRoot;else{ // rank[pRoot] == rank[qRoot]parent[pRoot] = qRoot;rank[qRoot] += 1;}}};class Solution {private:int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int m, n;public:int numIslands(vector<vector<char>>& grid) {m = grid.size();if(m == 0)return 0;n = grid[0].size();if(n == 0)return 0;UnionFind uf(m * n);for(int i = 0; i < m; i ++)for(int j = 0; j < n; j ++)if(grid[i][j] == '1')for(int k = 0; k < 4; k ++){int newX = i + d[k][0];int newY = j + d[k][1];if(inArea(newX, newY) && grid[newX][newY] == '1')uf.unionElements(i * n + j, newX * n + newY);}unordered_set<int> regions;for(int i = 0 ; i < m; i ++)for(int j = 0; j < n; j ++)if(grid[i][j] == '1')regions.insert(uf.find(i * n + j));return regions.size();}private:bool inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}};int main() {vector<vector<char>> grid1 = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};cout << Solution().numIslands(grid1) << endl;// 1// ---vector<vector<char>> grid2 = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};cout << Solution().numIslands(grid2) << endl;// 3return 0;}