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();
}
};

View File

@@ -0,0 +1,21 @@
class Solution {
public int longestValidParentheses(String str) {
Stack<Integer> stack = new Stack<>();
stack.push(-1);
int max = 0;
for (int cur = 0; cur < str.length(); cur++) {
if (str.charAt(cur) == '(') {
stack.push(cur);
} else {
stack.pop();
if (stack.empty()) {
stack.push(cur);
} else {
max = Math.max(max, cur - stack.peek());
}
}
}
return max;
}
}

View File

@@ -0,0 +1,26 @@
#include <stack>
#include <string>
class Solution {
public:
int longestValidParentheses(std::string line) {
std::stack<int> shelf;
shelf.push(-1);
int max = 0;
for (int i = 0; i < line.length(); ++i) {
if (line[i] == '(') {
shelf.push(i);
} else {
shelf.pop();
if (shelf.empty()) {
shelf.push(i);
} else if (max < i - shelf.top()) {
max = i - shelf.top();
}
}
}
return max;
}
};

View File

@@ -0,0 +1,57 @@
class Solution {
char[][] BOARD;
boolean isValid(int row, int col, char num) {
for (int i = 0; i < 9; i++) {
if (BOARD[i][col] == num)
return false;
if (BOARD[row][i] == num)
return false;
}
row = row - row % 3;
col = col - col % 3;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (BOARD[i + row][j + col] == num)
return false;
return true;
}
int[] nextEmpty() {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (BOARD[i][j] == '.')
return new int[] { i, j };
return new int[] { -1, -1 };
}
boolean solver() {
int[] tmp = nextEmpty();
if (tmp[0] == -1)
return true;
int row = tmp[0];
int col = tmp[1];
for (int i = 1; i <= 9; i++) {
if (isValid(row, col, (char) (48 + i))) {
BOARD[row][col] = (char) (48 + i);
if (solver())
return true;
}
BOARD[row][col] = '.';
}
return false;
}
public void solveSudoku(char[][] board) {
BOARD = board;
solver();
}
}

View File

@@ -0,0 +1,67 @@
#include <array>
#include <vector>
class Solution {
public:
std::array<int, 2> nextEmpty(std::vector<std::vector<char>> &board) {
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] == '.') {
return {i, j};
}
}
}
return {-1, -1};
}
bool valid(std::vector<std::vector<char>> &board, int row, int col,
char dgt) {
for (int i = 0; i < 9; ++i) {
if (board[i][col] == dgt || board[row][i] == dgt) {
return false;
}
}
row = row - row % 3;
col = col - col % 3;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i + row][j + col] == dgt) {
return false;
}
}
}
return true;
}
bool boardFiller(std::vector<std::vector<char>> &board) {
std::array<int, 2> index = nextEmpty(board);
if (index[0] == -1) {
return true;
}
int row = index[0];
int col = index[1];
for (int i = 0; i < 9; ++i) {
if (valid(board, row, col, (char)((int)'1' + i))) {
board[row][col] = (char)((int)'1' + i);
if (boardFiller(board)) {
return true;
}
}
board[row][col] = '.';
}
return false;
}
void solveSudoku(std::vector<std::vector<char>> &board) {
boardFiller(board);
}
};

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Arkaprabha Chakraborty
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,44 @@
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
class Solution {
public ListNode addTwoNumbers(ListNode list1, ListNode list2) {
ListNode head = new ListNode(0);
ListNode cur = head;
int carry = 0;
while (list1 != null || list2 != null) {
int dig1 = list1 != null ? list1.val : 0;
int dig2 = list2 != null ? list2.val : 0;
int sum = dig1 + dig2 + carry;
cur.next = new ListNode(sum % 10);
cur = cur.next;
carry = sum / 10;
if (list1 != null)
list1 = list1.next;
if (list2 != null)
list2 = list2.next;
}
if (carry > 0)
cur.next = new ListNode(carry);
return head.next;
}
}

View File

