Skip to content

Commit 95eab30

Browse files
committed
GFG
1 parent b001289 commit 95eab30

7 files changed

+226
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution
2+
{
3+
public:
4+
//Function to return the adjacency list for each vertex.
5+
vector<vector<int>> printGraph(int V, vector<int> adj[])
6+
{
7+
vector<vector<int>> v(V);
8+
for (int i = 0; i < V; ++i)
9+
{
10+
v[i].push_back(i);
11+
for (auto it : adj[i])
12+
{
13+
v[i].push_back(it);
14+
}
15+
}
16+
17+
return v;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
vector<int> reverseLevelOrder(Node *root)
2+
{
3+
vector<int> ans;
4+
if (root == nullptr)
5+
{
6+
return ans;
7+
}
8+
9+
queue<Node *> q;
10+
stack<int> s;
11+
q.push(root);
12+
while (!q.empty())
13+
{
14+
Node *curr = q.front();
15+
s.push(curr->data);
16+
q.pop();
17+
if (curr->right)
18+
{
19+
q.push(curr->right);
20+
}
21+
if (curr->left)
22+
{
23+
q.push(curr->left);
24+
}
25+
}
26+
while (!s.empty())
27+
{
28+
ans.push_back(s.top());
29+
s.pop();
30+
}
31+
return ans;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution
2+
{
3+
public:
4+
void sort012(int arr[], int n)
5+
{
6+
long int low = 0, mid = 0, high = n - 1;
7+
while (mid <= high)
8+
{
9+
if (arr[mid] == 0)
10+
{
11+
swap(arr[mid], arr[low]);
12+
low++;
13+
mid++;
14+
}
15+
else if (arr[mid] == 1)
16+
{
17+
mid++;
18+
}
19+
else
20+
{
21+
swap(arr[mid], arr[high]);
22+
high--;
23+
}
24+
}
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution
2+
{
3+
public:
4+
//Function to find the number of islands.
5+
6+
void dfs(vector<vector<char>> &grid, int n, int m, int i, int j)
7+
{
8+
if (i >= 0 && j >= 0 && i < n && j < m && grid[i][j] == '1')
9+
{
10+
grid[i][j] = '5';
11+
dfs(grid, n, m, i - 1, j); //top
12+
dfs(grid, n, m, i + 1, j); //down
13+
dfs(grid, n, m, i, j - 1); //left side
14+
dfs(grid, n, m, i, j + 1); //right side
15+
dfs(grid, n, m, i - 1, j - 1); //north west
16+
dfs(grid, n, m, i + 1, j + 1); //south east
17+
dfs(grid, n, m, i - 1, j + 1); //north east
18+
dfs(grid, n, m, i + 1, j - 1); //south west
19+
}
20+
}
21+
22+
int numIslands(vector<vector<char>> &grid)
23+
{
24+
int count = 0;
25+
int n = grid.size();
26+
int m = grid[0].size();
27+
28+
for (int i = 0; i < n; ++i)
29+
{
30+
for (int j = 0; j < m; ++j)
31+
{
32+
if (grid[i][j] == '1')
33+
{
34+
dfs(grid, n, m, i, j);
35+
count++;
36+
}
37+
}
38+
}
39+
40+
return count;
41+
}
42+
};
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution
2+
{
3+
public:
4+
//Function to return list containing vertices in Topological order.
5+
vector<int> topoSort(int V, vector<int> adj[])
6+
{
7+
vector<int> ans;
8+
vector<int> inorder(V, 0);
9+
10+
for (int i = 0; i < V; ++i)
11+
{
12+
for (auto it : adj[i])
13+
{
14+
inorder[it]++;
15+
}
16+
}
17+
18+
queue<int> q;
19+
20+
for (int i = 0; i < V; ++i)
21+
{
22+
if (inorder[i] == 0)
23+
{
24+
q.push(i);
25+
}
26+
}
27+
28+
while (!q.empty())
29+
{
30+
int u = q.front();
31+
ans.push_back(u);
32+
for (auto v : adj[u])
33+
{
34+
inorder[v]--;
35+
if (inorder[v] == 0)
36+
{
37+
q.push(v);
38+
}
39+
}
40+
q.pop();
41+
}
42+
43+
return ans;
44+
}
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution
13+
{
14+
public:
15+
vector<vector<int>> levelOrder(TreeNode *root)
16+
{
17+
vector<vector<int>> ans;
18+
vector<int> v;
19+
if (root == nullptr)
20+
{
21+
return ans;
22+
}
23+
queue<TreeNode *> q;
24+
q.push(root);
25+
while (!q.empty())
26+
{
27+
int sz = q.size();
28+
while (sz--)
29+
{
30+
TreeNode *curr = q.front();
31+
v.push_back(curr->val);
32+
q.pop();
33+
if (curr->left)
34+
{
35+
q.push(curr->left);
36+
}
37+
if (curr->right)
38+
{
39+
q.push(curr->right);
40+
}
41+
}
42+
ans.push_back(v);
43+
v.clear();
44+
}
45+
return ans;
46+
}
47+
};

Leet Code/C++/344. Reverse String.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution
2+
{
3+
public:
4+
void reverseString(vector<char> &s)
5+
{
6+
int last = s.size() - 1;
7+
int first = 0;
8+
while (first < last)
9+
{
10+
swap(s[first], s[last]);
11+
first++;
12+
last--;
13+
}
14+
}
15+
};

0 commit comments

Comments
 (0)