Initial commit

This commit is contained in:
Arkaprabha Chakraborty
2022-07-26 16:15:18 +05:30
commit cef4c4dcc4
65 changed files with 1806 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
#include <array>
#include <string>
class Solution {
public:
std::string addBinary(std::string a, std::string b) {
std::string r("");
bool c = 0;
for (int i = a.size() - 1, j = b.size() - 1; i >= 0 || j >= 0; --i, --j) {
bool d = i >= 0 ? a[i] == '1' : 0;
bool e = j >= 0 ? b[j] == '1' : 0;
r = ((bool)(d ^ e ^ c) ? '1' : '0') + r;
c = (d & e) || (e & c) || (d & c);
}
if (c) {
r = (c ? '1' : '0') + r;
}
return r;
}
};

View File

@@ -0,0 +1,14 @@
class Solution {
public:
int maxProfit(std::vector<int> &prices) {
int prof = 0;
for (int i = prices.size() - 1, max = 0; i >= 0; --i) {
if (prices[i] > max)
max = prices[i];
else if (max - prices[i] > prof)
prof = max - prices[i];
}
return prof;
}
};

View File

@@ -0,0 +1,31 @@
#include <vector>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
private:
void traverse(std::vector<int> &list, TreeNode *root) {
if (root == nullptr) {
return;
} else {
traverse(list, root->left);
list.push_back(root->val);
traverse(list, root->right);
}
}
public:
std::vector<int> inorderTraversal(TreeNode *root) {
std::vector<int> list;
traverse(list, root);
return list;
}
};

View File

@@ -0,0 +1,31 @@
#include <vector>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
private:
void traverse(std::vector<int> &list, TreeNode *root) {
if (root == nullptr) {
return;
} else {
traverse(list, root->left);
traverse(list, root->right);
list.push_back(root->val);
}
}
public:
std::vector<int> postorderTraversal(TreeNode *root) {
std::vector<int> list;
traverse(list, root);
return list;
}
};

View File

@@ -0,0 +1,31 @@
#include <vector>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
private:
void traverse(std::vector<int> &list, TreeNode *root) {
if (root == nullptr) {
return;
} else {
list.push_back(root->val);
traverse(list, root->left);
traverse(list, root->right);
}
}
public:
std::vector<int> preorderTraversal(TreeNode *root) {
std::vector<int> list;
traverse(list, root);
return list;
}
};

View File

@@ -0,0 +1,19 @@
class Solution {
public:
bool containsDuplicate(std::vector<int> &nums) {
std::unordered_set<int> unset;
int size = nums.size();
for (int i = 0; i < size; ++i) {
int occur = unset.count(nums[i]);
if (occur == 1) {
return true;
} else {
unset.insert(nums[i]);
}
}
return false;
}
};

View File

@@ -0,0 +1,9 @@
class Solution {
public int titleToNumber(String ttl) {
int num = 0;
for (int i = 0; i < ttl.length(); i++)
num += Math.pow(26, i) * (int) (ttl.charAt(ttl.length() - i - 1) - 64);
return num;
}
}

View File

@@ -0,0 +1,14 @@
class Solution {
public String convertToTitle(int num) {
StringBuilder ttl = new StringBuilder();
while (num > 0) {
--num;
char tba = (char) (num % 26 + 65);
ttl.insert(0, tba);
num /= 26;
}
return ttl.toString();
}
}

View File

