Skip to content

Commit bfd139e

Browse files
committed
All Leetcode Submissions
1 parent 35dd28a commit bfd139e

File tree

617 files changed

+16305
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

617 files changed

+16305
-0
lines changed

01_Matrix/submission1.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
4+
int m = mat.size(), n = mat[0].size();
5+
vector<vector<bool>> visited(m, vector<bool>(n, false));
6+
queue<pair<int, pair<int, int>>> q;
7+
vector<vector<int>> ans(m, vector<int>(n));
8+
9+
for (int i=0; i<m; i++){
10+
for (int j=0; j<n; j++){
11+
if (mat[i][j] == 0){
12+
visited[i][j] = true;
13+
q.push({0 , {i, j}});
14+
}
15+
}
16+
}
17+
18+
while (!q.empty()){
19+
pair<int, pair<int, int>> front = q.front();
20+
int value = front.first, x = front.second.first, y = front.second.second;
21+
vector<pair<int, int>> neighbours = {{x-1, y}, {x+1, y}, {x, y-1}, {x, y+1}};
22+
23+
q.pop();
24+
ans[x][y] = value;
25+
26+
for (auto neighbour : neighbours){
27+
int newX = neighbour.first, newY = neighbour.second, newValue = value + 1;
28+
if (newX>=0 && newY>=0 && newX<m && newY<n && !visited[newX][newY]){
29+
q.push({newValue, {newX, newY}});
30+
visited[newX][newY] = true;
31+
}
32+
}
33+
}
34+
35+
return ans;
36+
}
37+
};

3Sum/submission1.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> threeSum(vector<int>& nums) {
4+
5+
sort(nums.begin(), nums.end());
6+
int n = nums.size();
7+
vector<vector<int>> ans;
8+
9+
for (int i=0; i<n; i++){
10+
if (i>0 && nums[i]==nums[i-1]) continue;
11+
int j = i+1, k = n-1;
12+
while (j<k){
13+
int sum = nums[i] + nums[j] + nums[k];
14+
if (sum==0){
15+
ans.push_back({nums[i], nums[j++], nums[k--]});
16+
while (j < k && nums[j]==nums[j-1]){
17+
j++;
18+
}
19+
20+
while (j < k && nums[k]==nums[k+1]){
21+
k--;
22+
}
23+
} else if (sum < 0){
24+
j++;
25+
while (j < k && nums[j]==nums[j-1]){
26+
j++;
27+
}
28+
} else {
29+
k--;
30+
while (j < k && nums[k]==nums[k+1]){
31+
k--;
32+
}
33+
}
34+
}
35+
}
36+
37+
return ans;
38+
}
39+
};

4Sum/submission1.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> fourSum(vector<int>& nums, int target) {
4+
vector<vector<int>> ans;
5+
int n = nums.size();
6+
7+
sort(nums.begin(), nums.end());
8+
9+
for (int i=0; i<n; i++){
10+
if (i!=0 && nums[i]==nums[i-1]){
11+
continue;
12+
}
13+
for (int j=i+3; j<n; j++){
14+
if (j+1<n && nums[j]==nums[j+1]){
15+
continue;
16+
}
17+
int k = i+1, l = j-1;
18+
while (k<l){
19+
long sum = (long)nums[i] + nums[j] + nums[k] + nums[l];
20+
// cout << nums[i] << " " << nums[k] << " " << nums[l] << " " << nums[j] << endl;
21+
if (sum == (long)target){
22+
vector<int> temp = {nums[i], nums[j], nums[k], nums[l]};
23+
ans.push_back(temp);
24+
k++;
25+
l--;
26+
while (k<l && nums[k]==nums[k-1]){
27+
k++;
28+
}
29+
while (k<l && nums[l]==nums[l+1]){
30+
l--;
31+
}
32+
} else if (sum < (long)target){
33+
k++;
34+
} else {
35+
l--;
36+
}
37+
}
38+
}
39+
}
40+
41+
return ans;
42+
}
43+
};

Add_Binary/submission1.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
string addBinary(string a, string b) {
4+
5+
int carry = 0;
6+
int i = a.length()-1;
7+
int j = b.length()-1;
8+
9+
string ans;
10+
11+
while (carry>0 || i>=0 || j>=0){
12+
13+
int value = carry;
14+
15+
if (i>=0){
16+
value = value + a[i]-'0';
17+
i--;
18+
}
19+
if (j>=0){
20+
value = value + b[j]-'0';
21+
j--;
22+
}
23+
24+
if (value%2){
25+
ans.push_back('1');
26+
}
27+
else{
28+
ans.push_back('0');
29+
}
30+
31+
if (value<2){
32+
carry = 0;
33+
}
34+
else{
35+
carry = 1;
36+
}
37+
}
38+
39+
reverse(ans.begin(),ans.end());
40+
return ans;
41+
}
42+
};