@@ -0,0 +1,35 @@
#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 levelOrder(std::vector<std::vector<int>> &levels, TreeNode *root,
int index) {
if (root == nullptr) {
return;
} else if (levels.size() == index) {
levels.push_back(std::vector<int>{});
}
levels[index].push_back(root->val);
levelOrder(levels, root->left, index + 1);
levelOrder(levels, root->right, index + 1);
}
public:
std::vector<std::vector<int>> levelOrder(TreeNode *root) {
std::vector<std::vector<int>> levels;
levelOrder(levels, root, 0);
return levels;
}
};

View File

@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
class Solution {
public:
int maxArea(std::vector<int> &height) {
int left = 0;
int right = height.size() - 1;
int maxArea = 0;
int curArea = 0;
while (left < right) {
curArea =
(right - left) *
(height[left] < height[right] ? height[left] : height[right]);
maxArea = maxArea > curArea ? maxArea : curArea;
if (height[left] > height[right]) {
--right;
} else {
++left;
}
}
return maxArea;
}
};

View File

@@ -0,0 +1,25 @@
#include <vector>
class Solution {
public:
int equalPairs(std::vector<std::vector<int>> &grid) {
int count = 0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid.size(); ++j) {
bool match = true;
for (int k = 0; k < grid.size(); ++k) {
if (grid[i][k] != grid[k][j]) {
match = false;
break;
}
}
if (match) {
++count;
}
}
}
return count;
}
};

View File

@@ -0,0 +1,31 @@
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 *insertIntoBST(TreeNode *root, int val) {
TreeNode *node = root;
while (node != nullptr) {
if (val < node->val && node->left == nullptr) {
node->left = new TreeNode(val);
break;
} else if (val > node->val && node->right == nullptr) {
node->right = new TreeNode(val);
break;
} else if (val > node->val) {
node = node->right;
} else if (val < node->val) {
node = node->left;
}
}
return root != nullptr ? root : new TreeNode(val);
}
};

View File