@@ -0,0 +1,48 @@
#include <string>
#include <unordered_set>
class Solution {
public:
/*
int firstUniqChar(std::string line) {
std::unordered_map<char, int[2]> hmap;
for (int i = 0; i < line.size(); ++i) {
hmap[line[i]][0] = i;
++hmap[line[i]][1];
}
int result = INT32_MAX;
for (int i = (int)'a'; i <= (int)'z'; ++i) {
if (hmap[(char)i][1] == 1 && hmap[(char)i][0] == 0) {
return 0;
} else if (hmap[(char)i][1] == 1 && hmap[(char)i][0] < result) {
result = hmap[(char)i][0];
}
}
return result != INT32_MAX ? result : -1;
}
*/
int firstUniqChar(std::string line) {
std::unordered_set<char> hmap;
int index = 0;
while (index < line.size()) {
char letter = line[index];
if (hmap.find(letter) == hmap.end() &&
line.find(letter, index + 1) == -1) {
return index;
} else if (line.find(letter, index + 1) != -1) {
index = line.find_first_not_of(letter, index + 1);
hmap.insert(letter);
} else {
++index;
}
}
return -1;
}
};

View File

@@ -0,0 +1,43 @@
#include <stack>
class MyQueue {
private:
std::stack<int> shelfA;
std::stack<int> shelfB;
public:
MyQueue() {}
void push(int data) { shelfA.push(data); }
int pop() {
while (shelfA.size() != 1) {
shelfB.push(shelfA.top());
shelfA.pop();
}
int result = shelfA.top();
shelfA.pop();
while (!shelfB.empty()) {
shelfA.push(shelfB.top());
shelfB.pop();
}
return result;
}
int peek() {
while (shelfA.size() != 1) {
shelfB.push(shelfA.top());
shelfA.pop();
}
int result = shelfA.top();
while (!shelfB.empty()) {
shelfA.push(shelfB.top());
shelfB.pop();
}
return result;
}
bool empty() { return shelfA.empty(); }
};

View File

@@ -0,0 +1,25 @@
#include <string>
class Solution {
public:
int strStr(std::string haystack, std::string needle) {
for (int i = 0; i < haystack.size(); ++i) {
if (haystack.at(i) == needle.at(0)) {
bool found = false;
for (int j = 0; i + j < haystack.size() && j < needle.size(); ++j) {
if (haystack.at(i + j) != needle.at(j)) {
break;
} else if (j == needle.size() - 1) {
found = true;
}
}
if (found) {
return i;
}
}
}
return -1;
}
};

View File

@@ -0,0 +1,43 @@
class Solution {
public:
/*
std::vector<int> intersect(std::vector<int> &nums1,
std::vector<int> &nums2) {
std::unordered_map<int, int> hmap;
std::vector<int> rslt;
for (int i = 0; i < nums1.size(); ++i)
++hmap[nums1[i]];
for (int i = 0; i < nums2.size(); ++i)
if (hmap[nums2[i]] > 0) {
--hmap[nums2[i]];
rslt.push_back(nums2[i]);
}
return rslt;
}
*/
std::vector<int> intersect(std::vector<int> &nums1,
std::vector<int> &nums2) {
std::sort(nums1.begin(), nums1.end());
std::sort(nums2.begin(), nums2.end());
std::vector<int> rslt;
int i = 0, j = 0;
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] == nums2[j]) {
rslt.push_back(nums1[i]);
++i;
++j;
} else if (nums1[i] < nums2[j]) {
++i;
} else if (nums1[i] > nums2[j]) {
++j;
}
}
return rslt;
}
};

View File

@@ -0,0 +1,25 @@
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
public:
TreeNode *invertTree(TreeNode *root) {
if (root != nullptr) {
invertTree(root->left);
invertTree(root->right);
TreeNode *copy = root->left;
root->left = root->right;
root->right = copy;
}
return root;
}
};

View File

@@ -0,0 +1,50 @@
#include <iostream>
#include <unordered_map>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
// bool hasCycle(ListNode *head) {
// std::unordered_map<ListNode *, int> hmap;
// ListNode *curr = head;
// while (curr != NULL) {
// if (++hmap[curr] == 2) {
// return true;
// } else {
// curr = curr->next;
// }
// }
//
// return false;
// }
bool hasCycle(ListNode *head) {
if (head == NULL) {
return false;
}
ListNode *slow = head, *fast = head;
int taken = 0, limit = 2;
while (fast->next != NULL) {
fast = fast->next;
++taken;
if (slow == fast) {
return true;
}
if (taken == limit) {
taken = 0;
limit *= 2;
slow = fast;
}
}
return false;
}
};

View File

@@ -0,0 +1,21 @@
class Solution {
public String longestCommonPrefix(String[] strs) {
int len = strs.length;
if (len == 0)
return "";
if (len == 1)
return strs[0];
Arrays.sort(strs);
int end = Math.min(strs[0].length(), strs[len - 1].length());
int i = 0;
while (i < end && strs[0].charAt(i) == strs[len - 1].charAt(i))
i++;
return strs[0].substring(0, i);
}
}

View File

@@ -0,0 +1,33 @@
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
private:
void maxDepth(TreeNode *root, int index, int *max) {
if (root == nullptr) {
if (index > *max) {
*max = index;
}
return;
} else {
maxDepth(root->left, index + 1, max);
maxDepth(root->right, index + 1, max);
}
}
public:
int maxDepth(TreeNode *root) {
int max = 0;
maxDepth(root, 0, &max);
return max;
}
};

View File

@@ -0,0 +1,20 @@
class Solution {
public int canBeTypedWords(String str, String chars) {
StringTokenizer words = new StringTokenizer(str, " ");
int ret = 0;
for (boolean flag = true; words.hasMoreTokens(); flag = true) {
String word = words.nextToken();
for (int i = 0; i < chars.length(); i++)
if (word.indexOf(chars.charAt(i)) > -1) {
flag = false;
break;
}
if (flag)
++ret;
}
return ret;
}
}

View File

@@ -0,0 +1,15 @@
class Solution {
public:
int maxSubArray(std::vector<int> &nums) {
int max = INT32_MIN;
int tmax = 0;
for (int i = 0; i < nums.size(); ++i) {
tmax = tmax + nums[i];
if (max < tmax)
max = tmax;
if (tmax < 0)
tmax = 0;
}
return max;
}
};

View File

@@ -0,0 +1,10 @@
class Solution {
public:
void merge(std::vector<int> &nums1, int m, std::vector<int> &nums2, int n) {
for (int i = 0; i < n; ++i) {
nums1[m + i] = nums2[i];
}
std::sort(nums1.begin(), nums1.end());
}
};

View File

@@ -0,0 +1,24 @@
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) {
if (list1 == nullptr) {
return list2;
} else if (list2 == nullptr) {
return list1;
} else if (list1->val < list2->val) {
list1->next = mergeTwoLists(list1->next, list2);
return list1;
} else {
list2->next = mergeTwoLists(list1, list2->next);
return list2;
}
}
};

View File

@@ -0,0 +1,13 @@
bool isPalindrome(long int a) {
if (a < 0)
return false;
long int b = 0;
for (long int c = a; c != 0; c /= 10)
b = b * 10 + (c % 10);
if (a == b)
return true;
else
return false;
}

View File

@@ -0,0 +1,15 @@
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) {
return false;
} else {
long int y = 0;
for (int i = x; i != 0; i /= 10) {
y = y * 10 + i % 10;
}
return x == (int)y;
}
}
};

View File

@@ -0,0 +1,23 @@
class Solution {
public:
std::vector<int> getRow(int n) {
std::vector<int> prow = {1};
for (int i = 1, prev = 1; i < n + 1; ++i) {
int curr = (prev * (n - i + 1)) / i;
prow.push_back(curr);
prev = curr;
}
return prow;
}
std::vector<std::vector<int>> generate(int numRows) {
std::vector<std::vector<int>> rslt;
for (int i = 0; i < numRows; ++i)
rslt.push_back(getRow(i));
return rslt;
}
};

View File

@@ -0,0 +1,23 @@
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
public:
bool hasPathSum(TreeNode *root, int targetSum) {
if (root == nullptr) {
return false;
} else if (root->left == nullptr && root->right == nullptr) {
return targetSum == root->val;
} else {
return hasPathSum(root->left, targetSum - root->val) ||
hasPathSum(root->right, targetSum - root->val);
}
}
};

View File

@@ -0,0 +1,18 @@
#include <vector>
class Solution {
public:
std::vector<int> plusOne(std::vector<int> &digits) {
++digits[digits.size() - 1];
for (int i = digits.size() - 1; i >= 0 && digits[i] == 10; --i) {
digits[i] = 0;
if (i != 0) {
++digits[i - 1];
} else {
digits.insert(digits.begin(), 1);
}
}
return digits;
}
};

View File

@@ -0,0 +1,21 @@
#include <string>
#include <unordered_map>
class Solution {
public:
bool canConstruct(std::string ransomNote, std::string magazine) {
std::unordered_map<char, int> hmap[2];
for (int i = 0; i < magazine.size(); ++i) {
++hmap[0][magazine[i]];
}
for (int i = 0; i < ransomNote.size(); ++i) {
if (hmap[0][ransomNote[i]] < ++hmap[1][ransomNote[i]]) {
return false;
}
}
return true;
}
};

View File

@@ -0,0 +1,13 @@
class Solution {
public int removeDuplicates(int[] nums) {
int k = 0;
for (int i = 0, last = -101; i < nums.length; ++i) {
if (last != nums[i]) {
nums[k++] = nums[i];
last = nums[i];
}
}
return k;
}
}

View File

@@ -0,0 +1,18 @@
#include <iostream>
#include <unordered_set>
#include <vector>
class Solution {
public:
int removeDuplicates(std::vector<int> &nums) {
int k = 0, l = INT32_MIN;
for (auto iter = nums.begin(); iter != nums.end(); iter.operator++()) {
if (l != *iter) {
nums[k++] = *iter;
l = *iter;
}
}
return k;
}
};

View File

@@ -0,0 +1,33 @@
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
private:
ListNode *removeElements(ListNode *head, int val) {
if (head == nullptr) {
return nullptr;
} else if (head->val == val) {
return removeElements(head->next, val);
} else {
head->next = removeElements(head->next, val);
return head;
}
}
public:
ListNode *deleteDuplicates(ListNode *head) {
for (ListNode *curr = head; curr != nullptr && curr->next != nullptr;
curr = curr->next) {
if (curr->val == curr->next->val) {
curr->next = removeElements(curr->next, curr->val);
}
}
return head;
}
};

View File

@@ -0,0 +1,21 @@
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode *removeElements(ListNode *head, int val) {
if (head == nullptr) {
return nullptr;
} else if (head->val == val) {
return removeElements(head->next, val);
} else {
head->next = removeElements(head->next, val);
return head;
}
}
};

View File

@@ -0,0 +1,29 @@
class Solution {
public:
std::vector<std::vector<int>>
matrixReshape(std::vector<std::vector<int>> &mat, int r, int c) {
int m = mat.size(), n = mat[0].size();
if (m * n == r * c) {
std::vector<std::vector<int>> rslt(r, std::vector<int>(c));
for (int i = 0, k = 0, l = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rslt[k][l] = mat[i][j];
if (l < c) {
++l;
}
if (l == c) {
l = 0;
++k;
}
}
}
return rslt;
} else {
return mat;
}
}
};

View File

@@ -0,0 +1,22 @@
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode *reverseList(ListNode *head) {
ListNode *prev = nullptr, *next = nullptr, *curr = head;
while (curr != nullptr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};

View File

@@ -0,0 +1,55 @@
class RomanToInteger {
int applyOffset(int value, int last_value, int offset) {
if (last_value > value) {
return offset - value;
} else {
return offset + value;
}
}
public int romanToInt(String roman) {
int integer = 0;
int last_value = 0;
for (int x = roman.length() - 1; x >= 0; x--) {
char convertToDecimal = roman.charAt(x);
switch (convertToDecimal) {
case 'M':
integer = applyOffset(1000, last_value, integer);
last_value = 1000;
break;
case 'D':
integer = applyOffset(500, last_value, integer);
last_value = 500;
break;
case 'C':
integer = applyOffset(100, last_value, integer);
last_value = 100;
break;
case 'L':
integer = applyOffset(50, last_value, integer);
last_value = 50;
break;
case 'X':
integer = applyOffset(10, last_value, integer);
last_value = 10;
break;
case 'V':
integer = applyOffset(5, last_value, integer);
last_value = 5;
break;
case 'I':
integer = applyOffset(1, last_value, integer);
last_value = 1;
break;
}
}
return integer;
}
}

View File

@@ -0,0 +1,24 @@
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
public:
TreeNode *searchBST(TreeNode *root, int val) {
if (root == nullptr) {
return nullptr;
} else if (root->val > val) {
return searchBST(root->left, val);
} else if (root->val < val) {
return searchBST(root->right, val);
} else {
return root;
}
}
};

View File

@@ -0,0 +1,24 @@
#include <vector>
class Solution {
public:
int searchInsert(std::vector<int> &nums, int target) {
int l = 0;
int h = nums.size() - 1;
int m = l + (h - l) / 2;
while (l <= h) {
if (nums.at(m) == target) {
break;
} else if (nums.at(m) < target) {
l = m + 1;
} else {
h = m - 1;
}
m = l + (h - l) / 2;
}
return m;
}
};

View File

@@ -0,0 +1,30 @@
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
class Solution {
private:
bool isSymmetric(TreeNode *left, TreeNode *right) {
if (left == nullptr && right == nullptr) {
return true;
} else if (left == nullptr || right == nullptr) {
return false;
} else if (left->val == right->val) {
return isSymmetric(left->left, right->right) &&
isSymmetric(left->right, right->left);
} else {
return false;
}
}
public:
bool isSymmetric(TreeNode *root) {
return isSymmetric(root->left, root->right);
}
};

View File

@@ -0,0 +1,10 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++)
for (int j = i + 1; j < nums.length; j++)
if (target - nums[i] == nums[j])
return new int[] { i, j };
return new int[] { -1, -1 };
}
}

18
Easy/two-sum/solution.cpp Normal file
View File

@@ -0,0 +1,18 @@
class Solution {
public:
std::vector<int> twoSum(std::vector<int> &nums, int target) {
std::unordered_map<int, int> hmap;
for (int i = 0; i < nums.size(); ++i) {
int diff = target - nums[i];
if (hmap.find(diff) != hmap.end()) {
return {hmap[diff], i};
} else {
hmap[nums[i]] = i;
}
}
return {-1, -1};
}
};

View File

@@ -0,0 +1,25 @@
#include <string>
#include <unordered_map>
class Solution {
public:
bool isAnagram(std::string s, std::string t) {
if (s.size() == t.size()) {
std::unordered_map<char, int> hmap[2];
for (int i = 0; i < s.size(); ++i) {
++hmap[0][s[i]];
++hmap[1][t[i]];
}
for (int i = (int)'a'; i <= (int)'z'; ++i) {
if (hmap[0][(char)i] != hmap[1][(char)i]) {
return false;
}
}
return true;
}
return false;
}
};

View File

@@ -0,0 +1,24 @@
#include <iostream>
#include <stack>
class Solution {
public:
bool isValid(std::string line) {
std::stack<char> shelf;
for (auto iter = line.begin(); iter != line.end(); iter.operator++()) {
if (*iter == '(' || *iter == '{' || *iter == '[') {
shelf.push(*iter);
} else if (shelf.empty()) {
return false;
} else if ((*iter == ')' && shelf.top() == '(') ||
(*iter == '}' && shelf.top() == '{') ||
(*iter == ']' && shelf.top() == '[')) {
shelf.pop();
} else {
return false;
}
}
return shelf.empty();
}
};