File tree Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ // Definition for a Node.
3
+ class Node {
4
+ public:
5
+ int val;
6
+ vector<Node*> neighbors;
7
+ Node() {
8
+ val = 0;
9
+ neighbors = vector<Node*>();
10
+ }
11
+ Node(int _val) {
12
+ val = _val;
13
+ neighbors = vector<Node*>();
14
+ }
15
+ Node(int _val, vector<Node*> _neighbors) {
16
+ val = _val;
17
+ neighbors = _neighbors;
18
+ }
19
+ };
20
+ */
21
+
22
+ class Solution
23
+ {
24
+ public:
25
+ Node *cloneGraph (Node *node)
26
+ {
27
+ vector<Node *> nodes (101 , nullptr );
28
+ vector<bool > visited (101 , false );
29
+ queue<Node *> q;
30
+ q.push (node);
31
+
32
+ if (node == nullptr )
33
+ {
34
+ return nullptr ;
35
+ }
36
+
37
+ while (!q.empty ())
38
+ {
39
+ Node *cur = q.front ();
40
+ q.pop ();
41
+ vector<Node *> cpNeighbors;
42
+
43
+ for (auto n : cur->neighbors )
44
+ {
45
+ if (!visited[n->val ])
46
+ {
47
+ q.push (n);
48
+ if (!nodes[n->val ])
49
+ {
50
+ // Not exist
51
+ Node *temp = new Node (n->val );
52
+ nodes[n->val ] = temp;
53
+ }
54
+ }
55
+
56
+ // Add a neighbor
57
+ cpNeighbors.push_back (nodes[n->val ]);
58
+ }
59
+
60
+ if (!nodes[cur->val ])
61
+ {
62
+ // not existed
63
+ Node *newNode = new Node (cur->val , cpNeighbors);
64
+ nodes[cur->val ] = newNode;
65
+ visited[cur->val ] = true ;
66
+ }
67
+ else
68
+ {
69
+ nodes[cur->val ]->neighbors = cpNeighbors;
70
+ visited[cur->val ] = true ;
71
+ }
72
+ }
73
+
74
+ return nodes[1 ];
75
+ }
76
+ };
Original file line number Diff line number Diff line change
1
+ class Solution
2
+ {
3
+ public:
4
+ int rob (vector<int > &nums)
5
+ {
6
+ int first = 0 , second = 0 ;
7
+ for (int i = 0 ; i < nums.size (); i++)
8
+ {
9
+ int tempt = first;
10
+ first = second;
11
+ second = max (second, tempt + nums[i]);
12
+ }
13
+
14
+ return second;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ class Solution
2
+ {
3
+ public:
4
+ bool canJump (vector<int > &nums)
5
+ {
6
+ int l = 0 ;
7
+ int maxIndex = 0 ;
8
+
9
+ while (l < nums.size () - 1 )
10
+ {
11
+ maxIndex = max (maxIndex, l + nums[l]);
12
+ if (maxIndex >= nums.size () - 1 )
13
+ {
14
+ return true ;
15
+ }
16
+
17
+ if (l == maxIndex)
18
+ {
19
+ return false ;
20
+ }
21
+
22
+ l++;
23
+ }
24
+
25
+ return true ;
26
+ }
27
+ };
You can’t perform that action at this time.
0 commit comments