@@ -0,0 +1,68 @@
#include <string>
#include <vector>
class Solution {
public:
std::vector<std::string> letterCombinations(std::string digits) {
std::string letters[] = {"abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};
std::vector<std::string> finvec;
if (digits.length() == 1) {
std::string tmplet = letters[(int)digits[0] - 50];
for (int i = 0; i < tmplet.length(); ++i) {
finvec.push_back(std::string("") + tmplet[i]);
}
}
else if (digits.length() == 2) {
std::string tmplet_0 = letters[(int)digits[0] - 50];
std::string tmplet_1 = letters[(int)digits[1] - 50];
for (int i = 0; i < tmplet_0.length(); ++i) {
for (int j = 0; j < tmplet_1.length(); ++j) {
finvec.push_back(std::string("") + tmplet_0[i] +
tmplet_1[j]);
}
}
}
else if (digits.length() == 3) {
std::string tmplet_0 = letters[(int)digits[0] - 50];
std::string tmplet_1 = letters[(int)digits[1] - 50];
std::string tmplet_2 = letters[(int)digits[2] - 50];
for (int i = 0; i < tmplet_0.length(); ++i) {
for (int j = 0; j < tmplet_1.length(); ++j) {
for (int k = 0; k < tmplet_2.length(); ++k) {
finvec.push_back(std::string("") + tmplet_0[i] +
tmplet_1[j] + tmplet_2[k]);
}
}
}
}
else if (digits.length() == 4) {
std::string tmplet_0 = letters[(int)digits[0] - 50];
std::string tmplet_1 = letters[(int)digits[1] - 50];
std::string tmplet_2 = letters[(int)digits[2] - 50];
std::string tmplet_3 = letters[(int)digits[3] - 50];
for (int i = 0; i < tmplet_0.length(); ++i) {
for (int j = 0; j < tmplet_1.length(); ++j) {
for (int k = 0; k < tmplet_2.length(); ++k) {
for (int l = 0; l < tmplet_3.length(); ++l) {
finvec.push_back(std::string("") + tmplet_0[i] +
tmplet_1[j] + tmplet_2[k] +
tmplet_3[l]);
}
}
}
}
}
return finvec;
}
};

View File

@@ -0,0 +1,28 @@
class Solution {
public String longestPalindrome(String line) {
if (line.length() < 2) {
return line;
} else {
int start = 0, size = 0, left = 0, right = 0, middle = 0;
while (middle < line.length()) {
left = right = middle;
while (right < line.length() - 1 && line.charAt(right) == line.charAt(right + 1)) {
++right;
}
middle = right + 1;
while (left > 0 && right < line.length() - 1 && line.charAt(left - 1) == line.charAt(right + 1)) {
--left;
++right;
}
if (right - left + 1 > size) {
start = left;
size = right - left + 1;
}
}
return line.substring(start, start + size);
}
}
}

View File

@@ -0,0 +1,33 @@
#include <string>
class Solution {
public:
std::string longestPalindrome(std::string line) {
if (line.size() < 2) {
return line;
} else {
int size = 0, start = 0, left = 0, right = 0, middle = 0;
while (middle < line.size()) {
left = middle, right = middle;
while (right < line.size() - 1 &&
line[right] == line[right + 1]) {
++right;
}
middle = right + 1;
while (right < line.size() - 1 && left > 0 &&
line[right + 1] == line[left - 1]) {
--left;
++right;
}
if (right - left + 1 > size) {
start = left;
size = right - left + 1;
}
}
return line.substr(start, size);
}
}
};

View File

@@ -0,0 +1,13 @@
class IntegerReverse {
public int reverse(int num) {
if (num == 0)
return 0;
try {
return Integer.parseInt(new StringBuilder(Integer.toString(Math.abs(num))).reverse().toString())
* (num < 0 ? -1 : 1);
} catch (NumberFormatException err) {
return 0;
}
}
}

View File

@@ -0,0 +1,53 @@
#include <vector>
class Solution {
private:
bool binarySearch(std::vector<int> &nums, int start, int end, int target) {
while (end >= start) {
int middle = start + (end - start) / 2;
if (nums[middle] == target) {
return true;
} else if (nums[middle] > target) {
end = middle - 1;
} else {
start = middle + 1;
}
}
return false;
}
/*
bool binarySearch(std::vector<int> &nums, int start, int end, int target) {
if (end >= start) {
int middle = start + (end - start) / 2;
if (nums[middle] == target) {
return true;
} else if (nums[middle] > target) {
return binarySearch(nums, start, middle - 1, target);
} else {
return binarySearch(nums, middle + 1, end, target);
}
}
return false;
}
*/
public:
bool searchMatrix(std::vector<std::vector<int>> &matrix, int target) {
int row = matrix.size();
int col = matrix[0].size();
for (int i = 0; i < row; ++i) {
if (target > matrix[i][col - 1]) {
continue;
} else {
return binarySearch(matrix[i], 0, col - 1, target);
}
}
return false;
}
};

View File

@@ -0,0 +1,9 @@
class Solution {
public int singleNonDuplicate(int[] nums) {
for (int i = 0; i < nums.length - 1; i += 2)
if (nums[i] != nums[i + 1])
return i;
return nums.length - 1;
}
}

View File

@@ -0,0 +1,32 @@
struct ListNode *merge(struct ListNode *left, struct ListNode *right) {
if (!left) {
return right;
}
if (!right) {
return left;
}
if (left->val < right->val) {
left->next = merge(left->next, right);
return left;
} else {
right->next = merge(left, right->next);
return right;
}
}
struct ListNode *sortList(struct ListNode *head) {
if (!head || !head->next) {
return head;
}
struct ListNode *fast, *midd, *bmid;
fast = midd = head;
while (fast && fast->next) {
bmid = midd;
fast = fast->next->next;
midd = midd->next;
}
bmid->next = NULL;
return merge(sortList(head), sortList(midd));
}

View File

@@ -0,0 +1,70 @@
class Solution {
public:
int myAtoi(std::string s) {
unsigned long value = 0;
bool found = false, minus = false, signi = false;
for (int i = 0, j = 0; i <= 200; ++i) {
char chr = s[i];
if (j > 18)
break;
else if ((int)chr == 0)
break;
else if (((int)chr == 43 || (int)chr == 45) && !found) {
if ((int)chr == 45)
minus = true;
found = true;
continue;
}
else if (((int)chr == 43 || (int)chr == 45) && found) {
break;
}
else if (((int)chr == 46 || ((int)chr >= 65 && (int)chr <= 90) ||
((int)chr >= 97 && (int)chr <= 122)) &&
!found) {
break;
}
else if (((int)chr == 46 || ((int)chr >= 65 && (int)chr <= 90) ||
((int)chr >= 97 && (int)chr <= 122)) &&
found) {
break;
}
else if ((int)chr == 32 && found)
break;
else if ((int)chr >= 48 && (int)chr <= 57) {
if ((int)chr != 48)
signi = true;
value = value * 10 + ((int)chr - 48);
found = true;
if (signi)
++j;
continue;
}
else if ((int)chr == 32 && !found)
continue;
}
if (value > 2147483648 && minus)
return -2147483648;
else if (minus)
return value * -1;
else if (value > 2147483647)
return 2147483647;
else
return value;
}
};

View File

@@ -0,0 +1,33 @@
class Solution {
public:
std::vector<int> twoSum(std::vector<int> &numbers, int target) {
int j = -1;
for (int i = 0; i < numbers.size(); ++i) {
int otherhalf = target - numbers[i];
j = binarySearch(numbers, 0, numbers.size() - 1, otherhalf);
if (j != -1 && i != j) {
if (i < j)
return std::vector{i + 1, j + 1};
else
return std::vector{j + 1, i + 1};
}
}
return std::vector{-1, -1};
}
int binarySearch(std::vector<int> &numbers, int start, int end, int key) {
if (start <= end) {
int middle = (start + end) / 2;
if (numbers[middle] == key)
return middle;
else if (numbers[middle] > key)
return binarySearch(numbers, start, middle - 1, key);
else
return binarySearch(numbers, middle + 1, end, key);
}
return -1;
}
};

View File

@@ -0,0 +1,66 @@
#include <iostream>
#include <unordered_map>
#include <vector>
class Solution {
public:
/*
bool isValidSudoku(std::vector<std::vector<char>> &board) {
for (int r = 0; r < 9; ++r) {
for (int c = 0; c < 9; ++c) {
char num = board[r][c];
if (num != '.') {
int count = 0;
for (int i = 0; i < 9; ++i) {
if (num == board[r][i]) {
++count;
}
if (num == board[i][c]) {
++count;
}
}
int rof = r - r % 3;
int cof = c - c % 3;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (num == board[i + rof][j + cof]) {
++count;
}
}
}
if (count != 3) {
return false;
}
}
}
}
return true;
}
*/
bool isValidSudoku(std::vector<std::vector<char>> &board) {
std::unordered_map<char, int[9]> count[3];
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
char num = board[i][j];
if (num != '.') {
if (count[0][num][i] != 0 || count[1][num][j] != 0 ||
count[2][num][i / 3 * 3 + j / 3] != 0) {
return false;
}
++count[0][num][i];
++count[1][num][j];
++count[2][num][i / 3 * 3 + j / 3];
}
}
}
return true;
}
};