Add_Digits/submission1.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int addDigits(int num) {
4+
if (num<10){
5+
return num;
6+
}
7+
else{
8+
int sum = 0;
9+
while (num>0){
10+
sum = sum + num%10;
11+
num = num/10;
12+
}
13+
return addDigits(sum);
14+
}
15+
}
16+
};

Add_Strings/submission1.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string addStrings(string num1, string num2) {
4+
int carry = 0, n = num1.size(), m = num2.size();
5+
string ans = "";
6+
7+
reverse(num1.begin(), num1.end());
8+
reverse(num2.begin(), num2.end());
9+
10+
for (int i=0; i<max(m,n); i++){
11+
int digit = carry;
12+
if (i < n) digit = digit + num1[i] - '0';
13+
if (i < m) digit = digit + num2[i] - '0';
14+
ans.push_back(digit%10 + '0');
15+
carry = digit/10;
16+
}
17+
18+
if (carry){
19+
ans.push_back(carry + '0');
20+
}
21+
22+
reverse(ans.begin(), ans.end());
23+
return ans;
24+
}
25+
};

Add_Two_Numbers/submission1.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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* addTwoNumbers(ListNode* l1, ListNode* l2) {
14+
int carry = 0;
15+
ListNode *current = NULL, *head = NULL;
16+
while (l1 || l2){
17+
int sum = carry;
18+
if (l1) sum = sum + l1->val;
19+
if (l2) sum = sum + l2->val;
20+
int digit = sum%10;
21+
carry = sum/10;
22+
23+
if (l1 && l2){
24+
if (!current){
25+
head = current = new ListNode(digit);
26+
} else {
27+
current->next = new ListNode(digit);
28+
current = current->next;
29+
}
30+
l1 = l1->next;
31+
l2 = l2->next;
32+
} else if (l1){
33+
current->next = new ListNode(digit);
34+
current = current->next;
35+
l1 = l1->next;
36+
} else {
37+
current->next = new ListNode(digit);
38+
current = current->next;
39+
l2 = l2->next;
40+
}
41+
}
42+
43+
if (carry){
44+
current->next = new ListNode(1);
45+
}
46+
47+
return head;
48+
}
49+
};

Add_Two_Numbers/submission2.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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* findAns(ListNode* l1, ListNode* l2){
14+
15+
int carry = 0;
16+
ListNode* head = l1;
17+
while (true){
18+
19+
int value = carry + l1->val;
20+
if (l2!=NULL){
21+
value = value + l2->val;
22+
}
23+
l1->val = value%10;
24+
carry = value/10;
25+
if (l2!=NULL){
26+
l2 = l2->next;
27+
}
28+
29+
if (l1->next!=NULL){
30+
l1 = l1->next;
31+
}
32+
else{
33+
break;
34+
}
35+
}
36+
37+
if (carry){
38+
ListNode* lastNode = new ListNode(carry);
39+
l1->next = lastNode;
40+
}
41+
42+
return head;
43+
44+
}
45+
46+
int findLength(ListNode* head){
47+
int count = 0;
48+
while (head!=NULL){
49+
count++;
50+
head = head->next;
51+
}
52+
53+
return count;
54+
}
55+
56+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
57+
58+
int length1 = findLength(l1);
59+
int length2 = findLength(l2);
60+
61+
if (length1>length2){
62+
return findAns(l1,l2);
63+
}
64+
return findAns(l2,l1);
65+
66+
}
67+
};

Add_Two_Numbers/submission3.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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* addTwoNumbers(ListNode* l1, ListNode* l2) {
14+
15+
ListNode* head = new ListNode(0);
16+
ListNode* temp = head;
17+
int carry = 0;
18+
19+
while (l1!=NULL || l2!=NULL || carry!=0){
20+
21+
ListNode* nodeToInsert = new ListNode(0);
22+
int value = carry;
23+
24+
if (l1!=NULL){
25+
value = value + l1->val;
26+
l1 = l1->next;
27+
}
28+
if (l2!=NULL){
29+
value = value + l2->val;
30+
l2 = l2->next;
31+
}
32+
33+
nodeToInsert->val = value%10;
34+
carry = value/10;
35+
temp->next = nodeToInsert;
36+
temp = temp->next;
37+
}
38+
39+
ListNode* newhead = head->next;
40+
head->next = NULL;
41+
delete head;
42+
43+
return newhead;
44+
}
45+
};

0 commit comments

Comments
 (0)