number of islands leetcode code
C++
void numIslands_dfs(int r, int c, int n, int m, vector<vector<char>> &grid)
{
if(grid[r][c] != '1')
return;
grid[r][c] = '0';
if(r+1 < n)
numIslands_dfs(r+1,c,n,m,grid);
if(c+1 < m)
numIslands_dfs(r,c+1,n,m,grid);
if(r-1 >= 0)
numIslands_dfs(r-1,c,n,m,grid);
if(c-1 >= 0)
numIslands_dfs(r,c-1,n,m,grid);
}
int numIslands(vector<vector<char>>& grid)
{
if(grid.size() == 0)
return 0;
int n = grid.size();
int m = grid[0].size();
int count = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(grid[i][j] == '1')
{
count++;
numIslands_dfs(i,j,n,m,grid);
}
}
}
return count;
}
Also in C++:
- Title
- c++ variable argument
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- how to open an input file in c++
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- qt graphics scene map cursor position
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- rick astley - never gonna give you up
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- ternary search c++
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- runtime error in c++
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- queue c++
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- c++ for loops
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- how to initialize 2d vector of any size
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- how to convert string to int c++
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- convert entire string to lowercase c++
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- c++ delete printed characters
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- how to decalre a string in c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- getline not working c++
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- compare values within within a vector c++
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- loop through words in string c++
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- *max_element in c++
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- screen record ios simulator
- Category
- C++
- Title
- date to string c++
- Category
- C++
- Title
- c++ reading string
- Category
- C++
- Title
- c++ bsod
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++
- Title
- c++ vector
- Category
- C++