id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n void visit(vector<vector<char>>& grid, int i, int j) {\n std::vector<std::pair<int, int>> toVisit;\n toVisit.emplace_back(i, j);\n int m = grid.size();\n int n = grid[0].size();\n while (!toVisit.empty()) {\n auto [row, col] = toV... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\n queue<std::pair<int, int>> find, fill;\n set<std::pair<int, int>> check_set = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\npublic:\n void fillIsland(vector<vector<char>> &grid){\n while(fill.size() > 0){\n auto [ci, cj] = fill.front();\n fill.pop();\n\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n int withinBound(int i, int j, int m, int n) {\n return 0<=i && i<m && 0<=j && j<n;\n }\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>> v(m, vector<int>(n,0));\n queu... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n =0;\n int r = grid.size();\n int c = grid[0].size();\n \n queue<pair<int,int>> q;\n \n for(int i=0; i < r; i++){\n for(int j=0; j< c; j++){\n \n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n class DisjointSet\n {\n public:\n vector<int> parent, rank, size;\n int count;\n DisjointSet(int n)\n {\n rank.resize(n+1, 0);\n size.resize(n+1, 1);\n parent.resize(n+1);\n count = 0;\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n class DisjointSet\n {\n public:\n vector<int> parent, rank, size;\n int count;\n DisjointSet(int n)\n {\n rank.resize(n+1, 0);\n size.resize(n+1, 1);\n parent.resize(n+1);\n count = 0;\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\n stack<std::pair<int, int>> find, fill;\n set<std::pair<int, int>> check_set = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\npublic:\n void fillIsland(vector<vector<char>> &grid){\n while(fill.size() > 0){\n auto [ci, cj] = fill.top();\n fill.pop();\n\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int i, int j , vector<vector<char>>& grid, vector<vector<int>>& visited, int row, int col ){\n if(i >= row || j >= col || i < 0 || j < 0) return;\n if(grid[i][j] != '1' || visited[i][j] == 1) return;\n visited[i][j] = 1;\n dfs(i-1,... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class disjoint {\npublic:\n vector<int> size, parent;\n disjoint(int n) {\n size.resize(n + 1);\n parent.resize(n+1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n int findpar(int u) {\n if (u == parent[u]) {\... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "#include <stack>\n#include <utility>\n\nstruct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::stack<Index> &dfsStack,\n int X,\n int Y)\n {\n if (g... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "#include <stack>\n#include <utility>\n\nstruct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::stack<Index> &dfsStack,\n int X,\n int Y)\n {\n if (g... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "#include <stack>\n#include <utility>\n\nstruct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::stack<Index> &dfsStack,\n int X,\n int Y)\n {\n if (g... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\n private : \n void bfs(queue<pair<int,int>> &q,vector<vector<char>> &grid,vector<vector<int>> &visited,int m,int n)\n {\n while(!q.empty())\n {\n pair<int,int> temp=q.front();\n q.pop();\n\n int i=temp.first;\n int j=temp.... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "struct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::vector<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] ==... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "struct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::vector<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] ==... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int totalIslands = 0;\n int rowSize = grid.size();\n int colSize = grid[0].size();\n \n queue<pair<int, int>> myQueue;\n for (int r = 0; r < rowSize; r++)\n {\n for (int... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\r\npublic:\r\n void bfs(deque<pair<int, int>> &adj, vector<vector<int>> &vis, vector<vector<char>>& grid){\r\n int x[4] = {1, -1, 0, 0};\r\n int y[4] = {0, 0, 1, -1};\r\n int xn, yn;\r\n while(!adj.empty()){\r\n auto now = adj.front();\r\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "struct Index\n{\n int x = -1, y = -1;\n};\n\nclass Solution\n{\n void push_to_stack(\n const vector<vector<char>> &grid,\n vector<vector<int>> &nodeStates,\n std::vector<Index> &dfsStack,\n int X,\n int Y)\n {\n if (grid[X][Y] == '1' && nodeStates[X][Y] ==... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n\n int m = grid.size();\n int n = grid[0].size();\n\n int visited[m * n];\n for (size_t i = 0; i < m*n; i++) {\n visited[i] = 0;\n }\n\n std::queue<std::pair<int, int>> bfs_queue;\n\n for (size_t i = 0;... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\r\n\r\n int res_{0};\r\npublic:\r\n\r\n vector<pair<int, int>> GetVaildNeighbors(vector<vector<char>>& grid, int x, int y) {\r\n int m = grid.size();\r\n int n = grid[0].size();\r\n vector<pair<int,int>> neighbors;\r\n if(x > 0 and grid[x-1][y] == '1') {\r... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\r\n\r\n int res_{0};\r\npublic:\r\n\r\n vector<pair<int, int>> GetVaildNeighbors(vector<vector<char>>& grid, int x, int y) {\r\n int m = grid.size();\r\n int n = grid[0].size();\r\n vector<pair<int,int>> neighbors;\r\n if(x > 0 and grid[x-1][y] == '1') {\r... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n void func(vector<vector<char>>& grid, list<pair<int,int>>& que, int i, int j) {\n int n = grid.size(); int m = grid[0].size();\n pair<int,int> p = make_pair(i,j);\n que.push_back(p);\n grid[i][j] = '0';\n while(!que.empty()) {\n p... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<char>>& grid, int i, int j) {\n grid[i][j] = '0';\n vector<pair<int, int>> dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};\n for(pair<int, int> dir: dirs) {\n int di = i + dir.first;\n int dj = j + dir.second;\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n for (int i = 0; i < grid.size(); i++)\n {\n for (int j = 0; j < grid[0].size(); j++)\n {\n if (grid[i][j] == '1')\n {\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n void check(vector<vector<char>>& grid, int x, int y){\n if(x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == '0'){\n return ;\n }\n grid[x][y] = '0';\n vector<pair<int, int>> d = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.\n\n// 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.\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.\n\n// 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.\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution\n{\n const std::array<int, 5> OFFSETS = {-1, 0, 1, 0, -1};\n\npublic:\n int numIslands(std::vector<std::vector<char>> &grid)\n {\n int M = grid.size(), N = grid[0].size();\n int count = 0;\n\n for (int r = 0; r < M; r++)\n {\n for (int c = 0; c... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution\n{\n const std::array<int, 5> OFFSETS = {-1, 0, 1, 0, -1};\n\npublic:\n int numIslands(std::vector<std::vector<char>> &grid)\n {\n int M = grid.size(), N = grid[0].size();\n int count = 0;\n\n for (int r = 0; r < M; r++)\n {\n for (int c = 0; c... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "class Solution {\npublic:\n \n int numIslands(vector<vector<char>>& grid) {\n int rows = grid.size();\n int cols = grid[0].size();\n int cc = 0;\n for(int r = 0;r<rows;r++){\n for(int c = 0;c<cols;c++){\n if(grid[r][c]=='0'){\n c... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 2 | {
"code": "#include <vector>\n#include <deque>\n#include <string>\nclass Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n int rows = grid.size();\n if (rows == 0) return 0;\n int cols = grid[0].size();\n \n // Build the array of explore... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\n void dfs(int r, int c, vector<vector<int>> &vis, vector<vector<char>> &grid) {\n int m = grid.size();\n int n = grid[0].size();\n vis[r][c] = 1;\n vector<int> dr = {-1, 0, 1, 0};\n vector<int> dc = {0, 1, 0, -1};\n for(int i = 0; i < 4; i++) {... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\n void bfs(vector<vector<char>>& adj, int i, int j, vector<vector<int>> &vis){\n int n = adj.size();\n int m = adj[0].size();\n queue<pair<int,int>> q;\n q.push({i,j});\n vis[i][j] = 1;\n \n while(!q.empty()){\n int row = q.fro... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n=grid.size(),m=grid[0].size(),cnt=0,i,j;\n vector<vector<int>>vis(n,vector<int>(m,0));\n for(i=0;i<n;i++)\n {\n for(j=0;j<m;j++)\n {\n if(grid[i][j]=='1' && !v... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n=grid.size(),m=grid[0].size(),cnt=0,i,j;\n vector<vector<int>>vis(n,vector<int>(m,0));\n for(i=0;i<n;i++)\n {\n for(j=0;j<m;j++)\n {\n if(grid[i][j]=='1' && !v... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if (grid.empty() || grid[0].empty()) return 0;\n\n int n = grid.size();\n int m = grid[0].size();\n int islands = 0;\n\n vector<vector<bool>> visited(n, vector<bool>(m, false));\n\n for (... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "#include <iostream>\n#include <vector>\n#include <stack>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n void searchForConnectedLand(vector<vector<char>> &grid, int i, int j)\n {\n stack<pair<int, int>> landSquare;\n landSquare.push({i, j});\n\n // Search directions (up, ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(vector<vector<char>>& grid, int i, int j, int m, int n){\n queue<pair<int, int>> q;\n q.push(make_pair(i, j));\n \n\n while (!q.empty()) {\n pair<int, int> top = q.front();\n q.pop();\n\n int x = top.firs... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<bool>> visited(m, vector<bool> (n, false));\n\n auto process = [&](queue<tuple<int, int>>& q, int _i, int _j){\n bool inbound =... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n int numIslands(vector<vector<char>>& grid) {\n vector<vector<bool>> visited;\n vector<pair<int, int>> directions;\n int ans = 0;\n directions.push_back(make_pair(1, 0));\n directions.push_back(make_pair(-1, 0));\n directions.push_ba... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n using Matrix = std::vector<std::vector<char>>;\n using Coordinate = std::pair<int64_t, int64_t>;\n\n std::stack<Coordinate> m_Stack;\n std::vector<std::vector<bool>> m_Visited;\n\n std::array<Coordinate, 4> getNeighbours(int64_t x, int64_t y) {\n std::array... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int m;\n int n;\n int dx[4] = {1, -1, 0, 0};\n int dy[4] = {0, 0, 1, -1};\n\n bool isSafe(int i, int j) {\n return (i < m && i >= 0 && j < n && j >= 0);\n }\n\n void checkAdj(int i, int j, vector<vector<int>>& visited, vector<vector<char>>& grid) {\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Coor{\npublic:\n int x, y;\n Coor(int x, int y){\n this->x = x;\n this->y = y;\n }\n};\nint deltaX[4] = {0, 1, -1, 0};\nint deltaY[4] = {1, 0, 0, -1};\n\nclass Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if(grid.size() == 0 || grid[0].size() =... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n void convert(vector<vector<char>> grid, vector<int> adj[]) {\n int m = grid.size();\n int n = grid[0].size();\n\n for (int i=0; i<m; i++) {\n for (int j=0; j<n; j++) {\n if (grid[i][j] == '0') {\n continue;\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n void convert(vector<vector<char>> grid, vector<int> adj[]) {\n int m = grid.size();\n int n = grid[0].size();\n\n for (int i=0; i<m; i++) {\n for (int j=0; j<n; j++) {\n if (grid[i][j] == '0') {\n continue;\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n void visit(vector<vector<char>>& grid,vector<vector<bool>>& visited,int m,int n, int row, int col){\n queue<pair<int,int>> q;\n q.push(make_pair(m,n));\n visited[m][n]=true;\n vector<vector<int>> dir={{-1,0},{1,0},{0,-1},{0,1}};\n while(!q.e... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\n void dfs (int v, vector<vector<int>>& vec, vector<bool>& used){\n used[v] = true;\n for(auto& it: vec[v]){\n if(!used[it]){\n dfs(it, vec, used);\n }\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n #define F first\n #define S second\n int m,n;\n int cell[310][310];\n int vis[310][310];\n using state=pair<int,int>;\n int dx[4]={-1,0,1,0};\n int dy[4]={0,-1,0,1};\n\n bool valid(int x,int y){\n if(x<0 || x>=m || y<0 || y>=n || cell[x][y]==0) ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n #define F first\n #define S second\n int m,n;\n int cell[310][310];\n int vis[310][310];\n using state=pair<int,int>;\n int dx[4]={-1,0,1,0};\n int dy[4]={0,-1,0,1};\n\n vector<state> neigh(state node){\n vector<state> nodes;\n for(int k=... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n int ans=0;\n // vector<vector<bool>> vis(m,vector<bool>(n,false));\n for(int i=0; i<m; i++)\n {\n for(int j=0; j<n; j++)\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>> &grid) {\n int n = grid.size(),\n m = grid[0].size(),\n num_islands = 0;\n \n const auto neighbors = [&](int a, int b) -> vector<pair<int, int>> {\n vector<pair<int, int>> res;\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n const size_t height = grid.size();\n const size_t width = grid.back().size();\n \n int count = 0;\n vector<vector<bool>> visited(height, vector<bool>(width));\n for (size_t row = 0; row <... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int r=grid.size(); \n int c=grid[0].size();\n vector<vector<int>> vis(r,vector<int>(c,0));\n int cnt=0;\n\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n if(vis[i][j]... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int row, int col, int n, int m) {\n visited[row][col] = true;\n\n queue<pair<int, int>> q;\n q.push({row, col});\n\n while (!q.empty()) {\n auto front = q.front();\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n void bfs(int row, int col, vector<vector<bool>> &visited, vector<vector<char>>& grid) {\n // mark the node as the visited\n visited[row][col] = true;\n\n // m rows, n columns\n int m = grid.size(), n = grid[0].size();\n\n // queue for BFS tr... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n int x[4] = {-1, 0, 1, 0};\n int y[4] = {0, 1, 0, -1};\n\n map<pair<int, int>, bool> visited;\n\n\n int m,n;\n\n void dfs(int i, int j, vector<vector<char>>& grid) {\n visited[pair(i, j)] = true;\n\n for (int k = 0; k < 4; k++) {\n int r ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n int x[4] = {-1, 0, 1, 0};\n int y[4] = {0, 1, 0, -1};\n\n map<pair<int, int>, bool> visited;\n\n\n int m,n;\n\n void dfs(int i, int j, vector<vector<char>>& grid) {\n visited[pair(i, j)] = true;\n\n for (int k = 0; k < 4; k++) {\n int r ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\n private:\n void bfs(int row, int col, vector<vector<int>> &vis, vector<vector<char>>&grid ){\n int m = grid.size();\n int n = grid[0].size();\n vis[row][col]=1;\n queue<pair<int, int>>q;\n q.push({row, col});\n\n while(!q.empty()){\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "\nclass Solution {\nprivate:\n void bfs(int sRow, int sCol, vector<vector<int>>& visited, vector<vector<char>>& grid){\n visited[sRow][sCol] = 1;\n queue<pair<int, int>> q;\n q.push({sRow, sCol});\n int rows = grid.size();\n int cols = grid[0].size();\n\n while ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n void bfs(vector<vector<char>>&grid, int row, int col)\n {\n queue<pair<char,pair<int,int>>>q;\n char current=grid[row][col];\n grid[row][col]='0';\n q.push({current, {row,col}});\n //map to store visited\n map<pair<int,int>,bool>visited;\n whil... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\nprivate:\n // up, right, down, left\n vector<pair<int,int>> dirs = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};\n set<pair<int,int>> visited;\n\n void bfs(int i, int j, vector<vector<char>>& grid) {\n\n queue<pair<int,int>> q;\n visited.insert({i,j});\n q.push({i,j... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n vector<vector<bool>> isVisited(grid.size(), vector<bool>(grid[0].size()));\n int count = 0;\n for(int i = 0; i < grid.size(); i ++){\n for(int j = 0; j < grid[0].size(); j ++){\n if(... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution \n{\n // Steps:\n // 1. move in all four directions from all nodes to valid (land) nodes\n // 2. count number of connected components\n\npublic:\n // Input grid\n vector<vector<char>> grid;\n int m;\n int n;\n // Seen matrix\n vector<vector<bool>> seen;\n // Dir... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(int i,int j, vector<vector<char>> &grid, map<pair<int,int>,int> &vis, int m,int n)\n {\n \n if(i+1<m && grid[i+1][j]=='1' && vis[{i+1,j}]==0)\n {\n vis[{i+1,j}]=1;\n dfs(i+1,j,grid,vis,m,n);\n }\n if(i-1>=... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\nvoid dfs(map<pair<int , int> ,int>&visited , vector<vector<char>>&grid, int i , int j){\n visited[{i ,j}]=1;\n if(i+1<grid.size() && j<grid[i+1].size()){\n if(grid[i+1][j]=='1' && !visited[{i+1 , j}]){\n dfs(visited , grid , i+1 ,j);\n }\n }\n ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "struct Hash {\n size_t operator() (const std::pair<size_t, size_t> &p) const {\n return std::hash<size_t>()(p.first) ^ std::hash<size_t>()(p.second);\n }\n};\n\nclass Solution {\n std::unordered_set<std::pair<size_t, size_t>, Hash> visited;\n\npublic:\n int numIslands(vector<vector<char>>& gr... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n vector<pair<int, int>> get_unseen_neighbors(pair<int, int> next, vector<vector<char>> &grid, unordered_set<int>& islandsSeen){\n vector<pair<int, int>> v;\n\n if(next.first != 0){\n if(grid[next.first - 1][next.second] == '1'){\n if(isl... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(const vector<vector<char>>& grid) {\n const int R = grid.size();\n const int C = grid[0].size();\n\n auto cell = [&](int r, int c) -> char { return grid[r][c]; };\n\n int islands = 0;\n struct hash_func { size_t operator()(con... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n fo... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n fo... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n fo... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n int numIslands(vector<vector<char>>& grid) {\n if(grid.size() == 0)return 0;\n vector<vector<int>>dirs = {{0,1},{0,-1},{1,0},{-1,0}};\n int m = grid.size();\n //cols = rows ? grid[0].size() : 0;\n int n = grid[0].size();\n int coun... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n\n int islands = 0;\n int rows = grid.size();\n int cols = grid[0].size();\n unordered_set<string> visited;\n\n vector<pair<int, int>> directions = {{1,0}, {-1,0}, {0,1},{0,-1}};\n\n for(i... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "\nclass Solution {\npublic:\nint numIslands(vector<vector<char>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n function<int(int)> Find;\n unordered_map<int,int> Parents;\n unordered_map<int,int> Rank;\n Find=[&grid,&Find,&Parents](int i)->int{\n return Parents[i]==i?i:Fin... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n// Converts a 2D grid of chars to an adjacency list of node indices\nvector<vector<int>> convertGridTOAdjList(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>> adjList(m * n); // adjacency list\n\n aut... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n void islandDFS(pair<int,int> index, vector<vector<char>>& grid, set<pair<int, int>>& seen){\n stack<pair<int,int>> s;\n pair<int,int> curr;\n\n s.push(index);\n\n while (!s.empty()){\n curr = s.top();\n s.pop();\n \n if (seen.count(curr... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<char>>& grid, int r, int c) {\n if(r < 0 || c < 0 || r >= grid.size() || c >= grid[0].size() || grid[r][c] == '0') {\n return;\n }\n grid[r][c] = '0';\n\n vector<pair<int,int>> offset{ {1, 0}, {-1, 0}, {0, 1}, {0, ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n const int m = grid.size();\n const int n = grid[0].size();\n // create graph\n // vector<vector<int>> adj_list(m * n, vector<int>());\n // for (int i = 0; i < m; i++) {\n // for (int ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n const int m = grid.size();\n const int n = grid[0].size();\n // create graph\n // vector<vector<int>> adj_list(m * n, vector<int>());\n // for (int i = 0; i < m; i++) {\n // for (int ... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n unordered_set<string> visited;\n int numIslands(vector<vector<char>>& grid) {\n const int ROWS = grid.size();\n const int COLS = grid[0].size();\n \n int count = 0;\n for (int i=0; i<ROWS; ++i) {\n for (int j=0; j<COLS; ++j) {\... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n unordered_set<string> visited;\n int ROWS;\n int COLS;\n int numIslands(vector<vector<char>>& grid) {\n ROWS = grid.size();\n COLS = grid[0].size();\n \n int count = 0;\n for (int i=0; i<ROWS; ++i) {\n for (int j=0; j<COL... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n bool isValid(int i , int j, int r, int c,vector<vector<char>>& grid)\n {\n return i>=0 && i<r && j>=0 && j<c;\n }\n\n int doBFS(vector<vector<bool>>& visited, vector<vector<char>>& grid,int i, int j)\n {\n int r = grid.size();\n int c = grid... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n void bfsCoordinate(vector<vector<char>>& grid, vector<vector<bool>>& visited, int row, int column) {\n queue<vector<int>> toVisit;\n toVisit.push(vector<int>{row, column});\n vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n wh... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n int getKey(vector<int> V) {\n return V[0]*1000 + V[1];\n }\n \n vector<int> getCell(int x) {\n return { x/1000, x%1000 };\n }\n \n unordered_map<int, int> parents;\n int find(int x) {\n return parents[x] == x ? x: parents[x] = find(pa... |
200 | <p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>'1'</code>s (land) and <code>'0'</code>s (water), return <em>the number of islands</em>.</p>
<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve... | 3 | {
"code": "class Solution {\npublic:\n\n void bfs(map<pair<int,int> , bool>& visited,int row , int col,vector<vector<char>>& grid ){\n queue<pair<int,int>> q;\n\n q.push({row,col});\n visited[{row,col}] = true;\n\n while(!q.empty()){\n pair<int,int> fnode = q.front();\n ... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\n\ninline bool isDigit(cha... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\n\ninline bool isDigit(cha... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster ... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster ... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster ... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster ... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool isDigit(char ch) {\n return (ch >= '0') && (ch <= '9');\n}\n\nvoid reverseListAndPrint(const std::string& s, std::ofstream& out) {... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nvoid reverse(ListNode* & cu... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n... |
206 | <p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:... | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.