Skip to content

Commit 6552381

Browse files
authored
Merge pull request neetcode-gh#7 from UnresolvedCold/main
C++ Solution for Leetcode Question Numbered, 1, 20, 121, 125, 167, 217, 242, 704, 206, 21, 226, 104, 100
2 parents 7cdbc81 + 187fd3f commit 6552381

13 files changed

+317
-0
lines changed

cpp/1-Two-Sum.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int, int> umap;
5+
for(int i=0; i<nums.size(); i++) {
6+
if (umap.find(target-nums[i]) != umap.end()){
7+
return {umap[target-nums[i]], i};
8+
}
9+
10+
umap[nums[i]] = i;
11+
}
12+
13+
return {};
14+
}
15+
};

cpp/100-Same-Tree.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
public:
14+
bool isSameTree(TreeNode* p, TreeNode* q) {
15+
if (p == NULL and q == NULL) return true;
16+
if (p==NULL || q==NULL) return false;
17+
if (p->val != q->val) return false;
18+
19+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
20+
}
21+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
13+
// Using Stack
14+
// class Solution {
15+
// public:
16+
// int maxDepth(TreeNode* root) {
17+
// if (root == NULL) return 0;
18+
// stack<pair<TreeNode*, int>> stk;
19+
// int res = 0;
20+
// stk.push({root, 1});
21+
22+
// while (!stk.empty()) {
23+
// auto t = stk.top();
24+
// res = max(res, t.second);
25+
// stk.pop();
26+
// if (t.first->left) stk.push({t.first->left, t.second+1});
27+
// if (t.first->right) stk.push({t.first->right, t.second+1});
28+
// }
29+
// return res;
30+
// }
31+
// };
32+
33+
// Using Recursion
34+
class Solution {
35+
public:
36+
int maxDepth(TreeNode* root) {
37+
if (root == NULL) return 0;
38+
return max(maxDepth(root->left)+1, maxDepth(root->right)+1);
39+
}
40+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int n = prices.size();
5+
// Preprocessing
6+
// Polulate a smallest from beginning array
7+
// Polulate a largest from the end array
8+
vector<int> smallest(n, -1), largest(n, -1);
9+
10+
smallest[0] = prices[0];
11+
largest[n-1] = prices[n-1];
12+
13+
for (int i = 1; i < n; i++) {
14+
smallest[i] = min(smallest[i-1], prices[i]);
15+
}
16+
17+
for (int i = n-2; i>=0; i--) {
18+
largest[i] = max(largest[i+1], prices[i]);
19+
}
20+
21+
// Logic
22+
// Max difference b/w the smallest and largest element
23+
int res = 0;
24+
for(int i=0; i<n; i++) res = max(res, largest[i]-smallest[i]);
25+
26+
return res;
27+
28+
}
29+
};

cpp/125-Valid-Palindrome.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(string s) {
4+
int start = 0, end = s.length()-1;
5+
6+
while (start < end) {
7+
char a = s[start], b = s[end];
8+
9+
// Convert to caps
10+
if (a >= 'a' && a <= 'z') a = a-'a'+'A';
11+
if (b >= 'a' && b <= 'z') b = b - 'a' + 'A';
12+
13+
// Logic
14+
if ( (a < 'A' || a > 'Z') && (a < '0' || a > '9') ) {
15+
start ++;
16+
continue;
17+
}
18+
if ( (b < 'A' || b > 'Z') && (b < '0' || b > '9') ) {
19+
end --;
20+
continue;
21+
}
22+
if(a != b) return false;
23+
start ++;
24+
end --;
25+
}
26+
27+
return true;
28+
}
29+
};

cpp/167-Two-Sum-II.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Window Method
2+
3+
class Solution {
4+
private:
5+
6+
public:
7+
vector<int> twoSum(vector<int>& arr, int target) {
8+
int i = 0, j = arr.size()-1;
9+
10+
while(i < j){
11+
if(arr[i] + arr[j] == target) return {i+1,j+1};
12+
if(arr[i] + arr[j] > target) {
13+
j--;
14+
}
15+
else {i++;}
16+
}
17+
return {};
18+
}
19+
20+
};

cpp/20-Valid-Parentheses.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool isValid(string s) {
4+
stack<char> stk;
5+
for(auto a: s) {
6+
if (a=='(' || a=='{' || a=='[') {
7+
stk.push(a);
8+
}
9+
else {
10+
if(stk.empty()) return false;
11+
if( (stk.top() == '(' && a==')') || (stk.top() == '{' && a=='}') || (stk.top() == '[' && a==']') )
12+
stk.pop();
13+
else return false;
14+
}
15+
}
16+
17+
return stk.empty();
18+
}
19+
};

cpp/206-Reverse-Linked-List.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* reverseList(ListNode* head) {
14+
ListNode* rev = new ListNode(0);
15+
ListNode* ptr = head;
16+
17+
while(ptr) {
18+
auto temp = ptr;
19+
ptr=ptr->next;
20+
temp-> next = rev->next;
21+
rev->next = temp;
22+
}
23+
24+
return rev->next;
25+
}
26+
};

cpp/21-Merge-Two-Sorted-Lists.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
ListNode* res = new ListNode(0);
15+
auto ptr = res;
16+
17+
auto ptr1 = list1;
18+
auto ptr2 = list2;
19+
20+
while (ptr1 && ptr2) {
21+
if (ptr1->val < ptr2->val) {
22+
res->next = ptr1;
23+
ptr1 = ptr1->next;
24+
res = res->next;
25+
}
26+
else {
27+
res->next = ptr2;
28+
ptr2 = ptr2->next;
29+
res = res->next;
30+
}
31+
}
32+
33+
while (ptr1) {
34+
res->next = ptr1;
35+
res = res->next;
36+
ptr1 = ptr1->next;
37+
}
38+
while (ptr2) {
39+
res->next = ptr2;
40+
res = res->next;
41+
ptr2 = ptr2->next;
42+
}
43+
44+
return ptr->next;
45+
}
46+
};

cpp/217-Contains-Duplicate.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool containsDuplicate(vector<int>& nums) {
4+
unordered_set<int> s;
5+
for(auto n: nums){
6+
if(s.find(n) != s.end()) return true;
7+
8+
s.insert(n);
9+
}
10+
11+
return false;
12+
}
13+
};

0 commit comments

Comments
 (0)