Skip to content

Commit 2308ceb

Browse files
committed
🚀 27-Aug-2020
1 parent 8be001c commit 2308ceb

File tree

5 files changed

+424
-0
lines changed

5 files changed

+424
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
Given a reference of a node in a connected undirected graph.
2+
3+
Return a deep copy (clone) of the graph.
4+
5+
Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.
6+
7+
class Node {
8+
public int val;
9+
public List<Node> neighbors;
10+
}
11+
12+
13+
Test case format:
14+
15+
For simplicity sake, each node's value is the same as the node's index (1-indexed). For example, the first node with val = 1, the second node with val = 2, and so on. The graph is represented in the test case using an adjacency list.
16+
17+
Adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
18+
19+
The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.
20+
21+
22+
23+
Example 1:
24+
25+
26+
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
27+
Output: [[2,4],[1,3],[2,4],[1,3]]
28+
Explanation: There are 4 nodes in the graph.
29+
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
30+
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
31+
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
32+
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
33+
Example 2:
34+
35+
36+
Input: adjList = [[]]
37+
Output: [[]]
38+
Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
39+
Example 3:
40+
41+
Input: adjList = []
42+
Output: []
43+
Explanation: This an empty graph, it does not have any nodes.
44+
Example 4:
45+
46+
47+
Input: adjList = [[2],[1]]
48+
Output: [[2],[1]]
49+
50+
51+
Constraints:
52+
53+
1 <= Node.val <= 100
54+
Node.val is unique for each node.
55+
Number of Nodes will not exceed 100.
56+
There is no repeated edges and no self-loops in the graph.
57+
The Graph is connected and all nodes can be visited starting from the given node.
58+
59+
60+
61+
62+
63+
64+
/*
65+
// Definition for a Node.
66+
class Node {
67+
public:
68+
int val;
69+
vector<Node*> neighbors;
70+
71+
Node() {
72+
val = 0;
73+
neighbors = vector<Node*>();
74+
}
75+
76+
Node(int _val) {
77+
val = _val;
78+
neighbors = vector<Node*>();
79+
}
80+
81+
Node(int _val, vector<Node*> _neighbors) {
82+
val = _val;
83+
neighbors = _neighbors;
84+
}
85+
};
86+
*/
87+
88+
class Solution {
89+
public:
90+
Node* cloneGraph(Node* src) {
91+
if(!src) return src;
92+
93+
map<Node*, Node*> m;
94+
queue<Node*> q;
95+
96+
q.push(src);
97+
Node* node=new Node(src->val);
98+
m[src]=node;
99+
100+
while(!q.empty()){
101+
Node* u=q.front();
102+
q.pop();
103+
vector<Node*> v=u->neighbors;
104+
int n=v.size();
105+
for(int i=0;i<n;i++){
106+
if(m[v[i]]==NULL){
107+
node=new Node(v[i]->val);
108+
m[v[i]]=node;
109+
q.push(v[i]);
110+
}
111+
m[u]->neighbors.push_back(m[v[i]]);
112+
}
113+
}
114+
return m[src];
115+
}
116+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. 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.
2+
3+
4+
5+
Example 1:
6+
7+
Input: grid = [
8+
["1","1","1","1","0"],
9+
["1","1","0","1","0"],
10+
["1","1","0","0","0"],
11+
["0","0","0","0","0"]
12+
]
13+
Output: 1
14+
Example 2:
15+
16+
Input: grid = [
17+
["1","1","0","0","0"],
18+
["1","1","0","0","0"],
19+
["0","0","1","0","0"],
20+
["0","0","0","1","1"]
21+
]
22+
Output: 3
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
class Solution {
33+
public:
34+
bool isSafe(vector<vector<char>> &grid, vector<vector<bool>> &vis, int i, int j){
35+
return (i>=0 && i<grid.size() && j>=0 && j<grid[0].size() && grid[i][j]=='1' && !vis[i][j]);
36+
}
37+
void dfs(vector<vector<char>> &grid, vector<vector<bool>> &vis, int i, int j){
38+
vis[i][j]=true;
39+
if(isSafe(grid, vis, i-1, j)) dfs(grid, vis, i-1, j); // top
40+
41+
if(isSafe(grid, vis, i, j-1)) dfs(grid, vis, i, j-1); // left
42+
43+
if(isSafe(grid, vis, i+1, j)) dfs(grid, vis, i+1, j); // down
44+
45+
if(isSafe(grid, vis, i, j+1)) dfs(grid, vis, i, j+1); // right
46+
47+
}
48+
49+
int numIslands(vector<vector<char>>& grid) {
50+
int m=grid.size();
51+
if(m==0) return 0;
52+
int n=grid[0].size();
53+
54+
vector<vector<bool> > vis(m, vector<bool>(n,false));
55+
int island=0;
56+
57+
for(int i=0;i<m;i++){
58+
for(int j=0;j<n;j++){
59+
if(!vis[i][j] && grid[i][j]=='1'){
60+
island++;
61+
dfs(grid, vis, i, j);
62+
}
63+
}
64+
}
65+
return island;
66+
}
67+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Write a program that outputs the string representation of numbers from 1 to n.
2+
3+
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
4+
5+
Example:
6+
7+
n = 15,
8+
9+
Return:
10+
[
11+
"1",
12+
"2",
13+
"Fizz",
14+
"4",
15+
"Buzz",
16+
"Fizz",
17+
"7",
18+
"8",
19+
"Fizz",
20+
"Buzz",
21+
"11",
22+
"Fizz",
23+
"13",
24+
"14",
25+
"FizzBuzz"
26+
]
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
class Solution {
38+
public:
39+
vector<string> fizzBuzz(int n) {
40+
vector<string> res;
41+
for(int i=1;i<=n;i++){
42+
bool three=i%3==0;
43+
bool five=i%5==0;
44+
if(three && five)
45+
res.push_back("FizzBuzz");
46+
else if(five)
47+
res.push_back("Buzz");
48+
else if(three)
49+
res.push_back("Fizz");
50+
else res.push_back(to_string(i));
51+
}
52+
return res;
53+
}
54+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Write a program that outputs the string representation of numbers from 1 to n.
2+
3+
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
4+
5+
Example:
6+
7+
n = 15,
8+
9+
Return:
10+
[
11+
"1",
12+
"2",
13+
"Fizz",
14+
"4",
15+
"Buzz",
16+
"Fizz",
17+
"7",
18+
"8",
19+
"Fizz",
20+
"Buzz",
21+
"11",
22+
"Fizz",
23+
"13",
24+
"14",
25+
"FizzBuzz"
26+
]
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
class Solution {
38+
public:
39+
vector<string> fizzBuzz(int n) {
40+
vector<string> res;
41+
for(int i=1;i<=n;i++){
42+
bool three=i%3==0;
43+
bool five=i%5==0;
44+
if(three && five)
45+
res.push_back("FizzBuzz");
46+
else if(five)
47+
res.push_back("Buzz");
48+
else if(three)
49+
res.push_back("Fizz");
50+
else res.push_back(to_string(i));
51+
}
52+
return res;
53+
}
54+
};

0 commit comments

Comments
 (0)