diff --git a/C++/number-of-islands.cpp b/C++/number-of-islands.cpp new file mode 100644 index 00000000..bffdc2c2 --- /dev/null +++ b/C++/number-of-islands.cpp @@ -0,0 +1,49 @@ +class Solution { +public: + bool visited[300][300]; + int r[4] = {-1,0,0,1}; + int c[4] = {0,-1,1,0}; + + //Checks if the given row and col value are valid and if the cell is visited and if the cell contains '1' or not. + bool val(int row,int col,vector>& grid,int M,int N) + { + return (row=0 && col>=0 && !visited[row][col] && grid[row][col]=='1'); + } + + //Dfs function for exploring the surrounding cells + void dfs(int i,int j,vector>& grid, int M, int N) + { + visited[i][j] = true; + for(int a=0;a<4;a++) + { + int row = i + r[a]; + int col = j + c[a]; + if(val(row,col,grid,M,N)) + { + dfs(row,col,grid,M,N); + } + } + } + + int numIslands(vector>& grid) { + int m = grid.size(); + int n = grid[0].size(); + memset(visited,0,sizeof(visited)); + int island_count = 0; + for(int i=0;i [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Java](./Java/path-sum.java) | _O(n)_ | _O(n)_ | Easy | DFS | | | 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Java](./Java/Balanced-Binary-Tree.java) | _O(n)_ | _O(n)_ | Easy | DFS | | | 1376 | [ Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [C++](./C++/Cherry-Pickup-II.cpp) | _O(n)_ | _O(n)_ | Medium | DFS | | +| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [C++](./C++/number-of-islands.cpp) | _O(m * n)_ | _O(m * n)_ | Medium | DFS | | -|
- +
@@ -500,6 +500,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Sachin_Upadhyay](https://github.com/sachsbu)
| India | Java | [GitHub](https://github.com/sachsbu) | | [Amisha Sahu](https://github.com/Amisha328)
| India | C++ | [CodeChef](https://www.codechef.com/users/amisha328)
[LeetCode](https://leetcode.com/Mishi328/)
[HackerRank](https://www.hackerrank.com/amishasahu328)
+| [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh)