Skip to content

Commit f4a686e

Browse files
committed
🚀 25-Jul-2020
1 parent e9fdde1 commit f4a686e

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and return them in any order.
2+
3+
The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.
4+
5+
Example:
6+
Input: [[1,2], [3], [3], []]
7+
Output: [[0,1,3],[0,2,3]]
8+
Explanation: The graph looks like this:
9+
0--->1
10+
| |
11+
v v
12+
2--->3
13+
There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
14+
Note:
15+
16+
The number of nodes in the graph will be in the range [2, 15].
17+
You can print different paths in any order, but you should keep the order of nodes inside one path.
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
33+
void dfs(vector<vector<int> > &graph, int src, int dst, vector<int> &curr, vector<vector<int>> &res){
34+
if(src==dst){
35+
res.push_back(curr);
36+
return;
37+
}
38+
for(auto x: graph[src]){
39+
curr.push_back(x);
40+
dfs(graph, x, dst, curr, res);
41+
curr.erase(curr.end()-1);
42+
}
43+
}
44+
45+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
46+
vector<vector<int> > res;
47+
vector<int> curr;
48+
int n=graph.size();
49+
curr.push_back(0); // inserting src
50+
dfs(graph, 0, n-1, curr, res); // src --> 0 dst --> n-1
51+
return res;
52+
}
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
2+
3+
Example:
4+
5+
Input: [1,2,1,3,2,5]
6+
Output: [3,5]
7+
Note:
8+
9+
The order of the result is not important. So in the above example, [5, 3] is also correct.
10+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
vector<int> singleNumber(vector<int>& nums) {
24+
vector<int> res(2, 0);
25+
int xor2=0; // xor of 2 unique numbers
26+
for(auto &x : nums){
27+
xor2^=x;
28+
}
29+
int setBit=xor2 & (-xor2); // set bit from rigth to left
30+
for(auto x: nums){
31+
if((setBit & x) == 0)
32+
res[0]^=x;
33+
else res[1]^=x;
34+
}
35+
return res;
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and return them in any order.
2+
3+
The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.
4+
5+
Example:
6+
Input: [[1,2], [3], [3], []]
7+
Output: [[0,1,3],[0,2,3]]
8+
Explanation: The graph looks like this:
9+
0--->1
10+
| |
11+
v v
12+
2--->3
13+
There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
14+
Note:
15+
16+
The number of nodes in the graph will be in the range [2, 15].
17+
You can print different paths in any order, but you should keep the order of nodes inside one path.
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
33+
void dfs(vector<vector<int> > &graph, int src, int dst, vector<int> &curr, vector<vector<int>> &res){
34+
if(src==dst){
35+
res.push_back(curr);
36+
return;
37+
}
38+
for(auto x: graph[src]){
39+
curr.push_back(x);
40+
dfs(graph, x, dst, curr, res);
41+
curr.erase(curr.end()-1);
42+
}
43+
}
44+
45+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
46+
vector<vector<int> > res;
47+
vector<int> curr;
48+
int n=graph.size();
49+
curr.push_back(0); // inserting src
50+
dfs(graph, 0, n-1, curr, res); // src --> 0 dst --> n-1
51+
return res;
52+
}
53+
};

0 commit comments

Comments
 (0)