diff --git a/Easy/best-time-to-buy-and-sell-stock/solution.cpp b/Easy/best-time-to-buy-and-sell-stock/solution.cpp index f643e3b..c2949ab 100644 --- a/Easy/best-time-to-buy-and-sell-stock/solution.cpp +++ b/Easy/best-time-to-buy-and-sell-stock/solution.cpp @@ -1,5 +1,7 @@ +#include + class Solution { - public: +public: int maxProfit(std::vector &prices) { int prof = 0; for (int i = prices.size() - 1, max = 0; i >= 0; --i) { diff --git a/Easy/binary-tree-postorder-traversal/solution.cpp b/Easy/binary-tree-postorder-traversal/solution.cpp index 3804ac9..90f0217 100644 --- a/Easy/binary-tree-postorder-traversal/solution.cpp +++ b/Easy/binary-tree-postorder-traversal/solution.cpp @@ -11,7 +11,7 @@ struct TreeNode { }; class Solution { - private: +private: void traverse(std::vector &list, TreeNode *root) { if (root == nullptr) { return; @@ -22,7 +22,7 @@ class Solution { } } - public: +public: std::vector postorderTraversal(TreeNode *root) { std::vector list; traverse(list, root); diff --git a/Easy/binary-tree-preorder-traversal/solution.cpp b/Easy/binary-tree-preorder-traversal/solution.cpp index eaa4bf2..e33886d 100644 --- a/Easy/binary-tree-preorder-traversal/solution.cpp +++ b/Easy/binary-tree-preorder-traversal/solution.cpp @@ -11,7 +11,7 @@ struct TreeNode { }; class Solution { - private: +private: void traverse(std::vector &list, TreeNode *root) { if (root == nullptr) { return; @@ -22,7 +22,7 @@ class Solution { } } - public: +public: std::vector preorderTraversal(TreeNode *root) { std::vector list; traverse(list, root); diff --git a/Easy/contains-duplicate/solution.cpp b/Easy/contains-duplicate/solution.cpp index 84b92c7..c05197b 100644 --- a/Easy/contains-duplicate/solution.cpp +++ b/Easy/contains-duplicate/solution.cpp @@ -1,3 +1,6 @@ +#include +#include + class Solution { public: bool containsDuplicate(std::vector &nums) { diff --git a/Easy/excel-sheet-column-number/Solution.java b/Easy/excel-sheet-column-number/Solution.java index 04b44a6..5446785 100644 --- a/Easy/excel-sheet-column-number/Solution.java +++ b/Easy/excel-sheet-column-number/Solution.java @@ -2,7 +2,7 @@ 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); + 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 index e7d789b..7443903 100644 --- a/Easy/excel-sheet-column-title/Solution.java +++ b/Easy/excel-sheet-column-title/Solution.java @@ -2,9 +2,8 @@ class Solution { public String convertToTitle(int num) { StringBuilder ttl = new StringBuilder(); - while (num > 0) { - --num; - char tba = (char) (num % 26 + 65); + while (num-- > 0) { + char tba = (char)(num % 26 + 65); ttl.insert(0, tba); num /= 26; } diff --git a/Easy/first-unique-character-in-a-string/solution.cpp b/Easy/first-unique-character-in-a-string/solution.cpp index 27aa156..6bd7406 100644 --- a/Easy/first-unique-character-in-a-string/solution.cpp +++ b/Easy/first-unique-character-in-a-string/solution.cpp @@ -2,7 +2,7 @@ #include class Solution { - public: +public: /* int firstUniqChar(std::string line) { std::unordered_map hmap; diff --git a/Easy/implement-queue-using-stacks/solution.cpp b/Easy/implement-queue-using-stacks/solution.cpp index df5e166..fb167cb 100644 --- a/Easy/implement-queue-using-stacks/solution.cpp +++ b/Easy/implement-queue-using-stacks/solution.cpp @@ -1,11 +1,11 @@ #include class MyQueue { - private: +private: std::stack shelfA; std::stack shelfB; - public: +public: MyQueue() {} void push(int data) { shelfA.push(data); } diff --git a/Easy/intersection-of-two-arrays-ii/solution.cpp b/Easy/intersection-of-two-arrays-ii/solution.cpp index dfa8a66..7b8ec89 100644 --- a/Easy/intersection-of-two-arrays-ii/solution.cpp +++ b/Easy/intersection-of-two-arrays-ii/solution.cpp @@ -1,5 +1,8 @@ +#include +#include + class Solution { - public: +public: /* std::vector intersect(std::vector &nums1, std::vector &nums2) { diff --git a/Easy/invert-binary-tree/solution.cpp b/Easy/invert-binary-tree/solution.cpp index 0f085d1..cc006b3 100644 --- a/Easy/invert-binary-tree/solution.cpp +++ b/Easy/invert-binary-tree/solution.cpp @@ -9,7 +9,7 @@ struct TreeNode { }; class Solution { - public: +public: TreeNode *invertTree(TreeNode *root) { if (root != nullptr) { invertTree(root->left); diff --git a/Easy/linked-list-cycle/solution.cpp b/Easy/linked-list-cycle/solution.cpp index 1ce4d84..ea6ecca 100644 --- a/Easy/linked-list-cycle/solution.cpp +++ b/Easy/linked-list-cycle/solution.cpp @@ -8,7 +8,7 @@ struct ListNode { }; class Solution { - public: +public: // bool hasCycle(ListNode *head) { // std::unordered_map hmap; // ListNode *curr = head; diff --git a/Easy/maximum-depth-of-binary-tree/solution.cpp b/Easy/maximum-depth-of-binary-tree/solution.cpp index 4939b86..ecaafad 100644 --- a/Easy/maximum-depth-of-binary-tree/solution.cpp +++ b/Easy/maximum-depth-of-binary-tree/solution.cpp @@ -9,11 +9,11 @@ struct TreeNode { }; class Solution { - private: - void maxDepth(TreeNode *root, int index, int *max) { +private: + void maxDepth(TreeNode *root, int index, int &max) { if (root == nullptr) { - if (index > *max) { - *max = index; + if (index > max) { + max = index; } return; @@ -23,10 +23,10 @@ class Solution { } } - public: +public: int maxDepth(TreeNode *root) { int max = 0; - maxDepth(root, 0, &max); + 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 index f84e316..923c709 100644 --- a/Easy/maximum-number-of-words-you-can-type/Solution.java +++ b/Easy/maximum-number-of-words-you-can-type/Solution.java @@ -5,14 +5,16 @@ class Solution { int ret = 0; for (boolean flag = true; words.hasMoreTokens(); flag = true) { String word = words.nextToken(); - for (int i = 0; i < chars.length(); i++) + for (int i = 0; i < chars.length(); i++) { if (word.indexOf(chars.charAt(i)) > -1) { flag = false; break; } + } - if (flag) + if (flag) { ++ret; + } } return ret; diff --git a/Easy/maximum-subarray/solution.cpp b/Easy/maximum-subarray/solution.cpp index 7532e7a..9278e68 100644 --- a/Easy/maximum-subarray/solution.cpp +++ b/Easy/maximum-subarray/solution.cpp @@ -1,5 +1,8 @@ +#include +#include + class Solution { - public: +public: int maxSubArray(std::vector &nums) { int max = INT32_MIN; int tmax = 0; diff --git a/Easy/merge-sorted-array/solution.cpp b/Easy/merge-sorted-array/solution.cpp index ed668be..273f480 100644 --- a/Easy/merge-sorted-array/solution.cpp +++ b/Easy/merge-sorted-array/solution.cpp @@ -1,5 +1,8 @@ +#include +#include + class Solution { - public: +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]; diff --git a/Easy/palindrome-number/solution.c b/Easy/palindrome-number/solution.c index 205a06c..80cb50b 100644 --- a/Easy/palindrome-number/solution.c +++ b/Easy/palindrome-number/solution.c @@ -1,3 +1,5 @@ +#include + bool isPalindrome(long int a) { if (a < 0) return false; diff --git a/Easy/pascals-triangle/solution.cpp b/Easy/pascals-triangle/solution.cpp index c55d27b..9adfb15 100644 --- a/Easy/pascals-triangle/solution.cpp +++ b/Easy/pascals-triangle/solution.cpp @@ -1,5 +1,7 @@ +#include + class Solution { - public: +public: std::vector getRow(int n) { std::vector prow = {1}; diff --git a/Easy/ransom-note/solution.cpp b/Easy/ransom-note/solution.cpp index b17d0a2..0988327 100644 --- a/Easy/ransom-note/solution.cpp +++ b/Easy/ransom-note/solution.cpp @@ -2,7 +2,7 @@ #include class Solution { - public: +public: bool canConstruct(std::string ransomNote, std::string magazine) { std::unordered_map hmap[2]; diff --git a/Easy/remove-duplicates-from-sorted-array/solution.cpp b/Easy/remove-duplicates-from-sorted-array/solution.cpp index 013dd1f..962e534 100644 --- a/Easy/remove-duplicates-from-sorted-array/solution.cpp +++ b/Easy/remove-duplicates-from-sorted-array/solution.cpp @@ -3,7 +3,7 @@ #include class Solution { - public: +public: int removeDuplicates(std::vector &nums) { int k = 0, l = INT32_MIN; for (auto iter = nums.begin(); iter != nums.end(); iter.operator++()) { diff --git a/Easy/remove-duplicates-from-sorted-list/solution.cpp b/Easy/remove-duplicates-from-sorted-list/solution.cpp index aca39ba..b84a319 100644 --- a/Easy/remove-duplicates-from-sorted-list/solution.cpp +++ b/Easy/remove-duplicates-from-sorted-list/solution.cpp @@ -7,7 +7,7 @@ struct ListNode { }; class Solution { - private: +private: ListNode *removeElements(ListNode *head, int val) { if (head == nullptr) { return nullptr; @@ -19,7 +19,7 @@ class Solution { } } - public: +public: ListNode *deleteDuplicates(ListNode *head) { for (ListNode *curr = head; curr != nullptr && curr->next != nullptr; curr = curr->next) { diff --git a/Easy/remove-linked-list-elements/solution.cpp b/Easy/remove-linked-list-elements/solution.cpp index da56ddf..0f6ca27 100644 --- a/Easy/remove-linked-list-elements/solution.cpp +++ b/Easy/remove-linked-list-elements/solution.cpp @@ -7,7 +7,7 @@ struct ListNode { }; class Solution { - public: +public: ListNode *removeElements(ListNode *head, int val) { if (head == nullptr) { return nullptr; diff --git a/Easy/repeated-substring-pattern/solution.cpp b/Easy/repeated-substring-pattern/solution.cpp index e69de29..c42b2b0 100644 --- a/Easy/repeated-substring-pattern/solution.cpp +++ b/Easy/repeated-substring-pattern/solution.cpp @@ -0,0 +1,27 @@ +#include + +class Solution { +public: + bool repeatedSubstringPattern(std::string s) { + for (int i = 1; i <= s.size() / 2; ++i) { + std::string sl(s.substr(0, i)); + if (s.size() % i == 0) { + bool found = false; + for (int j = 1; j < s.size() / i; ++j) { + if (s.find(sl, sl.size() * j) == sl.size() * j) { + found = true; + } else { + found = false; + break; + } + } + + if (found) { + return true; + } + } + } + + return false; + } +}; diff --git a/Easy/reshape-the-matrix/solution.cpp b/Easy/reshape-the-matrix/solution.cpp index cbd1a9c..47c1c7e 100644 --- a/Easy/reshape-the-matrix/solution.cpp +++ b/Easy/reshape-the-matrix/solution.cpp @@ -1,5 +1,7 @@ +#include + class Solution { - public: +public: std::vector> matrixReshape(std::vector> &mat, int r, int c) { int m = mat.size(), n = mat[0].size(); diff --git a/Easy/reverse-linked-list/solution.cpp b/Easy/reverse-linked-list/solution.cpp index b3addc7..b64ce4f 100644 --- a/Easy/reverse-linked-list/solution.cpp +++ b/Easy/reverse-linked-list/solution.cpp @@ -7,7 +7,7 @@ struct ListNode { }; class Solution { - public: +public: ListNode *reverseList(ListNode *head) { ListNode *prev = nullptr, *next = nullptr, *curr = head; while (curr != nullptr) { diff --git a/Easy/search-in-a-binary-search-tree/solution.cpp b/Easy/search-in-a-binary-search-tree/solution.cpp index 33f5b82..629b345 100644 --- a/Easy/search-in-a-binary-search-tree/solution.cpp +++ b/Easy/search-in-a-binary-search-tree/solution.cpp @@ -9,7 +9,7 @@ struct TreeNode { }; class Solution { - public: +public: TreeNode *searchBST(TreeNode *root, int val) { if (root == nullptr) { return nullptr; diff --git a/Easy/two-sum/solution.cpp b/Easy/two-sum/solution.cpp index 6b563ee..5e2833a 100644 --- a/Easy/two-sum/solution.cpp +++ b/Easy/two-sum/solution.cpp @@ -1,5 +1,8 @@ +#include +#include + class Solution { - public: +public: std::vector twoSum(std::vector &nums, int target) { std::unordered_map hmap; diff --git a/Easy/valid-anagram/solution.cpp b/Easy/valid-anagram/solution.cpp index 858337b..881af82 100644 --- a/Easy/valid-anagram/solution.cpp +++ b/Easy/valid-anagram/solution.cpp @@ -2,7 +2,7 @@ #include class Solution { - public: +public: bool isAnagram(std::string s, std::string t) { if (s.size() == t.size()) { std::unordered_map hmap[2]; diff --git a/Easy/valid-parentheses/solution.cpp b/Easy/valid-parentheses/solution.cpp index bee70fa..8159158 100644 --- a/Easy/valid-parentheses/solution.cpp +++ b/Easy/valid-parentheses/solution.cpp @@ -2,7 +2,7 @@ #include class Solution { - public: +public: bool isValid(std::string line) { std::stack shelf; for (auto iter = line.begin(); iter != line.end(); iter.operator++()) { diff --git a/Hard/longest-valid-parentheses/Solution.java b/Hard/longest-valid-parentheses/Solution.java index 538073f..ad5b1c8 100644 --- a/Hard/longest-valid-parentheses/Solution.java +++ b/Hard/longest-valid-parentheses/Solution.java @@ -2,7 +2,7 @@ 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) == '(') { diff --git a/Hard/longest-valid-parentheses/solution.cpp b/Hard/longest-valid-parentheses/solution.cpp index 64969f9..53bea27 100644 --- a/Hard/longest-valid-parentheses/solution.cpp +++ b/Hard/longest-valid-parentheses/solution.cpp @@ -2,7 +2,7 @@ #include class Solution { - public: +public: int longestValidParentheses(std::string line) { std::stack shelf; shelf.push(-1); diff --git a/Hard/sudoku-solver/solution.cpp b/Hard/sudoku-solver/solution.cpp index f5ee4de..0c5b9d8 100644 --- a/Hard/sudoku-solver/solution.cpp +++ b/Hard/sudoku-solver/solution.cpp @@ -2,7 +2,7 @@ #include class Solution { - public: +public: std::array nextEmpty(std::vector> &board) { for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { diff --git a/Medium/container-with-most-water/solution.cpp b/Medium/container-with-most-water/solution.cpp index 942d9fc..b57bf58 100644 --- a/Medium/container-with-most-water/solution.cpp +++ b/Medium/container-with-most-water/solution.cpp @@ -2,7 +2,7 @@ #include class Solution { - public: +public: int maxArea(std::vector &height) { int left = 0; int right = height.size() - 1; diff --git a/Medium/insert-into-a-binary-search-tree/solution.cpp b/Medium/insert-into-a-binary-search-tree/solution.cpp index c6612e5..cea89cc 100644 --- a/Medium/insert-into-a-binary-search-tree/solution.cpp +++ b/Medium/insert-into-a-binary-search-tree/solution.cpp @@ -9,7 +9,7 @@ struct TreeNode { }; class Solution { - public: +public: TreeNode *insertIntoBST(TreeNode *root, int val) { TreeNode *node = root; while (node != nullptr) { diff --git a/Medium/letter-combinations-of-a-phone-number/solution.cpp b/Medium/letter-combinations-of-a-phone-number/solution.cpp index d0c7afc..79aa617 100644 --- a/Medium/letter-combinations-of-a-phone-number/solution.cpp +++ b/Medium/letter-combinations-of-a-phone-number/solution.cpp @@ -2,9 +2,9 @@ #include class Solution { - public: +public: std::vector letterCombinations(std::string digits) { - std::string letters[] = {"abc", "def", "ghi", "jkl", + std::string letters[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; std::vector finvec; diff --git a/Medium/longest-palindromic-substring/solution.cpp b/Medium/longest-palindromic-substring/solution.cpp index 1aba9f5..c4f7842 100644 --- a/Medium/longest-palindromic-substring/solution.cpp +++ b/Medium/longest-palindromic-substring/solution.cpp @@ -1,7 +1,7 @@ #include class Solution { - public: +public: std::string longestPalindrome(std::string line) { if (line.size() < 2) { return line; diff --git a/Medium/search-a-2d-matrix/solution.cpp b/Medium/search-a-2d-matrix/solution.cpp index 42f4260..76f2b69 100644 --- a/Medium/search-a-2d-matrix/solution.cpp +++ b/Medium/search-a-2d-matrix/solution.cpp @@ -1,7 +1,7 @@ #include class Solution { - private: +private: bool binarySearch(std::vector &nums, int start, int end, int target) { while (end >= start) { int middle = start + (end - start) / 2; @@ -36,7 +36,7 @@ class Solution { } */ - public: +public: bool searchMatrix(std::vector> &matrix, int target) { int row = matrix.size(); int col = matrix[0].size(); diff --git a/Medium/sort-list/solution.c b/Medium/sort-list/solution.c index 7a923d9..fff1273 100644 --- a/Medium/sort-list/solution.c +++ b/Medium/sort-list/solution.c @@ -1,3 +1,10 @@ +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + struct ListNode *merge(struct ListNode *left, struct ListNode *right) { if (!left) { return right; diff --git a/Medium/string-to-integer-atoi/solution.cpp b/Medium/string-to-integer-atoi/solution.cpp index ee9fe03..a89a301 100644 --- a/Medium/string-to-integer-atoi/solution.cpp +++ b/Medium/string-to-integer-atoi/solution.cpp @@ -1,5 +1,7 @@ +#include + class Solution { - public: +public: int myAtoi(std::string s) { unsigned long value = 0; diff --git a/Medium/two-sum-ii-input-array-is-sorted/solution.cpp b/Medium/two-sum-ii-input-array-is-sorted/solution.cpp index c7263e7..6f00c5b 100644 --- a/Medium/two-sum-ii-input-array-is-sorted/solution.cpp +++ b/Medium/two-sum-ii-input-array-is-sorted/solution.cpp @@ -1,5 +1,7 @@ +#include + class Solution { - public: +public: std::vector twoSum(std::vector &numbers, int target) { int j = -1; for (int i = 0; i < numbers.size(); ++i) { @@ -8,14 +10,15 @@ class Solution { 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}; + if (i < j) { + return {i + 1, j + 1}; + } else { + return {j + 1, i + 1}; + } } } - return std::vector{-1, -1}; + return {-1, -1}; } int binarySearch(std::vector &numbers, int start, int end, int key) {