View File

@@ -0,0 +1,29 @@
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 isValidBST(TreeNode *root, int *minptr, int *maxptr) {
if (root == nullptr) {
return true;
} else if ((maxptr != nullptr && *maxptr <= root->val) ||
(minptr != nullptr && *minptr >= root->val)) {
return false;
} else {
return isValidBST(root->left, minptr, &root->val) &&
isValidBST(root->right, &root->val, maxptr);
}
}
public:
bool isValidBST(TreeNode *root) {
return isValidBST(root, nullptr, nullptr);
}
};

View File

@@ -0,0 +1,26 @@
class Solution {
public String convert(String str, int rows) {
String[] zigzag = new String[rows];
for (int i = 0; i < rows; i++)
zigzag[i] = "";
for (int i = 0, j = 0, k = 0; i < str.length(); i++) {
zigzag[j] += str.charAt(i);
if (j == rows - 1)
k = 1;
if (j == 0)
k = 0;
if (k == 1 && j > 0)
j--;
if (k == 0 && j < rows - 1)
j++;
}
String ret = "";
for (int i = 0; i < rows; i++)
ret += zigzag[i];
return ret;
}
}

5
README.md Normal file
View File

@@ -0,0 +1,5 @@
# Preview
![](blob/preview.jpg)
# Description
All the LeetCode problems I have solved.

BIN
blob/preview.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 694 KiB