diff --git a/Easy/climbing-stairs/solution.cpp b/Easy/climbing-stairs/solution.cpp new file mode 100644 index 0000000..161659e --- /dev/null +++ b/Easy/climbing-stairs/solution.cpp @@ -0,0 +1,17 @@ +#include + +class Solution { +public: + int climbStairs(int n) { + if (n == 0) { + return 0; + } + + std::vector shiftTwoFibo{1, 2}; + for (int i = 2; i < n; ++i) { + shiftTwoFibo.push_back(shiftTwoFibo[i - 1] + shiftTwoFibo[i - 2]); + } + + return shiftTwoFibo[n - 1]; + } +}; diff --git a/Easy/contains-duplicate-ii/solution.cpp b/Easy/contains-duplicate-ii/solution.cpp new file mode 100644 index 0000000..fbdecd6 --- /dev/null +++ b/Easy/contains-duplicate-ii/solution.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +class Solution { +public: + bool containsDuplicate(std::vector &nums, int k) { + std::unordered_map> unset; + + int size = nums.size(); + for (int i = 0; i < size; ++i) { + if (unset.find(nums[i]) != unset.end()) { + std::vector indices = unset[nums[i]]; + + for (int j = 0; j < indices.size(); ++j) { + if (std::abs(indices[j] - i) <= k) { + return true; + } + } + } + + unset[nums[i]].push_back(i); + } + + return false; + } +}; diff --git a/Easy/contains-duplicate/solution.cpp b/Easy/contains-duplicate/solution.cpp index c8d6db8..84b92c7 100644 --- a/Easy/contains-duplicate/solution.cpp +++ b/Easy/contains-duplicate/solution.cpp @@ -1,5 +1,5 @@ class Solution { - public: +public: bool containsDuplicate(std::vector &nums) { std::unordered_set unset; diff --git a/Easy/minimum-depth-of-binary-tree/solution.cpp b/Easy/minimum-depth-of-binary-tree/solution.cpp new file mode 100644 index 0000000..b106083 --- /dev/null +++ b/Easy/minimum-depth-of-binary-tree/solution.cpp @@ -0,0 +1,38 @@ +#include +#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 { +public: + void traverse(TreeNode *root, int curlvl, int &minCount) { + if (root == nullptr) { + return; + } + + if (root->left == nullptr && root->right == nullptr && curlvl < minCount) { + minCount = curlvl; + } + + traverse(root->left, curlvl + 1, minCount); + traverse(root->right, curlvl + 1, minCount); + } + + int minDepth(TreeNode *root) { + if (root == nullptr) { + return 0; + } + + int minCount = INT32_MAX; + traverse(root, 1, minCount); + + return minCount; + } +}; diff --git a/Easy/reverse-bits/solution.cpp b/Easy/reverse-bits/solution.cpp new file mode 100644 index 0000000..3485b4e --- /dev/null +++ b/Easy/reverse-bits/solution.cpp @@ -0,0 +1,17 @@ +#include +#include + +class Solution { +public: + uint32_t reverseBits(uint32_t n) { + uint32_t r = 0; + for (int i = 31; i >= 0; --i) { + if (n / (uint32_t)std::pow(2, i) >= 1) { + n -= (uint32_t)std::pow(2, i); + r += (uint32_t)std::pow(2, 31 - i); + } + } + + return r; + } +}; diff --git a/Easy/same-tree/solution.cpp b/Easy/same-tree/solution.cpp new file mode 100644 index 0000000..8207fb7 --- /dev/null +++ b/Easy/same-tree/solution.cpp @@ -0,0 +1,21 @@ +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 isSameTree(TreeNode *p, TreeNode *q) { + if (p != nullptr && q != nullptr) { + return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); + } else if (p == nullptr ^ q == nullptr) { + return false; + } else { + return true; + } + } +}; diff --git a/Easy/shuffle-string/solution.cpp b/Easy/shuffle-string/solution.cpp new file mode 100644 index 0000000..e4bae76 --- /dev/null +++ b/Easy/shuffle-string/solution.cpp @@ -0,0 +1,23 @@ +#include +#include + +class Solution { +public: + std::string restoreString(std::string s, std::vector &indices) { + for (int i = 1; i < indices.size(); i++) { + int itmp = indices[i]; + char ctmp = s[i]; + int j = i - 1; + + while (j >= 0 && indices[j] > itmp) { + indices[j + 1] = indices[j]; + s[j + 1] = s[j]; + j = j - 1; + } + indices[j + 1] = itmp; + s[j + 1] = ctmp; + } + + return s; + } +}; diff --git a/Easy/transpose-matrix/solution.cpp b/Easy/transpose-matrix/solution.cpp new file mode 100644 index 0000000..9a3d501 --- /dev/null +++ b/Easy/transpose-matrix/solution.cpp @@ -0,0 +1,16 @@ +#include + +class Solution { +public: + std::vector> transpose(std::vector> &matrix) { + std::vector> trans(matrix[0].size(), std::vector(matrix.size())); + + for (int i = 0; i < matrix.size(); ++i) { + for (int j = 0; j < matrix[0].size(); ++j) { + trans[j][i] = matrix[i][j]; + } + } + + return trans; + } +}; diff --git a/Easy/valid-palindrome/solution.java b/Easy/valid-palindrome/solution.java new file mode 100644 index 0000000..ca1bf04 --- /dev/null +++ b/Easy/valid-palindrome/solution.java @@ -0,0 +1,20 @@ +class Solution { + public boolean isPalindrome(String s) { + String t = ""; + for (int i = 0; i < s.length(); ++i) { + int a = (int)s.charAt(i); + if (a >= (int)'A' && a <= (int)'Z') { + t += (char)(a - (int)'A' + (int)'a'); + } else if (a >= (int)'a' && a <= (int)'z') { + t += (char)a; + } else if (a >= (int)'0' && a <= (int)'9') { + t += (char)a; + } + } + + StringBuilder ob = new StringBuilder(t); + String r = ob.reverse().toString(); + + return t.compareTo(r) == 0; + } +}