commit cef4c4dcc4aa194b7c7463ba21981881ed46660a Author: Arkaprabha Chakraborty Date: Tue Jul 26 16:15:18 2022 +0530 Initial commit diff --git a/Easy/add-binary/solution.cpp b/Easy/add-binary/solution.cpp new file mode 100644 index 0000000..551d3ed --- /dev/null +++ b/Easy/add-binary/solution.cpp @@ -0,0 +1,23 @@ +#include +#include + +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; + } +}; diff --git a/Easy/best-time-to-buy-and-sell-stock/solution.cpp b/Easy/best-time-to-buy-and-sell-stock/solution.cpp new file mode 100644 index 0000000..f643e3b --- /dev/null +++ b/Easy/best-time-to-buy-and-sell-stock/solution.cpp @@ -0,0 +1,14 @@ +class Solution { + public: + int maxProfit(std::vector &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; + } +}; diff --git a/Easy/binary-tree-inorder-traversal/solution.cpp b/Easy/binary-tree-inorder-traversal/solution.cpp new file mode 100644 index 0000000..24d4643 --- /dev/null +++ b/Easy/binary-tree-inorder-traversal/solution.cpp @@ -0,0 +1,31 @@ +#include + +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 &list, TreeNode *root) { + if (root == nullptr) { + return; + } else { + traverse(list, root->left); + list.push_back(root->val); + traverse(list, root->right); + } + } + + public: + std::vector inorderTraversal(TreeNode *root) { + std::vector list; + traverse(list, root); + return list; + } +}; diff --git a/Easy/binary-tree-postorder-traversal/solution.cpp b/Easy/binary-tree-postorder-traversal/solution.cpp new file mode 100644 index 0000000..3804ac9 --- /dev/null +++ b/Easy/binary-tree-postorder-traversal/solution.cpp @@ -0,0 +1,31 @@ +#include + +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 &list, TreeNode *root) { + if (root == nullptr) { + return; + } else { + traverse(list, root->left); + traverse(list, root->right); + list.push_back(root->val); + } + } + + public: + std::vector postorderTraversal(TreeNode *root) { + std::vector list; + traverse(list, root); + return list; + } +}; diff --git a/Easy/binary-tree-preorder-traversal/solution.cpp b/Easy/binary-tree-preorder-traversal/solution.cpp new file mode 100644 index 0000000..eaa4bf2 --- /dev/null +++ b/Easy/binary-tree-preorder-traversal/solution.cpp @@ -0,0 +1,31 @@ +#include + +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 &list, TreeNode *root) { + if (root == nullptr) { + return; + } else { + list.push_back(root->val); + traverse(list, root->left); + traverse(list, root->right); + } + } + + public: + std::vector preorderTraversal(TreeNode *root) { + std::vector list; + traverse(list, root); + return list; + } +}; diff --git a/Easy/contains-duplicate/solution.cpp b/Easy/contains-duplicate/solution.cpp new file mode 100644 index 0000000..c8d6db8 --- /dev/null +++ b/Easy/contains-duplicate/solution.cpp @@ -0,0 +1,19 @@ +class Solution { + public: + bool containsDuplicate(std::vector &nums) { + std::unordered_set 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; + } +}; diff --git a/Easy/excel-sheet-column-number/Solution.java b/Easy/excel-sheet-column-number/Solution.java new file mode 100644 index 0000000..04b44a6 --- /dev/null +++ b/Easy/excel-sheet-column-number/Solution.java @@ -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; + } +} diff --git a/Easy/excel-sheet-column-title/Solution.java b/Easy/excel-sheet-column-title/Solution.java new file mode 100644 index 0000000..e7d789b --- /dev/null +++ b/Easy/excel-sheet-column-title/Solution.java @@ -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(); + } +} diff --git a/Easy/first-unique-character-in-a-string/solution.cpp b/Easy/first-unique-character-in-a-string/solution.cpp new file mode 100644 index 0000000..27aa156 --- /dev/null +++ b/Easy/first-unique-character-in-a-string/solution.cpp @@ -0,0 +1,48 @@ +#include +#include + +class Solution { + public: + /* + int firstUniqChar(std::string line) { + std::unordered_map 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 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; + } +}; diff --git a/Easy/implement-queue-using-stacks/solution.cpp b/Easy/implement-queue-using-stacks/solution.cpp new file mode 100644 index 0000000..df5e166 --- /dev/null +++ b/Easy/implement-queue-using-stacks/solution.cpp @@ -0,0 +1,43 @@ +#include + +class MyQueue { + private: + std::stack shelfA; + std::stack 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(); } +}; diff --git a/Easy/implement-strstr/solution.cpp b/Easy/implement-strstr/solution.cpp new file mode 100644 index 0000000..211b229 --- /dev/null +++ b/Easy/implement-strstr/solution.cpp @@ -0,0 +1,25 @@ +#include + +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; + } +}; diff --git a/Easy/intersection-of-two-arrays-ii/solution.cpp b/Easy/intersection-of-two-arrays-ii/solution.cpp new file mode 100644 index 0000000..dfa8a66 --- /dev/null +++ b/Easy/intersection-of-two-arrays-ii/solution.cpp @@ -0,0 +1,43 @@ +class Solution { + public: + /* + std::vector intersect(std::vector &nums1, + std::vector &nums2) { + std::unordered_map hmap; + std::vector 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 intersect(std::vector &nums1, + std::vector &nums2) { + std::sort(nums1.begin(), nums1.end()); + std::sort(nums2.begin(), nums2.end()); + std::vector 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; + } +}; diff --git a/Easy/invert-binary-tree/solution.cpp b/Easy/invert-binary-tree/solution.cpp new file mode 100644 index 0000000..0f085d1 --- /dev/null +++ b/Easy/invert-binary-tree/solution.cpp @@ -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; + } +}; diff --git a/Easy/linked-list-cycle/solution.cpp b/Easy/linked-list-cycle/solution.cpp new file mode 100644 index 0000000..1ce4d84 --- /dev/null +++ b/Easy/linked-list-cycle/solution.cpp @@ -0,0 +1,50 @@ +#include +#include + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +class Solution { + public: + // bool hasCycle(ListNode *head) { + // std::unordered_map 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; + } +}; diff --git a/Easy/longest-common-prefix/Solution.java b/Easy/longest-common-prefix/Solution.java new file mode 100644 index 0000000..df439a0 --- /dev/null +++ b/Easy/longest-common-prefix/Solution.java @@ -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); + } +} diff --git a/Easy/maximum-depth-of-binary-tree/solution.cpp b/Easy/maximum-depth-of-binary-tree/solution.cpp new file mode 100644 index 0000000..4939b86 --- /dev/null +++ b/Easy/maximum-depth-of-binary-tree/solution.cpp @@ -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; + } +}; diff --git a/Easy/maximum-number-of-words-you-can-type/Solution.java b/Easy/maximum-number-of-words-you-can-type/Solution.java new file mode 100644 index 0000000..f84e316 --- /dev/null +++ b/Easy/maximum-number-of-words-you-can-type/Solution.java @@ -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; + } +} diff --git a/Easy/maximum-subarray/solution.cpp b/Easy/maximum-subarray/solution.cpp new file mode 100644 index 0000000..7532e7a --- /dev/null +++ b/Easy/maximum-subarray/solution.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + int maxSubArray(std::vector &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; + } +}; diff --git a/Easy/merge-sorted-array/solution.cpp b/Easy/merge-sorted-array/solution.cpp new file mode 100644 index 0000000..ed668be --- /dev/null +++ b/Easy/merge-sorted-array/solution.cpp @@ -0,0 +1,10 @@ +class Solution { + public: + void merge(std::vector &nums1, int m, std::vector &nums2, int n) { + for (int i = 0; i < n; ++i) { + nums1[m + i] = nums2[i]; + } + + std::sort(nums1.begin(), nums1.end()); + } +}; diff --git a/Easy/merge-two-sorted-lists/solution.cpp b/Easy/merge-two-sorted-lists/solution.cpp new file mode 100644 index 0000000..be88f66 --- /dev/null +++ b/Easy/merge-two-sorted-lists/solution.cpp @@ -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; + } + } +}; diff --git a/Easy/palindrome-number/solution.c b/Easy/palindrome-number/solution.c new file mode 100644 index 0000000..205a06c --- /dev/null +++ b/Easy/palindrome-number/solution.c @@ -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; +} diff --git a/Easy/palindrome-number/solution.cpp b/Easy/palindrome-number/solution.cpp new file mode 100644 index 0000000..a762581 --- /dev/null +++ b/Easy/palindrome-number/solution.cpp @@ -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; + } + } +}; diff --git a/Easy/pascals-triangle/solution.cpp b/Easy/pascals-triangle/solution.cpp new file mode 100644 index 0000000..c55d27b --- /dev/null +++ b/Easy/pascals-triangle/solution.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + std::vector getRow(int n) { + std::vector 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> generate(int numRows) { + std::vector> rslt; + + for (int i = 0; i < numRows; ++i) + rslt.push_back(getRow(i)); + + return rslt; + } +}; diff --git a/Easy/path-sum/solution.cpp b/Easy/path-sum/solution.cpp new file mode 100644 index 0000000..bca566e --- /dev/null +++ b/Easy/path-sum/solution.cpp @@ -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); + } + } +}; diff --git a/Easy/plus-one/solution.cpp b/Easy/plus-one/solution.cpp new file mode 100644 index 0000000..b2f18c8 --- /dev/null +++ b/Easy/plus-one/solution.cpp @@ -0,0 +1,18 @@ +#include + +class Solution { +public: + std::vector plusOne(std::vector &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; + } +}; diff --git a/Easy/ransom-note/solution.cpp b/Easy/ransom-note/solution.cpp new file mode 100644 index 0000000..b17d0a2 --- /dev/null +++ b/Easy/ransom-note/solution.cpp @@ -0,0 +1,21 @@ +#include +#include + +class Solution { + public: + bool canConstruct(std::string ransomNote, std::string magazine) { + std::unordered_map 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; + } +}; diff --git a/Easy/remove-duplicates-from-sorted-array/Solution.java b/Easy/remove-duplicates-from-sorted-array/Solution.java new file mode 100644 index 0000000..d27bcf0 --- /dev/null +++ b/Easy/remove-duplicates-from-sorted-array/Solution.java @@ -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; + } +} diff --git a/Easy/remove-duplicates-from-sorted-array/solution.cpp b/Easy/remove-duplicates-from-sorted-array/solution.cpp new file mode 100644 index 0000000..013dd1f --- /dev/null +++ b/Easy/remove-duplicates-from-sorted-array/solution.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +class Solution { + public: + int removeDuplicates(std::vector &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; + } +}; diff --git a/Easy/remove-duplicates-from-sorted-list/solution.cpp b/Easy/remove-duplicates-from-sorted-list/solution.cpp new file mode 100644 index 0000000..aca39ba --- /dev/null +++ b/Easy/remove-duplicates-from-sorted-list/solution.cpp @@ -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; + } +}; diff --git a/Easy/remove-linked-list-elements/solution.cpp b/Easy/remove-linked-list-elements/solution.cpp new file mode 100644 index 0000000..da56ddf --- /dev/null +++ b/Easy/remove-linked-list-elements/solution.cpp @@ -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; + } + } +}; diff --git a/Easy/repeated-substring-pattern/solution.cpp b/Easy/repeated-substring-pattern/solution.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Easy/reshape-the-matrix/solution.cpp b/Easy/reshape-the-matrix/solution.cpp new file mode 100644 index 0000000..cbd1a9c --- /dev/null +++ b/Easy/reshape-the-matrix/solution.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + std::vector> + matrixReshape(std::vector> &mat, int r, int c) { + int m = mat.size(), n = mat[0].size(); + if (m * n == r * c) { + std::vector> rslt(r, std::vector(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; + } + } +}; diff --git a/Easy/reverse-linked-list/solution.cpp b/Easy/reverse-linked-list/solution.cpp new file mode 100644 index 0000000..b3addc7 --- /dev/null +++ b/Easy/reverse-linked-list/solution.cpp @@ -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; + } +}; diff --git a/Easy/roman-to-integer/Solution.java b/Easy/roman-to-integer/Solution.java new file mode 100644 index 0000000..a9727ff --- /dev/null +++ b/Easy/roman-to-integer/Solution.java @@ -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; + } +} diff --git a/Easy/search-in-a-binary-search-tree/solution.cpp b/Easy/search-in-a-binary-search-tree/solution.cpp new file mode 100644 index 0000000..33f5b82 --- /dev/null +++ b/Easy/search-in-a-binary-search-tree/solution.cpp @@ -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; + } + } +}; diff --git a/Easy/search-insert-position/solution.cpp b/Easy/search-insert-position/solution.cpp new file mode 100644 index 0000000..52322f0 --- /dev/null +++ b/Easy/search-insert-position/solution.cpp @@ -0,0 +1,24 @@ +#include + +class Solution { +public: + int searchInsert(std::vector &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; + } +}; diff --git a/Easy/symmetric-tree/solution.cpp b/Easy/symmetric-tree/solution.cpp new file mode 100644 index 0000000..6502b1a --- /dev/null +++ b/Easy/symmetric-tree/solution.cpp @@ -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); + } +}; diff --git a/Easy/two-sum/Solution.java b/Easy/two-sum/Solution.java new file mode 100644 index 0000000..25cb814 --- /dev/null +++ b/Easy/two-sum/Solution.java @@ -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 }; + } +} diff --git a/Easy/two-sum/solution.cpp b/Easy/two-sum/solution.cpp new file mode 100644 index 0000000..6b563ee --- /dev/null +++ b/Easy/two-sum/solution.cpp @@ -0,0 +1,18 @@ +class Solution { + public: + std::vector twoSum(std::vector &nums, int target) { + std::unordered_map 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}; + } +}; diff --git a/Easy/valid-anagram/solution.cpp b/Easy/valid-anagram/solution.cpp new file mode 100644 index 0000000..858337b --- /dev/null +++ b/Easy/valid-anagram/solution.cpp @@ -0,0 +1,25 @@ +#include +#include + +class Solution { + public: + bool isAnagram(std::string s, std::string t) { + if (s.size() == t.size()) { + std::unordered_map 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; + } +}; diff --git a/Easy/valid-parentheses/solution.cpp b/Easy/valid-parentheses/solution.cpp new file mode 100644 index 0000000..bee70fa --- /dev/null +++ b/Easy/valid-parentheses/solution.cpp @@ -0,0 +1,24 @@ +#include +#include + +class Solution { + public: + bool isValid(std::string line) { + std::stack 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(); + } +}; diff --git a/Hard/longest-valid-parentheses/Solution.java b/Hard/longest-valid-parentheses/Solution.java new file mode 100644 index 0000000..538073f --- /dev/null +++ b/Hard/longest-valid-parentheses/Solution.java @@ -0,0 +1,21 @@ +class Solution { + public int longestValidParentheses(String str) { + Stack 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; + } +} diff --git a/Hard/longest-valid-parentheses/solution.cpp b/Hard/longest-valid-parentheses/solution.cpp new file mode 100644 index 0000000..64969f9 --- /dev/null +++ b/Hard/longest-valid-parentheses/solution.cpp @@ -0,0 +1,26 @@ +#include +#include + +class Solution { + public: + int longestValidParentheses(std::string line) { + std::stack 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; + } +}; diff --git a/Hard/sudoku-solver/Solution.java b/Hard/sudoku-solver/Solution.java new file mode 100644 index 0000000..91482bf --- /dev/null +++ b/Hard/sudoku-solver/Solution.java @@ -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(); + } +} diff --git a/Hard/sudoku-solver/solution.cpp b/Hard/sudoku-solver/solution.cpp new file mode 100644 index 0000000..f5ee4de --- /dev/null +++ b/Hard/sudoku-solver/solution.cpp @@ -0,0 +1,67 @@ +#include +#include + +class Solution { + public: + std::array nextEmpty(std::vector> &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> &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> &board) { + std::array 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> &board) { + boardFiller(board); + } +}; diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..720e153 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Medium/add-two-numbers/Solution.java b/Medium/add-two-numbers/Solution.java new file mode 100644 index 0000000..cac9d9f --- /dev/null +++ b/Medium/add-two-numbers/Solution.java @@ -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; + } +} diff --git a/Medium/binary-tree-level-order-traversal/solution.cpp b/Medium/binary-tree-level-order-traversal/solution.cpp new file mode 100644 index 0000000..cad3c27 --- /dev/null +++ b/Medium/binary-tree-level-order-traversal/solution.cpp @@ -0,0 +1,35 @@ +#include + +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> &levels, TreeNode *root, + int index) { + if (root == nullptr) { + return; + } else if (levels.size() == index) { + levels.push_back(std::vector{}); + } + + levels[index].push_back(root->val); + levelOrder(levels, root->left, index + 1); + levelOrder(levels, root->right, index + 1); + } + + public: + std::vector> levelOrder(TreeNode *root) { + std::vector> levels; + levelOrder(levels, root, 0); + + return levels; + } +}; diff --git a/Medium/container-with-most-water/solution.cpp b/Medium/container-with-most-water/solution.cpp new file mode 100644 index 0000000..942d9fc --- /dev/null +++ b/Medium/container-with-most-water/solution.cpp @@ -0,0 +1,26 @@ +#include +#include + +class Solution { + public: + int maxArea(std::vector &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; + } +}; diff --git a/Medium/equal-row-and-column-pairs/solution.cpp b/Medium/equal-row-and-column-pairs/solution.cpp new file mode 100644 index 0000000..6abf2d6 --- /dev/null +++ b/Medium/equal-row-and-column-pairs/solution.cpp @@ -0,0 +1,25 @@ +#include + +class Solution { +public: + int equalPairs(std::vector> &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; + } +}; diff --git a/Medium/insert-into-a-binary-search-tree/solution.cpp b/Medium/insert-into-a-binary-search-tree/solution.cpp new file mode 100644 index 0000000..c6612e5 --- /dev/null +++ b/Medium/insert-into-a-binary-search-tree/solution.cpp @@ -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); + } +}; diff --git a/Medium/letter-combinations-of-a-phone-number/solution.cpp b/Medium/letter-combinations-of-a-phone-number/solution.cpp new file mode 100644 index 0000000..d0c7afc --- /dev/null +++ b/Medium/letter-combinations-of-a-phone-number/solution.cpp @@ -0,0 +1,68 @@ +#include +#include + +class Solution { + public: + std::vector letterCombinations(std::string digits) { + std::string letters[] = {"abc", "def", "ghi", "jkl", + "mno", "pqrs", "tuv", "wxyz"}; + + std::vector 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; + } +}; diff --git a/Medium/longest-palindromic-substring/Solution.java b/Medium/longest-palindromic-substring/Solution.java new file mode 100644 index 0000000..e7c2327 --- /dev/null +++ b/Medium/longest-palindromic-substring/Solution.java @@ -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); + } + } +} diff --git a/Medium/longest-palindromic-substring/solution.cpp b/Medium/longest-palindromic-substring/solution.cpp new file mode 100644 index 0000000..1aba9f5 --- /dev/null +++ b/Medium/longest-palindromic-substring/solution.cpp @@ -0,0 +1,33 @@ +#include + +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); + } + } +}; diff --git a/Medium/reverse-integer/Solution.java b/Medium/reverse-integer/Solution.java new file mode 100644 index 0000000..15a8cf6 --- /dev/null +++ b/Medium/reverse-integer/Solution.java @@ -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; + } + } +} diff --git a/Medium/search-a-2d-matrix/solution.cpp b/Medium/search-a-2d-matrix/solution.cpp new file mode 100644 index 0000000..42f4260 --- /dev/null +++ b/Medium/search-a-2d-matrix/solution.cpp @@ -0,0 +1,53 @@ +#include + +class Solution { + private: + bool binarySearch(std::vector &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 &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> &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; + } +}; diff --git a/Medium/single-element-in-a-sorted-array/Solution.java b/Medium/single-element-in-a-sorted-array/Solution.java new file mode 100644 index 0000000..5743852 --- /dev/null +++ b/Medium/single-element-in-a-sorted-array/Solution.java @@ -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; + } +} diff --git a/Medium/sort-list/solution.c b/Medium/sort-list/solution.c new file mode 100644 index 0000000..7a923d9 --- /dev/null +++ b/Medium/sort-list/solution.c @@ -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)); +} diff --git a/Medium/string-to-integer-atoi/solution.cpp b/Medium/string-to-integer-atoi/solution.cpp new file mode 100644 index 0000000..ee9fe03 --- /dev/null +++ b/Medium/string-to-integer-atoi/solution.cpp @@ -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; + } +}; diff --git a/Medium/two-sum-ii-input-array-is-sorted/solution.cpp b/Medium/two-sum-ii-input-array-is-sorted/solution.cpp new file mode 100644 index 0000000..c7263e7 --- /dev/null +++ b/Medium/two-sum-ii-input-array-is-sorted/solution.cpp @@ -0,0 +1,33 @@ +class Solution { + public: + std::vector twoSum(std::vector &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 &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; + } +}; diff --git a/Medium/valid-sudoku/solution.cpp b/Medium/valid-sudoku/solution.cpp new file mode 100644 index 0000000..b86a282 --- /dev/null +++ b/Medium/valid-sudoku/solution.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +class Solution { + public: + /* + bool isValidSudoku(std::vector> &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> &board) { + std::unordered_map 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; + } +}; diff --git a/Medium/validate-binary-search-tree/solution.cpp b/Medium/validate-binary-search-tree/solution.cpp new file mode 100644 index 0000000..9d25dd8 --- /dev/null +++ b/Medium/validate-binary-search-tree/solution.cpp @@ -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); + } +}; diff --git a/Medium/zigzag-conversion/Solution.java b/Medium/zigzag-conversion/Solution.java new file mode 100644 index 0000000..8f9e713 --- /dev/null +++ b/Medium/zigzag-conversion/Solution.java @@ -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; + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b8d91f --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Preview +![](blob/preview.jpg) + +# Description +All the LeetCode problems I have solved. diff --git a/blob/preview.jpg b/blob/preview.jpg new file mode 100644 index 0000000..34b30da Binary files /dev/null and b/blob/preview.jpg differ