repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
0064-Minimum-Path-Sum/cpp_0064/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/13. // #ifndef CPP_0064__SOLUTION1_H_ #define CPP_0064__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i][j] = cur_num + min(dp[i-1][j] + dp[i][j-1]) */ class Solution { public: int minPathSum(vector<vector<int>> &grid) { if (grid.empty()) return 0; int m = grid.size(), n = grid[0].size(); vector<vector<int>> dp(m, vector<int>(n)); dp[0][0] = grid[0][0]; for (int i = 1; i < m; ++i) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } for (int i = 1; i < n; ++i) { dp[0][i] = dp[0][i - 1] + grid[0][i]; } for (int i = 1; i < m; ++i) { for (int j = 1; j < n; ++j) { dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1]; } }; #endif //CPP_0064__SOLUTION1_H_
ooooo-youwillsee/leetcode
1817-Finding the Users Active Minutes/cpp_1817/Solution1.h
<filename>1817-Finding the Users Active Minutes/cpp_1817/Solution1.h /** * @author ooooo * @date 2021/4/11 15:54 */ #ifndef CPP_1817__SOLUTION1_H_ #define CPP_1817__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <numeric> #include <queue> using namespace std; class Solution { public: vector<int> findingUsersActiveMinutes(vector<vector<int>> &logs, int k) { unordered_map<int, unordered_set<int>> m; for (auto &log :logs) { m[log[0]].insert(log[1]); } unordered_map<int, int> x; for (auto &e : m) { x[e.second.size()]++; } vector<int> ans(k, 0); for (int i = 0; i < k; ++i) { ans[i] = x[i + 1]; } return ans; } }; #endif //CPP_1817__SOLUTION1_H_
ooooo-youwillsee/leetcode
1609-Even Odd Tree/cpp_1609/Solution1.h
<filename>1609-Even Odd Tree/cpp_1609/Solution1.h /** * @author ooooo * @date 2020/10/11 13:41 */ #ifndef CPP_1609__SOLUTION1_H_ #define CPP_1609__SOLUTION1_H_ #include "TreeNode.h" bool isEvenOddTree(TreeNode *root) { queue<TreeNode *> q; q.push(root); int level = 0; while (!q.empty()) { bool incr = level % 2 == 0; int val = incr ? 0 : 10000000; for (int size = q.size(); size > 0; size--) { TreeNode *node = q.front(); q.pop(); if (incr) { if (node->val <= val || node->val % 2 == 0) return false; } else { if (node->val >= val || node->val % 2 == 1) return false; } val = node->val; if (node->left) q.push(node->left); if (node->right) q.push(node->right); } level++; } return true; } #endif //CPP_1609__SOLUTION1_H_
ooooo-youwillsee/leetcode
0572-Subtree-of-Another-Tree/cpp_0572/Solution1.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0572_SOLUTION1_H #define CPP_0572_SOLUTION1_H #include "TreeNode.h" class Solution { public: bool isSameTree(TreeNode *s, TreeNode *t) { if (!s && !t) return true; if (!s || !t) return false; return s->val == t->val && isSameTree(s->left, t->left) && isSameTree(s->right, t->right); } bool isSubtree(TreeNode *s, TreeNode *t) { if (!s) return false; return isSameTree(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t); } }; #endif //CPP_0572_SOLUTION1_H
ooooo-youwillsee/leetcode
0049-Group-Anagrams/cpp_0049/Solution1.h
<filename>0049-Group-Anagrams/cpp_0049/Solution1.h // // Created by ooooo on 2020/2/11. // #ifndef CPP_0049__SOLUTION1_H_ #define CPP_0049__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 哈希表 */ class Solution { public: vector<vector<string>> groupAnagrams(vector<string> &strs) { unordered_map<string, vector<string>> map; for (int i = 0; i < strs.size(); ++i) { string key = string(strs[i]); sort(key.begin(), key.end()); map[key].push_back(strs[i]); } vector<vector<string>> ans; for (auto &entry: map) { ans.push_back(entry.second); } return ans; } }; #endif //CPP_0049__SOLUTION1_H_
ooooo-youwillsee/leetcode
1816-Truncate Sentence/cpp_1816/Solution1.h
/** * @author ooooo * @date 2021/4/11 15:50 */ #ifndef CPP_1816__SOLUTION1_H_ #define CPP_1816__SOLUTION1_H_ #include <iostream> #include <sstream> using namespace std; class Solution { public: string truncateSentence(string s, int k) { istringstream in(s); string w; string ans = ""; while (k && (in >> w)) { ans += w; k--; if (k > 0) { ans += " "; } } return ans; } }; #endif //CPP_1816__SOLUTION1_H_
ooooo-youwillsee/leetcode
0098-Validate-Binary-Search-Tree/cpp_0098/Solution2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2019/11/1. // #ifndef CPP_0098_SOLUTION2_H #define CPP_0098_SOLUTION2_H #include <iostream> #include "TreeNode.h" using namespace std; /** * 比较左孩子的val和当前节点的值 */ class Solution { private: TreeNode *prev; bool help(TreeNode *node) { if (node == NULL) { return true; } if (!help(node->left)) { return false; } if (this->prev && this->prev->val >= node->val) { return false; } this->prev = node; return help(node->right); } public: bool isValidBST(TreeNode *root) { this->prev = NULL; return help(root); } }; #endif //CPP_0098_SOLUTION2_H
ooooo-youwillsee/leetcode
lcof_059-2/cpp_059-2/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/25. // #ifndef CPP_059_2__SOLUTION1_H_ #define CPP_059_2__SOLUTION1_H_ #include <iostream> #include <deque> #include <unordered_map> using namespace std; class MaxQueue { public: deque<int> q, help; MaxQueue() { q.clear(); help.clear(); } int max_value() { if (q.empty()) return -1; return help.front(); } void push_back(int value) { q.push_back(value); while (!help.empty() && help.back() < value) help.pop_back(); help.push_back(value); } int pop_front() { if (q.empty()) return -1; int ret = q.front(); q.pop_front(); if (help.front() == ret) help.pop_front(); return ret; } }; #endif //CPP_059_2__SOLUTION1_H_
ooooo-youwillsee/leetcode
1221-Split-a-String-in-Balanced-Strings/cpp_1221/Solution1.h
// // Created by ooooo on 2020/2/3. // #ifndef CPP_1221__SOLUTION1_H_ #define CPP_1221__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: int balancedStringSplit(string s) { int R_count = 0, ans = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'R') R_count++; else if (s[i] == 'L') R_count--; if (R_count == 0) ans += 1; } return ans; } }; #endif //CPP_1221__SOLUTION1_H_
ooooo-youwillsee/leetcode
0242-Valid-Anagram/cpp_0242/Solution2.h
// // Created by ooooo on 2019/11/1. // #ifndef CPP_0242_SOLUTION2_H #define CPP_0242_SOLUTION2_H #include <iostream> #include <string> #include <algorithm> #include <map> using namespace std; class Solution { public: bool isAnagram(string s, string t) { map<char, int> m1; map<char, int> m2; for (auto i : s) { map<char, int>::iterator it = m1.find(i); if (it == m1.end()) { m1[i] = 1; } else { m1[i] = it->second + 1; } } for (auto i : t) { map<char, int>::iterator it = m2.find(i); if (it == m1.end()) { m2[i] = 1; } else { m2[i] = it->second + 1; } } return m1 == m2; } }; #endif //CPP_0242_SOLUTION2_H
ooooo-youwillsee/leetcode
0496-Next-Greater-Element-I/cpp_0496/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/5. // #ifndef CPP_0496_SOLUTION1_H #define CPP_0496_SOLUTION1_H #include <iostream> #include <vector> using namespace std; /** * 暴力破解 */ class Solution { public: vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2) { vector<int> res; for (auto num1: nums1) { int find = -1; for (int i = 0; i < nums2.size(); ++i) { if (nums2[i] == num1) { // 向nums1的后面找 for (int j = i + 1; j < nums2.size(); ++j) { if (nums2[j] > num1) { find = nums2[j]; break; } } break; } } res.push_back(find); } return res; } }; #endif //CPP_0496_SOLUTION1_H
ooooo-youwillsee/leetcode
0145-Binary-Tree-Postorder-Traversal/cpp_0145/TreeNode.h
<filename>0145-Binary-Tree-Postorder-Traversal/cpp_0145/TreeNode.h /** * @author ooooo * @date 2020/9/25 23:33 */ #ifndef CPP_0145__TREENODE_H_ #define CPP_0145__TREENODE_H_ #include <iostream> #include <vector> #include <stack> using namespace std; 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) {} }; #endif //CPP_0145__TREENODE_H_
ooooo-youwillsee/leetcode
0377-Combination Sum IV/cpp_0377/Solution1.h
<filename>0377-Combination Sum IV/cpp_0377/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/4/24 08:34 */ #ifndef CPP_0377__SOLUTION1_H_ #define CPP_0377__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int dfs(vector<int> &nums, int target) { if (memo.find(target) != memo.end()) return memo[target]; if (target == 0) return memo[target] = 1; if (target < 0) return memo[target] = 0; int ans = 0; for (int i = 0; i < nums.size(); i++) { ans += dfs(nums, target - nums[i]); } return memo[target] = ans; } unordered_map<int, int> memo; int combinationSum4(vector<int> &nums, int target) { return dfs(nums, target); } }; #endif //CPP_0377__SOLUTION1_H_
ooooo-youwillsee/leetcode
0073-Set Matrix Zeroes/cpp_0073/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/10/11 20:09 */ #ifndef CPP_0073__SOLUTION1_H_ #define CPP_0073__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: void setZeroes(vector<vector<int>> &matrix) { if (matrix.empty()) return; int m = matrix.size(), n = matrix[0].size(); vector<bool> rows(m), cols(n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) { rows[i] = true; cols[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (rows[i] || cols[j]) { matrix[i][j] = 0; } } } } }; #endif //CPP_0073__SOLUTION1_H_
ooooo-youwillsee/leetcode
0046-Permutations/cpp_0046/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/10. // #ifndef CPP_0046__SOLUTION1_H_ #define CPP_0046__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: void dfs() { if (marked.size() == nums.size()) { ans.push_back(added); return; } for (int i = 0; i < nums.size(); ++i) { if (marked.count(nums[i])) continue; marked.insert(nums[i]); added.push_back(nums[i]); dfs(); marked.erase(nums[i]); added.pop_back(); } } vector<int> nums; vector<vector<int>> ans; unordered_set<int> marked; vector<int> added; vector<vector<int>> permute(vector<int> &nums) { this->nums = nums; dfs(); return ans; } }; #endif //CPP_0046__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_033/cpp_033/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>lcof_033/cpp_033/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/3/21. // #ifndef CPP_033__SOLUTION1_H_ #define CPP_033__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool help(int l, int r) { if (l >= r) return true; int i = r - 1; while (i >= 0) { if (postorder[i] < postorder[r]) { for (int j = 0; j < i; ++j) { if (postorder[j] > postorder[r]) return false; } break; } i--; } return help(l, i) && help(i + 1, r - 1); } vector<int> postorder; bool verifyPostorder(vector<int> &postorder) { if (postorder.empty()) return true; this->postorder = postorder; return help(0, postorder.size() - 1); } }; #endif //CPP_033__SOLUTION1_H_
ooooo-youwillsee/leetcode
0559-Maximum Depth of N-ary Tree/cpp_0559/Solution2.h
// // Created by ooooo on 2019/12/29. // #ifndef CPP_0599_SOLUTION2_H #define CPP_0599_SOLUTION2_H #include "Node.h" class Solution { public: int maxDepth(Node *root) { if (!root) return 0; int max = 0; for (int i = 0; i < root->children.size(); ++i) { int depth = maxDepth(root->children[i]); max = std::max(max, depth); } return max + 1; } }; #endif //CPP_0599_SOLUTION2_H
ooooo-youwillsee/leetcode
0227-Basic Calculator II/cpp_0227/Solution1.h
/** * @author ooooo * @date 2021/3/11 09:25 */ #ifndef CPP_0227__SOLUTION1_H_ #define CPP_0227__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: void calc(stack<long long> &numStack, stack<char> &opStack) { int x = numStack.top(); numStack.pop(); int y = numStack.top(); numStack.pop(); char c = opStack.top(); opStack.pop(); if (c == '+') { numStack.push(y + x); } else if (c == '-') { numStack.push(y - x); } else if (c == '*') { numStack.push(y * x); } else if (c == '/') { numStack.push(y / x); } } /* * 测试用例 * "3+2*2" "1-1+1" "1-1-1" "2*3+4" * */ int calculate(string s) { int i = 0, n = s.size(); stack<long long> numStack; stack<char> opStack; int sign = 1; while (i < n) { while (i < n && s[i] == ' ') i++; if (i >= n) break; if (s[i] == '-') { sign = -sign; i++; opStack.push('+'); continue; } if (s[i] == '+' || s[i] == '*' || s[i] == '/') { opStack.push(s[i]); i++; continue; } long long num = 0; while (i < n && s[i] >= '0' && s[i] <= '9') { num = num * 10 + (s[i] - '0'); i++; } numStack.push(num * sign); sign = 1; if (!opStack.empty() && (opStack.top() == '*' || opStack.top() == '/')) { calc(numStack, opStack); } } while (numStack.size() > 1) { calc(numStack, opStack); } return numStack.top(); } }; #endif //CPP_0227__SOLUTION1_H_
ooooo-youwillsee/leetcode
0021-Merge-Two-Sorted-Lists/cpp_0021/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/23. // #ifndef CPP_0021__SOLUTION2_H_ #define CPP_0021__SOLUTION2_H_ #include "ListNode.h" /** * 使用原先的节点 (效率高) */ class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *dummyHead = new ListNode(0), *cur = dummyHead; while (l1 && l2) { if (l1->val == l2->val) { ListNode *temp1 = l1->next; ListNode *temp2 = l2->next; cur->next = l1; cur = cur->next; cur->next = l2; cur = cur->next; l1 = temp1; l2 = temp2; } else if (l1->val < l2->val) { ListNode *temp1 = l1->next; cur->next = l1; cur = cur->next; l1 = temp1; } else { ListNode *temp2 = l2->next; cur->next = l2; cur = cur->next; l2 = temp2; } } while (l1) { ListNode *temp1 = l1->next; cur->next = l1; cur = cur->next; l1 = temp1; } while (l2) { ListNode *temp2 = l2->next; cur->next = l2; cur = cur->next; l2 = temp2; } return dummyHead->next; } }; #endif //CPP_0021__SOLUTION2_H_
ooooo-youwillsee/leetcode
0861-Score After Flipping Matrix/cpp_0861/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/12/7 16:07 */ #ifndef CPP_0861__SOLUTION2_H_ #define CPP_0861__SOLUTION2_H_ #include <iostream> #include <math.h> #include <vector> using namespace std; class Solution { public: int matrixScore(vector<vector<int>> &A) { int m = A.size(), n = A[0].size(); int ans = m * (1 << (n - 1)); vector<int> cols(n, 0); for (int j = 1; j < n; ++j) { for (int i = 0; i < m; ++i) { if (A[i][0] == 0) { // need flip cols[j] += 1 - A[i][j]; } else { cols[j] += A[i][j]; } } int k = max(cols[j], m - cols[j]); ans += k * (1 << (n - j - 1)); } return ans; } }; #endif //CPP_0861__SOLUTION2_H_
ooooo-youwillsee/leetcode
0977-Squares-of-a-Sorted-Arrays/cpp_0977/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0977-Squares-of-a-Sorted-Arrays/cpp_0977/Solution1.h // // Created by ooooo on 2020/1/7. // #ifndef CPP_0977_SOLUTION1_H #define CPP_0977_SOLUTION1_H #include <iostream> #include <vector> #include <cmath> using namespace std; /** * sort */ class Solution { public: vector<int> sortedSquares(vector<int> &A) { for (int i = 0; i < A.size(); ++i) { A[i] = pow(A[i], 2); } sort(A.begin(), A.end()); return A; } }; #endif //CPP_0977_SOLUTION1_H
ooooo-youwillsee/leetcode
0160-Intersection-of-Two-Linked-Lists/cpp_0160/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/23. // #ifndef CPP_0160__SOLUTION1_H_ #define CPP_0160__SOLUTION1_H_ #include "ListNode.h" #include <unordered_set> /** * 哈希表 */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { unordered_set<ListNode *> s; while (headA) { s.insert(headA); headA = headA->next; } while (headB) { if (s.count(headB)) return headB; headB = headB->next; } return nullptr; } }; #endif //CPP_0160__SOLUTION1_H_
ooooo-youwillsee/leetcode
0590-N-ary-Tree-Postorder-Transversal/cpp_0590/Solution1.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0590_SOLUTION1_H #define CPP_0590_SOLUTION1_H #include "Node.h" class Solution { public: void dfs(Node *node, vector<int> &vec) { if (!node) return; for (auto child: node->children) { dfs(child, vec); } vec.push_back(node->val); } vector<int> postorder(Node *root) { vector<int> vec; dfs(root, vec); return vec; } }; #endif //CPP_0590_SOLUTION1_H
ooooo-youwillsee/leetcode
lcof_046/cpp_046/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/2. // #ifndef CPP_046__SOLUTION2_H_ #define CPP_046__SOLUTION2_H_ #include <iostream> #include <unordered_map> #include <vector> using namespace std; /** * dp[i] = dp[i+1] + ( valid ? dp[i+2] : 0 ) */ class Solution { public: int valid(int i) { if (i + 1 >= s.size() || s[i] == '0' || s[i] > '2') return false; if (s[i] == '1') return true; return s[i] == '2' && s[i + 1] < '6'; } string s; int translateNum(int num) { this->s = to_string(num); int len = s.size(); vector<int> dp(len + 1); dp[len] = 1; for (int i = len - 1; i >= 0; --i) { dp[i] = dp[i + 1] + (valid(i) ? dp[i + 2] : 0); } return dp[0]; } }; #endif //CPP_046__SOLUTION2_H_
ooooo-youwillsee/leetcode
0129-Sum Root to Leaf Numbers/cpp_0129/TreeNode.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/10/29 09:31 */ #ifndef CPP_0129__TREENODE_H_ #define CPP_0129__TREENODE_H_ #include <iostream> #include <vector> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; #endif //CPP_0129__TREENODE_H_
ooooo-youwillsee/leetcode
lcof_007/cpp_007/Solution1.h
// // Created by ooooo on 2020/3/7. // #ifndef CPP_007__SOLUTION1_H_ #define CPP_007__SOLUTION1_H_ #include "TreeNode.h" #include <unordered_map> class Solution { public: TreeNode *dfs(TreeNode *node, int l, int r) { if (l > r) return nullptr; int root_value = preorder[i++]; int root_index = in_map[root_value]; node = new TreeNode(root_value); node->left = dfs(node->left, l, root_index - 1); node->right = dfs(node->right, root_index + 1, r); return node; } vector<int> preorder; unordered_map<int, int> in_map; int i = 0; TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { for (int j = 0, len = inorder.size(); j < len; ++j) { in_map[inorder[j]] = j; } this->preorder = preorder; return dfs(nullptr, 0, inorder.size() - 1); } }; #endif //CPP_007__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_041/cpp_041/Solution2.h
// // Created by ooooo on 2020/3/26. // #ifndef CPP_041__SOLUTION2_H_ #define CPP_041__SOLUTION2_H_ #include <iostream> #include <queue> using namespace std; class MedianFinder { private: vector<int> nums; public: /** initialize your data structure here. */ MedianFinder() { } void addNum(int num) { if (nums.empty()) nums.emplace_back(num); else nums.insert(lower_bound(nums.begin(), nums.end(), num), num); } double findMedian() { int mid = nums.size() / 2; return (nums.size() & 1) == 0 ? (nums[mid - 1] + nums[mid]) / 2.0 : nums[mid]; } }; #endif //CPP_041__SOLUTION2_H_
ooooo-youwillsee/leetcode
lcof_049/cpp_049/Solution3.h
<filename>lcof_049/cpp_049/Solution3.h // // Created by ooooo on 2020/4/6. // #ifndef CPP_049__SOLUTION3_H_ #define CPP_049__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * dp */ class Solution { public: int nthUglyNumber(int n) { vector<int> nums(n, 1); int count = 1, num_2 = 0, num_3 = 0, num_5 = 0; while (count < n) { int min_v = min({nums[num_2] * 2, nums[num_3] * 3, nums[num_5] * 5}); nums[count] = min_v; // 可能会出现 3 * 2 = 6 和 2 * 3 = 6 if (nums[num_2] * 2 == nums[count]) num_2++; if (nums[num_3] * 3 == nums[count]) num_3++; if (nums[num_5] * 5 == nums[count]) num_5++; count++; } return nums[n - 1]; } }; #endif //CPP_049__SOLUTION3_H_
ooooo-youwillsee/leetcode
lcof_010-2/cpp_010-2/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/9. // #ifndef CPP_010_2__SOLUTION1_H_ #define CPP_010_2__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[n] = dp[n-1] + dp[n-2] */ class Solution { public: int numWays(int n) { if (n ==0) return 1; if (n == 1 || n == 2) return n; int a = 1, b = 2, c = 0; for (int i = 3; i <= n; ++i) { c = (a + b) % 1000000007; a = b; b = c; } return c; } }; #endif //CPP_010_2__SOLUTION1_H_
ooooo-youwillsee/leetcode
1812-Determine Color of a Chessboard Square/cpp_1812/Solution1.h
/** * @author ooooo * @date 2021/4/11 07:57 */ #ifndef CPP_1812__SOLUTION1_H_ #define CPP_1812__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool squareIsWhite(string s) { return (s[1] - (s[0] - 'a')) % 2 == 0; } }; #endif //CPP_1812__SOLUTION1_H_
ooooo-youwillsee/leetcode
0461-Hamming-Distance/cpp_0461/Solution2.h
// // Created by ooooo on 2020/2/6. // #ifndef CPP_0461__SOLUTION2_H_ #define CPP_0461__SOLUTION2_H_ #include <iostream> #include <sstream> using namespace std; /** * ^操作, i & (i-1) --> 去掉最后一个1 */ class Solution { public: int hammingDistance(int x, int y) { int res = x ^y, ans = 0; while (res) { ans += 1; res = res & (res - 1); } return ans; } }; #endif //CPP_0461__SOLUTION2_H_
ooooo-youwillsee/leetcode
1629-Slowest Key/cpp_1629/Solution1.h
/** * @author ooooo * @date 2020/11/15 09:39 */ #ifndef CPP_1629__SOLUTION1_H_ #define CPP_1629__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: char slowestKey(vector<int> &releaseTimes, string keysPressed) { vector<pair<int, char>> ans; int prev_time = 0; for (int i = 0; i < keysPressed.size(); ++i) { ans.push_back(make_pair(releaseTimes[i] - prev_time, keysPressed[i])); prev_time = releaseTimes[i]; } sort(ans.begin(), ans.end(), greater<pair<int, char>>()); return ans[0].second; } } #endif //CPP_1629__SOLUTION1_H_
ooooo-youwillsee/leetcode
0917-Reverse-Only-Letters/cpp_0917/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/1. // #ifndef CPP_0917__SOLUTION1_H_ #define CPP_0917__SOLUTION1_H_ #include <iostream> using namespace std; /** * 双指针 */ class Solution { public: string reverseOnlyLetters(string S) { if (S.size() <= 1) return S; int left = 0, right = S.size() - 1; while (true) { while (left <= right && !isalpha(S[left])) left++; while (right >= left && !isalpha(S[right])) right--; if (left > right) break; swap(S[left++], S[right--]); } return S; } }; #endif //CPP_0917__SOLUTION1_H_
ooooo-youwillsee/leetcode
0017-Letter-Combinations-of-a-Phone-Number/cpp_0017/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/7. // #ifndef CPP_0017__SOLUTION1_H_ #define CPP_0017__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<string>> letters = { {"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k", "l"}, {"m", "n", "o"}, {"p", "q", "r", "s"}, {"t", "u", "v"}, {"w", "x", "y", "z"}, }; vector<string> ans; void help(string s, string &digits, int i) { if (i == digits.size()) { ans.push_back(s); return; } for (auto letter: letters[digits[i] - '2']) { help(s + letter, digits, i + 1); } } vector<string> letterCombinations(string digits) { if (digits.empty()) return {}; ans.clear(); help("", digits, 0); return ans; } }; #endif //CPP_0017__SOLUTION1_H_
ooooo-youwillsee/leetcode
0120-Triangle/cpp_0120/Solution2.h
<filename>0120-Triangle/cpp_0120/Solution2.h // // Created by ooooo on 2019/12/16. // #ifndef CPP_0120_SOLUTION2_H #define CPP_0120_SOLUTION2_H #include <iostream> #include <vector> #include <climits> using namespace std; /** * 自底向上 * * res[i] = min (res[i] , res[j]) + res[row][i] */ class Solution { public: int minimumTotal(vector<vector<int>> &triangle) { vector<int> res(triangle.size() + 1, 0); for (int i = triangle.size() - 1; i >= 0; --i) { for (int j = 0; j < triangle[i].size(); ++j) { res[j] = min(res[j], res[j + 1]) + triangle[i][j]; } } return res[0]; } }; #endif //CPP_0120_SOLUTION2_H
ooooo-youwillsee/leetcode
0226-Invert-Binary-Tree/cpp_0226/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/9/15. // #ifndef CPP_0226_SOLUTION1_H #define CPP_0226_SOLUTION1_H #include "TreeNode.h" class Solution { public: TreeNode *invertTree(TreeNode *root) { if (!root) return nullptr; TreeNode *rightNode = root->right; root->right = invertTree(root->left); root->left = invertTree(rightNode); return root; } }; #endif //CPP_0226_SOLUTION1_H
ooooo-youwillsee/leetcode
0018-4Sum/cpp_0018/Solution1.h
<filename>0018-4Sum/cpp_0018/Solution1.h // // Created by ooooo on 2019/11/1. // #ifndef CPP_0018_SOLUTION1_H #define CPP_0018_SOLUTION1_H #include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; /** * 两层循环, 加上双指针 */ class Solution { public: vector<vector<int>> fourSum(vector<int> &nums, int target) { vector<vector<int>> res; if (nums.size() < 4) { return res; } sort(nums.begin(), nums.end()); set<vector<int>> set; for (int i = 0; i < nums.size() - 3; ++i) { for (int j = i + 1; j < nums.size() - 2; ++j) { int k = j + 1; int p = nums.size() - 1; while (k < p) { int n = nums[i] + nums[j] + nums[k] + nums[p]; if (n < target) { k += 1; } else if (n > target) { p -= 1; } else { vector<int> vec = {nums[i], nums[j], nums[k], nums[p]}; set.insert(vec); k += 1; p -= 1; } } } } for (auto item : set) { res.push_back(item); } return res; } }; #endif //CPP_0018_SOLUTION1_H
ooooo-youwillsee/leetcode
1473-Paint House III/cpp_1473/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/4 09:15 */ #ifndef CPP_1473__SOLUTION1_H_ #define CPP_1473__SOLUTION1_H_ #include <vector> #include <iostream> using namespace std; class Solution { public: /** dp[m][n][target] 表示第m个房子,涂第n个颜色,为target的neighborhood的值 */ int minCost(vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target) { vector<vector<vector<int>>> dp(m, vector<vector<int>>(n + 1, vector<int>(target + 1, INT_MAX / 2))); // 没有涂色 if (houses[0] == 0) { for (int j = 1; j <= n; j++) { dp[0][j][1] = cost[0][j - 1]; } } else { dp[0][houses[0]][1] = 0; } for (int i = 1; i < m; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= target; k++) { // 没有涂色 if (houses[i] == 0) { dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k] + cost[i][j - 1]); for (int p = 1; p <= n; p++) { if (p != j) { dp[i][j][k] = min(dp[i][j][k], dp[i - 1][p][k - 1] + cost[i][j - 1]); } } } else { // 当前涂的颜色必须是house[i] if (j != houses[i]) continue; dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k]); for (int p = 1; p <= n; p++) { if (p != j) { dp[i][j][k] = min(dp[i][j][k], dp[i - 1][p][k - 1]); } } } } } } int ans = INT_MAX / 2; for (int j = 1; j <= n; j++) { ans = min(ans, dp[m - 1][j][target]); } return ans == INT_MAX / 2 ? -1 : ans; } }; #endif //CPP_1473__SOLUTION1_H_
ooooo-youwillsee/leetcode
0744-Find-Smallest-Letter-Greater-Than-Target/cpp_0744/Solution2.h
// // Created by ooooo on 2020/1/20. // #ifndef CPP_0744__SOLUTION2_H_ #define CPP_0744__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * loop */ class Solution { public: char nextGreatestLetter(vector<char> &letters, char target) { for (auto c: letters) { if (c > target) return c; } return letters[0]; } }; #endif //CPP_0744__SOLUTION2_H_
ooooo-youwillsee/leetcode
0605-Can-Place-Flowers/cpp_0605/Solution1.h
/** * @author ooooo * @date 2021/1/1 09:57 */ #ifndef CPP_0605__SOLUTION1_H_ #define CPP_0605__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool canPlaceFlowers(vector<int> &f, int n) { if (n == 0) return true; int x = f.size(); if (x == 1 && f[0] == 0 && n == 1) return true; for (int i = 0; i < x; i++) { if (f[i] == 0) { bool can = false; if (i == 0 && i + 1 < x && f[i + 1] == 0) can = true; else if (i == x - 1 && i - 1 >= 0 && f[i - 1] == 0) can = true; else if (i + 1 < x && i - 1 >= 0 && f[i - 1] == 0 && f[i + 1] == 0) can = true; if (can) { n--; f[i] = 1; if (n == 0) return true; } } } return n == 0; } }; #endif //CPP_0605__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_031/cpp_031/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/3/19. // #ifndef CPP_031__SOLUTION1_H_ #define CPP_031__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: bool validateStackSequences(vector<int> &pushed, vector<int> &popped) { stack<int> s; for (int i = 0, j = 0, len = popped.size(); i < len;) { int pop_val = popped[i]; while (s.empty() || s.top() != pop_val) { if (j == len) return false; s.push(pushed[j++]); } s.pop(); i++; } return true; } }; #endif //CPP_031__SOLUTION1_H_
ooooo-youwillsee/leetcode
0720-Longest-Word-in-Dictionary/cpp_0720/Solution1.h
<filename>0720-Longest-Word-in-Dictionary/cpp_0720/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/14. // #ifndef CPP_0720__SOLUTION1_H_ #define CPP_0720__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 超时 */ class Solution { public: string ans = ""; void dfs(string s, int len, unordered_map<int, vector<string>> m) { vector<string> words = m[len]; if (words.empty()) { // 不满足关系说明已经查找完毕 和ans作比较 if (ans.size() < s.size()) { ans = s; } else if (ans.size() == s.size() && s < ans) { ans = s; } return; } for (int i = 0; i < words.size(); ++i) { if (s == words[i].substr(0, len - 1)) { dfs(words[i], len + 1, m); } else { // 不满足关系说明已经查找完毕 和ans作比较 if (ans.size() < s.size()) { ans = s; } else if (ans.size() == s.size() && s < ans) { ans = s; } } } } string longestWord(vector<string> &words) { unordered_map<int, vector<string>> m; for (const auto &word: words) { int len = word.size(); if (m.count(len)) { m[len].push_back(word); } else { m[len] = {word}; } } dfs("", 1, m); return ans; } }; #endif //CPP_0720__SOLUTION1_H_
ooooo-youwillsee/leetcode
0279-Perfect-Squares/cpp_0279/Solution1.h
// // Created by ooooo on 2020/2/23. // #ifndef CPP_0279__SOLUTION1_H_ #define CPP_0279__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * dfs timeout */ class Solution { public: void dfs(int n, int count) { if (n == 0) { ans = min(ans, count); return; } for (int i = 0; i < nums.size(); ++i) { if (nums[i] > n) continue; dfs(n - nums[i], count + 1); } } vector<int> nums; int ans = INT_MAX; int numSquares(int n) { for (int i = int(sqrt(n)); i >= 1; --i) { nums.push_back(i * i); } dfs(n, 0); return ans; } }; #endif //CPP_0279__SOLUTION1_H_
ooooo-youwillsee/leetcode
1356-Sort-Integers-by-The-Number-of-1-Bits/cpp_1356/Solution1.h
/** * @author ooooo * @date 2020/11/6 20:38 */ #ifndef CPP_1356__SOLUTION1_H_ #define CPP_1356__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> sortByBits(vector<int> &arr) { auto fn = [](int n) { int i = 0; while (n) { i++; n = n & (n - 1); } return i; }; sort(arr.begin(), arr.end(), [&](int x, int y) { int c1 = fn(x), c2 = fn(y); return c1 == c2 ? x < y : c1 < c2; }); return arr; } }; #endif //CPP_1356__SOLUTION1_H_
ooooo-youwillsee/leetcode
0836-Rectangle-Overlap/cpp_0836/Solution1.h
/** * @author ooooo * @date 2020/9/30 10:19 */ #ifndef CPP_0836__SOLUTION1_H_ #define CPP_0836__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool isRectangleOverlap(vector<int> &rec1, vector<int> &rec2) { return !( rec1[2] <= rec2[0] || // left rec1[3] <= rec2[1] || // bottom rec1[0] >= rec2[2] || // right rec1[1] >= rec2[3]); // top } }; #endif //CPP_0836__SOLUTION1_H_
ooooo-youwillsee/leetcode
0117-Populating-Next-Right-Pointers-in-Each-Node-II/cpp_0117/Solution1.h
/** * @author ooooo * @date 2020/9/28 09:19 */ #ifndef CPP_0117__SOLUTION1_H_ #define CPP_0117__SOLUTION1_H_ #include "Node.h" class Solution { public: Node *connect(Node *root) { if (!root) return root; queue<Node *> q; q.push(root); vector<vector<Node *>> rows; while (!q.empty()) { vector<Node *> row; for (int i = 0, len = q.size(); i < len; ++i) { auto node = q.front(); q.pop(); row.emplace_back(node); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } rows.emplace_back(row); } for (auto &row :rows) { for (int i = 0; i < row.size() - 1; ++i) { row[i]->next = row[i + 1]; } } return root; } }; #endif //CPP_0117__SOLUTION1_H_
ooooo-youwillsee/leetcode
0088-Merge-Sorted-Array/cpp_0088/Solution1.h
<filename>0088-Merge-Sorted-Array/cpp_0088/Solution1.h // // Created by ooooo on 2019/12/5. // #ifndef CPP_0088_SOLUTION1_H #define CPP_0088_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: void merge(vector<int> &nums1, int m, vector<int> &nums2, int n) { if (nums1.empty() && nums2.empty()) return; vector<int> target(m + n); for (int i = 0, j = 0, k = 0; i < m || j < n; k++) { if (i == m) { target[k] = nums2[j]; j++; continue; } if (j == n) { target[k] = nums1[i]; i++; continue; } if (nums1[i] <= nums2[j]) { target[k] = nums1[i]; i++; } else { target[k] = nums2[j]; j++; } } for (int l = 0; l < m + n; ++l) { nums1[l] = target[l]; } } }; #endif //CPP_0088_SOLUTION1_H
ooooo-youwillsee/leetcode
1773-Count Items Matching a Rule/cpp_1773/Solution1.h
/** * @author ooooo * @date 2021/3/14 14:52 */ #ifndef CPP_1773__SOLUTION1_H_ #define CPP_1773__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <numeric> #include <queue> using namespace std; class Solution { public: int countMatches(vector<vector<string>> &items, string ruleKey, string ruleValue) { int ans = 0; for (auto &item: items) { if (ruleKey == "type" && ruleValue == item[0]) { ans++; } else if (ruleKey == "color" && ruleValue == item[1]) { ans++; } else if (ruleKey == "name" && ruleValue == item[2]) { ans++; } } return ans; } }; #endif //CPP_1773__SOLUTION1_H_
ooooo-youwillsee/leetcode
1546-Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/cpp_1546/Solution1.h
/** * @author ooooo * @date 2021/3/9 11:59 */ #ifndef CPP_1546__SOLUTION1_H_ #define CPP_1546__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; // timeout class Solution { public: int maxNonOverlapping(vector<int> &nums, int target) { int n = nums.size(); vector<int> preSum(n + 1); for (int i = 0; i < n; ++i) { preSum[i + 1] = preSum[i] + nums[i]; } vector<int> dp(n); for (int i = 0; i < n; ++i) { if (i > 0) { dp[i] = dp[i - 1]; } for (int j = i; j >= 0; --j) { if (preSum[i + 1] - preSum[j] == target) { dp[i] = max(dp[i], (j > 0 ? dp[j - 1] : 0) + 1); } } } return dp[n - 1]; } }; #endif //CPP_1546__SOLUTION1_H_
ooooo-youwillsee/leetcode
0160-Intersection-of-Two-Linked-Lists/cpp_0160/Solution2.h
// // Created by ooooo on 2020/1/23. // #ifndef CPP_0160__SOLUTION2_H_ #define CPP_0160__SOLUTION2_H_ #include "ListNode.h" #include <unordered_set> /** * 双指针 (循环走A-B) */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *pA = headA, *pB = headB; while (pA != pB) { pA = pA ? pA->next : headB; pB = pB ? pB->next : headA; } return pA; } }; #endif //CPP_0160__SOLUTION2_H_
ooooo-youwillsee/leetcode
0264-Ugly Number II/cpp_0264/Solution2.h
<filename>0264-Ugly Number II/cpp_0264/Solution2.h /** * @author ooooo * @date 2021/4/11 15:17 */ #ifndef CPP_0264__SOLUTION2_H_ #define CPP_0264__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: using ll = long long; int nthUglyNumber(int n) { vector<ll> dp(n, 1); int p2 = 0, p3 = 0, p5 = 0; for (int i = 0; i < n - 1; i++) { ll a = dp[p2] * 2, b = dp[p3] * 3, c = dp[p5] * 5; ll v = min(a, min(b, c)); dp[i + 1] = v; if (v == a) { p2++; } if (v == b) { p3++; } if (v == c) { p5++; } } return dp[n - 1]; } }; #endif //CPP_0264__SOLUTION2_H_
ooooo-youwillsee/leetcode
0001-Two-Sum/cpp_0001/Solution1.h
#include <vector> #include <iostream> using namespace std; /** * 暴力破解法 */ class Solution1 { public: static vector<int> twoSum(vector<int> &nums, int target) { vector<int> result; for (int i = 0; i < nums.size(); ++i) { for (int j = 1; j < nums.size(); ++j) { if (nums[j] + nums[i] == target) { result.push_back(i); result.push_back(j); break; } } } return result; } };
ooooo-youwillsee/leetcode
0403-Frog Jump/cpp_0403/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/4/30 19:57 */ #ifndef CPP_0403__SOLUTION1_H_ #define CPP_0403__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: /* * i 表示第几个位置, step 表示上一步的步数 */ bool dfs(int i, int step) { if (memo.find(i) != memo.end()) { if (memo[i].find(step) != memo[i].end()) { return memo[i][step]; } } if (i == can.size()) return true; if (i > can.size()) return false; bool pass = false; if (i + step - 1 > i && can[i+step-1]) { pass = dfs(i + step - 1, step - 1); } if (i + step > i && can[i+step]) { pass = pass || dfs(i + step, step); } if (i + step + 1 > i && can[i+step+1]) { pass = pass || dfs(i + step + 1, step + 1); } return memo[i][step] = pass; } vector<bool> can; unordered_map<int, unordered_map<int, bool>> memo; bool canCross(vector<int>& stones) { can.assign(stones[stones.size() - 1], false); for (auto i: stones){ can[i] = true; } if (!can[1]) return false; return dfs(1, 1); } }; #endif //CPP_0403__SOLUTION1_H_
ooooo-youwillsee/leetcode
1269-Number of Ways to Stay in the Same Place After Some Steps/cpp_1269/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/14 21:06 */ #ifndef CPP_1269__SOLUTION2_H_ #define CPP_1269__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: const int mod = 1e9 + 7; int numWays(int steps, int arrLen) { int maxJ = min(arrLen, steps); vector<vector<long long>> dp(steps + 1, vector<long long>(maxJ, 0)); dp[0][0] = 1; for (int i = 1; i <= steps; i++) { for (int j = 0; j < maxJ; j++) { dp[i][j] = dp[i - 1][j]; if (j - 1 >= 0) { dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod; } if (j + 1 < maxJ) { dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % mod; } } } return dp[steps][0]; } }; #endif //CPP_1269__SOLUTION2_H_
ooooo-youwillsee/leetcode
0554-Brick Wall/cpp_0554/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/2 08:14 */ #ifndef CPP_0554__SOLUTION1_H_ #define CPP_0554__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int leastBricks(vector<vector<int>> &wall) { unordered_map<int, int> m; for (int i = 0; i < wall.size(); i++) { vector<int> w = wall[i]; int preSum = 0; for (int j = 0; j < w.size() - 1; j++) { preSum += w[j]; m[preSum]++; } } int maxCol = 0; for (auto &e: m) { maxCol = max(maxCol, e.second); } return wall.size() - maxCol; } }; #endif //CPP_0554__SOLUTION1_H_
ooooo-youwillsee/leetcode
0279-Perfect-Squares/cpp_0279/Solution3.h
<filename>0279-Perfect-Squares/cpp_0279/Solution3.h // // Created by ooooo on 2020/2/23. // #ifndef CPP_0279__SOLUTION3_H_ #define CPP_0279__SOLUTION3_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; /** * dp[i] = min(dp[i], dp[i - j*j] + 1 ) */ class Solution { public: int numSquares(int n) { vector<int> dp(n + 1, 0); for (int i = 1; i <= n; ++i) { dp[i] = i; // 最多就是所有的 1 相加 for (int j = 1; i - j * j >= 0; ++j) { dp[i] = min(dp[i], dp[i - j * j] + 1); } } return dp[n]; } }; #endif //CPP_0279__SOLUTION3_H_
ooooo-youwillsee/leetcode
0307-Range-Sum-Query-Mutable/cpp_0307/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0307-Range-Sum-Query-Mutable/cpp_0307/Solution1.h // // Created by ooooo on 2020/2/2. // #ifndef CPP_0307__SOLUTION1_H_ #define CPP_0307__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class NumArray { private: template<typename T> struct SegmentTree { vector<T> tree, data; SegmentTree(vector<T> data) : data(data.size()), tree(data.size() * 4) { for (int i = 0; i < data.size(); ++i) { this->data[i] = data[i]; } if (!data.empty()) { buildSegmentTree(0, 0, data.size() - 1); } } void buildSegmentTree(int treeIndex, int left, int right) { if (left == right) { this->tree[treeIndex] = this->data[left]; return; } int mid = left + (right - left) / 2; buildSegmentTree(leftChild(treeIndex), left, mid); buildSegmentTree(rightChild(treeIndex), mid + 1, right); tree[treeIndex] = merge(tree[leftChild(treeIndex)], tree[rightChild(treeIndex)]); } T query(int queryL, int queryR) { return query(0, 0, data.size() - 1, queryL, queryR); } T query(int treeIndex, int left, int right, int queryL, int queryR) { if (queryL == left && queryR == right) { return tree[treeIndex]; } int mid = left + (right - left) / 2; if (queryL > mid) { return query(rightChild(treeIndex), mid + 1, right, queryL, queryR); } else if (queryR <= mid) { return query(leftChild(treeIndex), left, mid, queryL, queryR); } T leftResult = query(leftChild(treeIndex), left, mid, queryL, mid); T rightResult = query(rightChild(treeIndex), mid + 1, right, mid + 1, queryR); return merge(leftResult, rightResult); } void set(int i, T v) { this->data[i] = v; set(0, 0, data.size() - 1, i); } void set(int treeIndex, int left, int right, int targetIndex) { if (left == right) { tree[treeIndex] = data[left]; return; } int mid = left + (right - left) / 2; if (targetIndex > mid) { set(rightChild(treeIndex), mid + 1, right, targetIndex); } else if (targetIndex <= mid) { set(leftChild(treeIndex), left, mid, targetIndex); } tree[treeIndex] = merge(tree[leftChild(treeIndex)], tree[rightChild(treeIndex)]); } int leftChild(int i) { return 2 * i + 1; } int rightChild(int i) { return 2 * i + 2; } T merge(T a, T b) { return a + b; } }; SegmentTree<int> segmentTree; int size; public: NumArray(vector<int> &nums) : segmentTree(nums) { } void update(int i, int val) { segmentTree.set(i, val); } int sumRange(int i, int j) { return segmentTree.query(i, j); } }; #endif //CPP_0307__SOLUTION1_H_
ooooo-youwillsee/leetcode
1122-Relative-Sort-Array/cpp_1122/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/11/14 20:22 */ #ifndef CPP_1122__SOLUTION1_H_ #define CPP_1122__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> relativeSortArray(vector<int> &arr1, vector<int> &arr2) { unordered_map<int, int> indexes_map; for (int i = 0; i < arr2.size(); ++i) { indexes_map[arr2[i]] = i; } vector<int> ans, other; for (auto &item: arr1) { if (indexes_map.find(item) != indexes_map.end()) { ans.push_back(item); } else { other.push_back(item); } } sort(ans.begin(), ans.end(), [&](int x, int y) { return indexes_map[x] < indexes_map[y]; }); sort(other.begin(), other.end()); ans.insert(ans.end(), other.begin(), other.end()); return ans; } }; #endif //CPP_1122__SOLUTION1_H_
ooooo-youwillsee/leetcode
1319-Number of Operations to Make Network Connected/cpp_1319/Solution1.h
/** * @author ooooo * @date 2021/1/23 12:45 */ #ifndef CPP_1319__SOLUTION1_H_ #define CPP_1319__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: struct UF { int n; vector<int> p; UF(int n) : n(n), p(n) { for (int i = 0; i < n; ++i) { p[i] = i; } } int find(int i) { if (p[i] == i) return i; return p[i] = find(p[i]); } bool connected(int i, int j) { int pi = find(i), pj = find(j); if (pi == pj) return true; return false; } void connect(int i, int j) { int pi = find(i), pj = find(j); if (pi == pj) return; p[pi] = pj; n--; } }; int makeConnected(int n, vector<vector<int>> &connections) { UF uf(n); int idle = 0; for (int i = 0; i < connections.size(); ++i) { if (uf.connected(connections[i][0], connections[i][1])) { idle++; } else { uf.connect(connections[i][0], connections[i][1]); } } int ans = 0; for (int i = 0; i < n; ++i) { if (!uf.connected(0, i)) { ans++; uf.connect(0, i); if (ans > idle) return -1; } } return ans; } }; #endif //CPP_1319__SOLUTION1_H_
ooooo-youwillsee/leetcode
0167-Two-Sum-II-Input-array-is-sorted/cpp_0167/Solution1.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0167_SOLUTION1_H #define CPP_0167_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> twoSum(vector<int> &numbers, int target) { for (int i = 0, j = numbers.size() - 1; i < j;) { int sum = numbers[i] + numbers[j]; if (sum == target) return {i + 1, j + 1}; else if (sum < target) i++; else j--; } return {}; } }; #endif //CPP_0167_SOLUTION1_H
ooooo-youwillsee/leetcode
0078-Subsets/cpp_0078/Solution3.h
/** * @author ooooo * @date 2020/9/20 12:14 */ #ifndef CPP_0078__SOLUTION3_H_ #define CPP_0078__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> ans; vector<int> nums; void dfs(vector<int> &num, int i) { ans.push_back(num); if (i == nums.size()) return; for (int j = i; j < nums.size(); ++j) { num.push_back(nums[j]); dfs(num, j + 1); num.pop_back(); } } vector<vector<int>> subsets(vector<int> &nums) { sort(nums.begin(), nums.end()); this->nums = nums; vector<int> num; dfs(num, 0); return ans; } }; #endif //CPP_0078__SOLUTION3_H_
ooooo-youwillsee/leetcode
1793-Maximum Score of a Good Subarray/cpp_1793/Solution1.h
/** * @author ooooo * @date 2021/3/28 12:14 */ #ifndef CPP_1793__SOLUTION1_H_ #define CPP_1793__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <numeric> #include <queue> using namespace std; class Solution { public: int maximumScore(vector<int> &nums, int k) { int n = nums.size(); int ans = 0; int i = k, j = k; int minValue = nums[k]; while (i >= 0 && j < n) { ans = max(ans, minValue * (j - i + 1)); if (i - 1 >= 0 && j + 1 < n) { if (nums[i - 1] <= nums[j + 1]) { minValue = min(minValue, nums[j + 1]); j++; } else { minValue = min(minValue, nums[i - 1]); i--; } } else { break; } } int p = i, q = j; while (i >= 0) { minValue = min(minValue, nums[i]); ans = max(ans, minValue * (q - i + 1)); i--; } while (j < n) { minValue = min(minValue, nums[j]); ans = max(ans, minValue * (j - p + 1)); j++; } return ans; } }; #endif //CPP_1793__SOLUTION1_H_
ooooo-youwillsee/leetcode
0083-Remove-Duplicates-from-Sorted-List/cpp_0083/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/23. // #ifndef CPP_0083__SOLUTION2_H_ #define CPP_0083__SOLUTION2_H_ #include "ListNode.h" /** * recursion */ class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if (!head) return nullptr; head->next = deleteDuplicates(head->next); return head->next && head->next->val == head->val ? head->next : head; } }; #endif //CPP_0083__SOLUTION2_H_
ooooo-youwillsee/leetcode
lcof_054/cpp_054/Solution1.h
// // Created by ooooo on 2020/4/11. // #ifndef CPP_054__SOLUTION1_H_ #define CPP_054__SOLUTION1_H_ #include "TreeNode.h" #include <vector> class Solution { public: void dfs(TreeNode *node) { if (!node) return; dfs(node->left); nums.push_back(node->val); dfs(node->right); } vector<int> nums; int kthLargest(TreeNode *root, int k) { dfs(root); return nums[nums.size() - k]; } }; #endif //CPP_054__SOLUTION1_H_
ooooo-youwillsee/leetcode
0169-Majority-Element/cpp_0169/Solution1.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0169_SOLUTION1_H #define CPP_0169_SOLUTION1_H #include <vector> #include <iostream> #include <map> using namespace std; class Solution { public: int majorityElement(vector<int> &nums) { map<int, int> m; int size = nums.size(); for (auto i: nums) { map<int, int>::iterator it = m.find(i); if (it == m.end()) { m[i] = 1; } else { m[i] = it->second + 1; } if (m[i] > size / 2) { return i; } } return 0; } }; #endif //CPP_0169_SOLUTION1_H
ooooo-youwillsee/leetcode
1108-Defanging-an-IP-Address/cpp_1108/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>1108-Defanging-an-IP-Address/cpp_1108/Solution1.h // // Created by ooooo on 2020/2/2. // #ifndef CPP_1108__SOLUTION1_H_ #define CPP_1108__SOLUTION1_H_ #include <iostream> #include <sstream> using namespace std; class Solution { public: string defangIPaddr(string address) { string target = "[.]"; stringstream ss; for (auto &c: address) { if (c == '.') { ss << target; } else { ss << c; } } return ss.str(); } }; #endif //CPP_1108__SOLUTION1_H_
ooooo-youwillsee/leetcode
0086-Partition List/cpp_0086/Solution1.h
/** * @author ooooo * @date 2021/1/4 16:16 */ #ifndef CPP_0086__SOLUTION1_H_ #define CPP_0086__SOLUTION1_H_ #include "ListNode.h" class Solution { public: ListNode *partition(ListNode *head, int x) { ListNode *dummyHead = new ListNode(-1); dummyHead->next = head; ListNode *cur = dummyHead; while (cur && cur->next) { if (cur->next->val >= x) { break; } cur = cur->next; } ListNode *prev = cur; while (cur && cur->next) { if (cur->next->val < x) { auto prev_next = prev->next; auto cur_next = cur->next->next; prev->next = cur->next; prev->next->next = prev_next; cur->next = cur_next; prev = prev->next; continue; } cur = cur->next; } return dummyHead->next; } }; #endif //CPP_0086__SOLUTION1_H_
ooooo-youwillsee/leetcode
0783-Minimum-Distance-Between-BST-Nodes/cpp_0783/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/5. // #ifndef CPP_0783_SOLUTION1_H #define CPP_0783_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { public: void dfs(TreeNode *node, vector<int> &vec) { if (!node) return; dfs(node->left, vec); vec.push_back(node->val); dfs(node->right, vec); } int minDiffInBST(TreeNode *root) { if (!root) return 0; vector<int> vec; dfs(root, vec); int min = INT_MAX; for (int i = 0; i < vec.size() - 1; ++i) { min = std::min(min, vec[i + 1] - vec[i]); } return min; } }; #endif //CPP_0783_SOLUTION1_H
ooooo-youwillsee/leetcode
0315-Count of Smaller Numbers After Self/cpp_0315/Solution2.h
/** * @author ooooo * @date 2020/11/27 13:54 */ #ifndef CPP_0315__SOLUTION2_H_ #define CPP_0315__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> nums, tmp, indexes, ans, tmp_indexes; void merge(int l, int mid, int r) { copy(nums.begin() + l, nums.begin() + r + 1, tmp.begin() + l); copy(indexes.begin() + l, indexes.begin() + r + 1, tmp_indexes.begin() + l); int i = l, j = mid + 1; int k = l; while (i <= mid && j <= r) { if (tmp[i] <= tmp[j]) { nums[k] = tmp[i]; indexes[k] = tmp_indexes[i]; // 经典!!! ans[tmp_indexes[i]] += (j - mid - 1); i++; } else { //for (int p = i; p <= mid; ++p) { // ans[tmp_indexes[p]]++; //} nums[k] = tmp[j]; indexes[k] = tmp_indexes[j]; j++; } k++; } while (i <= mid) { nums[k] = tmp[i]; indexes[k] = tmp_indexes[i]; ans[tmp_indexes[i]] += (j - mid - 1); i++; k++; } while (j <= r) { nums[k] = tmp[j]; indexes[k] = tmp_indexes[j]; j++; k++; } } void mergeSort(int l, int r) { if (l >= r) return; int mid = l + (r - l) / 2; mergeSort(l, mid); mergeSort(mid + 1, r); merge(l, mid, r); } vector<int> countSmaller(vector<int> &nums) { int n = nums.size(); this->nums = nums; ans.assign(n, 0); tmp.assign(n, 0); indexes.assign(n, 0); tmp_indexes.assign(n, 0); for (int i = 0; i < n; ++i) { indexes[i] = i; } mergeSort(0, nums.size() - 1); //for (auto &num : this->nums) { // cout << num << " "; //} //cout << endl; return ans; } }; #endif //CPP_0315__SOLUTION2_H_
ooooo-youwillsee/leetcode
0409-Longest-Palindrome/cpp_0409/Solution1.h
<filename>0409-Longest-Palindrome/cpp_0409/Solution1.h // // Created by ooooo on 2020/1/11. // #ifndef CPP_0409__SOLUTION1_H_ #define CPP_0409__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: int longestPalindrome(string s) { if (s.empty()) return 0; unordered_map<char, int> m; for (auto &c: s) { m[c]++; } int i = 0, j = 0; for (auto &entry: m) { j += entry.second / 2; if (entry.second % 2 == 1) { // 有多余的置为1 i = 1; } } return i + j * 2; } }; #endif //CPP_0409__SOLUTION1_H_
ooooo-youwillsee/leetcode
0300-Longest-Increasing-Subsequence/cpp_0300/Solution1.h
// // Created by ooooo on 2020/2/24. // #ifndef CPP_0300__SOLUTION1_H_ #define CPP_0300__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] = num[j] < num[i] ==> max( dp[i] , dp[j] + 1 ) */ class Solution { public: int lengthOfLIS(vector<int> &nums) { int n = nums.size(), ans = 0; if (n <= 1) return n; vector<int> dp(n, 1); for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { if (nums[j] < nums[i]) { dp[i] = max(dp[i], dp[j] + 1); } ans = max(ans, dp[i]); } } return ans; } }; #endif //CPP_0300__SOLUTION1_H_
ooooo-youwillsee/leetcode
0119-Pascals-Triangle-II/cpp_0119/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/6. // #ifndef CPP_0119_SOLUTION1_H #define CPP_0119_SOLUTION1_H #include <iostream> #include <vector> using namespace std; /** * 动态规划 */ class Solution { public: vector<int> getRow2(int rowIndex) { if (rowIndex == 0) return {1}; vector<int> ans(rowIndex + 1, 1); for (int i = 1; i <= rowIndex; ++i) { for (int rightIndex = i - 1; rightIndex > 0; --rightIndex) { ans[rightIndex] += ans[rightIndex - 1]; } } return ans; } vector<int> getRow(int rowIndex) { vector<int> ans; for (int i = 0; i <= rowIndex; ++i) { ans.push_back(1); for (int rightIndex = i - 1; rightIndex > 0; --rightIndex) { ans[rightIndex] += ans[rightIndex - 1]; } } return ans; } }; #endif //CPP_0119_SOLUTION1_H
ooooo-youwillsee/leetcode
1237-Find-Positive-Integer-Solution-for-a-Given-Equation/cpp_1237/CustomFunction.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/22. // #ifndef CPP_1237__CUSTOMFUNCTION_H_ #define CPP_1237__CUSTOMFUNCTION_H_ #include <iostream> #include <vector> using namespace std; class CustomFunction { public: // Returns f(x, y) for any given positive integers x and y. // Note that f(x, y) is increasing with respect to both x and y. // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1) int f(int x, int y) { return x + y; } }; #endif //CPP_1237__CUSTOMFUNCTION_H_
ooooo-youwillsee/leetcode
0034-Search-for-a-Range/cpp_0034/Solution2.h
<filename>0034-Search-for-a-Range/cpp_0034/Solution2.h // // Created by ooooo on 2020/2/9. // #ifndef CPP_0034__SOLUTION2_H_ #define CPP_0034__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 二分法 (找到左边界和右边界) */ class Solution { public: int findBound(vector<int> nums, int target, bool isLeft) { if (nums.empty()) return -1; int left = 0, right = nums.size() - 1; while (left < right) { // isLeft ? 0 : 1 --> 默认向左偏 int mid = left + (right - left + (isLeft ? 0 : 1)) / 2; if (nums[mid] > target) { right = mid - 1; } else if (nums[mid] < target) { left = mid + 1; } else { if (isLeft) { right = mid; } else { left = mid; } } } return nums[left] == target ? left : -1; } vector<int> searchRange(vector<int> &nums, int target) { if (nums.empty()) return {-1, -1}; int leftIndex = findBound(nums, target, true); if (leftIndex == -1) return {-1, -1}; return {leftIndex, findBound(nums, target, false)}; } }; #endif //CPP_0034__SOLUTION2_H_
ooooo-youwillsee/leetcode
1813-Sentence Similarity III/cpp_1813/Solution1.h
/** * @author ooooo * @date 2021/4/11 15:31 */ #ifndef CPP_1813__SOLUTION1_H_ #define CPP_1813__SOLUTION1_H_ #include <iostream> #include <vector> #include <sstream> #include <queue> using namespace std; class Solution { public: bool areSentencesSimilar(string sentence1, string sentence2) { auto split = [](const string &s) { istringstream in(s); string w; deque<string> words; while (in >> w) { words.push_back(w); } return words; }; if (sentence1.size() < sentence2.size()) swap(sentence1, sentence2); deque<string> word1 = split(sentence1), word2 = split(sentence2); while (!word2.empty() && word1.front() == word2.front()) { word1.pop_front(); word2.pop_front(); } while (!word2.empty() && word1.back() == word2.back()) { word1.pop_back(); word2.pop_back(); } return word2.empty(); } }; #endif //CPP_1813__SOLUTION1_H_
ooooo-youwillsee/leetcode
0014-Longest-Common-Prefix/cpp_0014/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/24. // #ifndef CPP_0014__SOLUTION1_H_ #define CPP_0014__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string check(string s1, string s2) { int len = s1.size() < s2.size() ? s1.size() : s2.size(); string ans = ""; for (int i = 0; i < len; ++i) { if (s1[i] != s2[i]) return ans; ans += string(1, s1[i]); } return ans; } string longestCommonPrefix(vector<string> &strs) { if (strs.empty()) return ""; string ans = strs[0]; for (int i = 1; i < strs.size(); ++i) { ans = check(ans, strs[i]); } return ans; } }; #endif //CPP_0014__SOLUTION1_H_
ooooo-youwillsee/leetcode
0395-Longest Substring with At Least K Repeating Characters/cpp_0395/Solution2.h
/** * @author ooooo * @date 2021/2/27 11:48 */ #ifndef CPP_0395__SOLUTION2_H_ #define CPP_0395__SOLUTION2_H_ #include <vector> #include <iostream> #include <unordered_map> using namespace std; // 分治法 class Solution { public: int dfs(string &s, int l, int r, int k) { if (l > r) return 0; int n = r + 1; vector<int> m(26, 0); for (int i = l; i < n; ++i) { m[s[i] - 'a']++; } char split = '0'; for (int i = 0; i < 26; ++i) { if (m[i] > 0 && m[i] < k) { // 这个字符没有k个,以它为分界线 split = i + 'a'; break; } } if (split == '0') { return r - l + 1; } int ans = 0; int i = l; while (i < n) { while (i < n && s[i] == split) { i++; } if (i >= n) { return 0; } int j = i; while (j < n && s[j] != split) { j++; } ans = max(ans, dfs(s, i, j - 1, k)); i = j; } return ans; } int longestSubstring(string s, int k) { return dfs(s, 0, s.size() - 1, k); } }; #endif //CPP_0395__SOLUTION2_H_
ooooo-youwillsee/leetcode
1806-Minimum Number of Operations to Reinitialize a Permutation/cpp_1806/Solution1.h
<filename>1806-Minimum Number of Operations to Reinitialize a Permutation/cpp_1806/Solution1.h /** * @author ooooo * @date 2021/4/4 08:30 */ #ifndef CPP_1806__SOLUTION1_H_ #define CPP_1806__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <numeric> #include <queue> using namespace std; class Solution { public: int reinitializePermutation(int n) { int ans = 0; for (int i = 0; i < n; ++i) { int cnt = 0; int cur = i, next = i; do { if (next % 2 == 0) { next = next / 2; } else { next = n / 2 + (next - 1) / 2; } cnt++; } while (next != cur); ans = max(ans, cnt); } return ans; } }; #endif //CPP_1806__SOLUTION1_H_
ooooo-youwillsee/leetcode
1684-Count the Number of Consistent Strings/cpp_1684/Solution1.h
/** * @author ooooo * @date 2020/12/20 13:03 */ #ifndef CPP_1684__SOLUTION1_H_ #define CPP_1684__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int countConsistentStrings(string allowed, vector<string> &words) { int ans = 0; unordered_set<char> set(allowed.begin(), allowed.end()); for (auto &word: words) { bool flag = true; for (auto &c: word) { if (!set.count(c)) { flag = false; break; } } if (flag) { ans++; } } return ans; } }; #endif //CPP_1684__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_040/cpp_040/Solution2.h
// // Created by ooooo on 2020/3/25. // #ifndef CPP_040__SOLUTION2_H_ #define CPP_040__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> getLeastNumbers(vector<int> &arr, int k) { if (k == 0) return {}; sort(arr.begin(), arr.end()); return vector<int>(arr.begin(), arr.begin() + k); } }; #endif //CPP_040__SOLUTION2_H_
ooooo-youwillsee/leetcode
1726-Tuple with Same Product/cpp_1726/Solution1.h
/** * @author ooooo * @date 2021/1/24 17:07 */ #ifndef CPP_1726__SOLUTION1_H_ #define CPP_1726__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <unordered_set> #include <set> using namespace std; class Solution { public: int tupleSameProduct(vector<int> &nums) { int ans = 0; int n = nums.size(); unordered_map<int, int> m; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int x = nums[i] * nums[j]; ans += m[x] * 4 * 2; m[x]++; } } return ans; } }; #endif //CPP_1726__SOLUTION1_H_
ooooo-youwillsee/leetcode
0477-Total Hamming Distance/cpp_0477/Solution1.h
/** * @author ooooo * @date 2021/5/28 11:13 */ #ifndef CPP_0477__SOLUTION1_H_ #define CPP_0477__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int totalHammingDistance(vector<int> &nums) { int ans = 0; int n = nums.size(); for (int i = 0; i < 32; i++) { int cnt_1 = 0; for (int j = 0; j < n; j++) { cnt_1 += (nums[j] >> i & 1); } ans += (n - cnt_1) * cnt_1; } return ans; } }; #endif //CPP_0477__SOLUTION1_H_
ooooo-youwillsee/leetcode
0219-Contains-Duplicate-II/cpp_0219/Solution1.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0219_SOLUTION1_H #define CPP_0219_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 存在 索引差为k 就返回true */ class Solution { public: bool containsNearbyDuplicate(vector<int> &nums, int k) { unordered_map<int, int> m; for (int i = 0, len = nums.size(); i < len; ++i) { if (m.count(nums[i]) && i - m[nums[i]] <= k) { return true; } m[nums[i]] = i; } return false; } }; #endif //CPP_0219_SOLUTION1_H
ooooo-youwillsee/leetcode
0206-Reverse-Linked-List/cpp_0206/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2019/10/29. // #ifndef CPP_0206_SOLUTION2_H #define CPP_0206_SOLUTION2_H #include <iostream> #include "ListNode.h" using namespace std; class Solution2 { public: ListNode *reverseList(ListNode *head) { if (head == NULL || head->next == NULL) return head; ListNode *pNode = reverseList(head->next); head->next->next = head; head->next = NULL; return pNode; } }; #endif //CPP_0206_SOLUTION2_H
ooooo-youwillsee/leetcode
0122-Best-Time-to-Buy-and-Sell-Stock-II/cpp_0122/Solution1.h
<filename>0122-Best-Time-to-Buy-and-Sell-Stock-II/cpp_0122/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2019/11/4. // #ifndef CPP_0122_SOLUTION1_H #define CPP_0122_SOLUTION1_H #include <iostream> #include <vector> using namespace std; /** * 贪心算法 */ class Solution { public: int maxProfit(vector<int> &prices) { int res = 0; for (int i = 0; !prices.empty() && i < prices.size() - 1; ++i) { if (prices[i + 1] > prices[i]) { res += prices[i + 1] - prices[i]; } } return res; } }; #endif //CPP_0122_SOLUTION1_H
ooooo-youwillsee/leetcode
lcof_039/cpp_039/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/24. // #ifndef CPP_039__SOLUTION1_H_ #define CPP_039__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * hash */ class Solution { public: int majorityElement(vector<int> &nums) { unordered_map<int, int> num_map; int n = nums.size() / 2; for (auto &num: nums) { num_map[num]++; if (num_map[num] > n) return num; } return -1; } }; #endif //CPP_039__SOLUTION1_H_
ooooo-youwillsee/leetcode
0347-Top-K-Frequent-Elements/cpp_0347/Solution2.h
<filename>0347-Top-K-Frequent-Elements/cpp_0347/Solution2.h<gh_stars>10-100 /** * @author ooooo * @date 2020/9/7 11:08 */ #ifndef CPP_0347__SOLUTION2_H_ #define CPP_0347__SOLUTION2_H_ #include <vector> #include <queue> #include <map> #include <iostream> using namespace std; class Solution { public: template <typename T> struct comp{ bool operator()(const T &p1, const T &p2) const { return p2.second > p1.second; } }; vector<int> topKFrequent(vector<int> &nums, int k) { map<int, int> m; for (auto num : nums) m[num]++; priority_queue<pair<int, int>, vector<pair<int, int>>, comp<pair<int,int>>> q; for (auto[k, v]: m) { q.push({k, v}); } vector<int> ans; while (k > 0) { ans.push_back(q.top().first); q.pop(); k--; } return ans; } }; #endif //CPP_0347__SOLUTION2_H_
ooooo-youwillsee/leetcode
0104-Maximum-Depth-of-Binary-Tree/cpp_0104/Solution1.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0104_SOLUTION1_H #define CPP_0104_SOLUTION1_H #include "TreeNode.h" using namespace std; class Solution { private: int max = 0; void dfs(int level, TreeNode *node) { if (!node) return; if (level > max) max = level; dfs(level + 1, node->left); dfs(level + 1, node->right); } public: int maxDepth(TreeNode *root) { dfs(1, root); return this->max; } }; #endif //CPP_0104_SOLUTION1_H
ooooo-youwillsee/leetcode
0441-Arranging-Coins/cpp_0441/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/19. // #ifndef CPP_0441__SOLUTION1_H_ #define CPP_0441__SOLUTION1_H_ #include <iostream> using namespace std; /** * loop */ class Solution { public: int arrangeCoins(int n) { long long sum = 0, i = 0; while (true) { if (sum == n) return i; if (sum > n) return i - 1; sum += (++i); } } }; #endif //CPP_0441__SOLUTION1_H_
ooooo-youwillsee/leetcode
0989-Add to Array-Form of Integer/cpp_0989/Solution1.h
/** * @author ooooo * @date 2021/1/22 22:45 */ #ifndef CPP_0989__SOLUTION1_H_ #define CPP_0989__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> addToArrayForm(vector<int> &A, int K) { vector<int> ans; int carry = 0, i = A.size() - 1; while (K) { int a = carry + K % 10; if (i >= 0) { a += A[i]; } carry = a / 10; ans.push_back(a % 10); i--; K /= 10; } while (i >= 0) { int a = carry + A[i]; carry = a / 10; ans.push_back(a % 10); i--; } if (carry) { ans.push_back(carry); } reverse(ans.begin(), ans.end()); return ans; } }; #endif //CPP_0989__SOLUTION1_H_
ooooo-youwillsee/leetcode
0701-Insert-into-a-Binary-Search-Tree/cpp_0701/TreeNode.h
<reponame>ooooo-youwillsee/leetcode<filename>0701-Insert-into-a-Binary-Search-Tree/cpp_0701/TreeNode.h /** * @author ooooo * @date 2020/9/30 09:16 */ #ifndef CPP_0701__TREENODE_H_ #define CPP_0701__TREENODE_H_ #include <iostream> using namespace std; 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) {} }; #endif //CPP_0701__TREENODE_H_
ooooo-youwillsee/leetcode
0053-Maximum-Subarray/cpp_0053/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/2/4. // #ifndef CPP_0053__SOLUTION1_H_ #define CPP_0053__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] 表示数组长度为i的最大连续数组 * dp[i] = nums[i] (dp[i-1] < 0) * dp[i] = dp[i-1] + nums[i] (dp[i-1] >= 0) 前一个大于0,有增益效果,所有加上 * */ class Solution { public: int maxSubArray(vector<int> &nums) { int ans = nums[0]; vector<int> dp(nums.size(), 0); dp[0] = nums[0]; for (int i = 1; i < nums.size(); ++i) { if (dp[i - 1] < 0) { dp[i] = nums[i]; } else { dp[i] = dp[i - 1] + nums[i]; } ans = max(ans, dp[i]); } return ans; } int maxSubArray2(vector<int> &nums) { int ans = nums[0], sum = 0; for (auto &num: nums) { if (sum > 0) { sum += num; } else { sum = num; } ans = max(sum, ans); } return ans; } }; #endif //CPP_0053__SOLUTION1_H_
ooooo-youwillsee/leetcode
0649-Dota2 Senate/cpp_0649/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/12/11 09:11 */ #ifndef CPP_0649__SOLUTION1_H_ #define CPP_0649__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; class Solution { public: string predictPartyVictory(string senate) { queue<int> R, D; int n = senate.size(); for (int i = 0; i < n; ++i) { if (senate[i] == 'R') { R.push(i); } else { D.push(i); } } while (!R.empty() && !D.empty()) { if (R.front() < D.front()) { R.push(R.front() + n); } else { D.push(D.front() + n); } D.pop(); R.pop(); } return D.empty() ? "Radiant" : "Dire"; } }; #endif //CPP_0649__SOLUTION1_H_
ooooo-youwillsee/leetcode
1642-Furthest Building You Can Reach/cpp_1642/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/6 16:09 */ #ifndef CPP_1642__SOLUTION1_H_ #define CPP_1642__SOLUTION1_H_ #include <iostream> #include <queue> #include <vector> using namespace std; // 堆 class Solution { public: int furthestBuilding(vector<int> &heights, int bricks, int ladders) { int n = heights.size(); int i = 0; priority_queue<int, vector<int>, greater<int>> pq; int sum = 0; for (; i < n - 1; ++i) { int diff = heights[i + 1] - heights[i]; if (diff > 0) { // 直接用梯子 pq.push(diff); if (pq.size() > ladders) { // 改用砖块 sum += pq.top(); pq.pop(); } if (sum > bricks) { return i; } } } return i; } }; #endif //CPP_1642__SOLUTION1_H_
ooooo-youwillsee/leetcode
0024-Swap-Nodes-in-Pairs/cpp_0024/ListNode.h
// // Created by ooooo on 2019/10/30. // #ifndef CPP_0024_LISTNODE_H #define CPP_0024_LISTNODE_H #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; #endif //CPP_0024_LISTNODE_H
ooooo-youwillsee/leetcode
0303-Range-Sum-Query-Immutable/cpp_0303/Solution2.h
// // Created by ooooo on 2020/1/29. // #ifndef CPP_0303__SOLUTION2_H_ #define CPP_0303__SOLUTION2_H_ #include <iostream> #include <vector> /** * 线段树 */ using namespace std; class NumArray { private: int *tree; int *data; int size; public: NumArray(vector<int> &nums) { data = new int[nums.size()]; for (int i = 0; i < nums.size(); ++i) { data[i] = nums[i]; } size = nums.size(); tree = new int[nums.size() * 4]; buildSegmentTree(0, 0, size - 1); } void buildSegmentTree(int treeIndex, int left, int right) { if (left == right) { tree[treeIndex] = data[left]; return; } int mid = left + (right - left) / 2; buildSegmentTree(leftChild(treeIndex), left, mid); buildSegmentTree(rightChild(treeIndex), mid + 1, right); tree[treeIndex] = merge(tree[leftChild(treeIndex)], tree[rightChild(treeIndex)]); } int merge(int a, int b) { return a + b; } int leftChild(int i) { return 2 * i + 1; } int rightChild(int i) { return 2 * i + 2; } int sumRange(int i, int j) { return sumRange(0, 0, size - 1, i, j); } int sumRange(int treeIndex, int left, int right, int queryLeft, int queryRight) { if (queryLeft == left && queryRight == right) { return tree[treeIndex]; } int mid = left + (right - left) / 2; if (queryLeft > mid) { return sumRange(rightChild(treeIndex), mid + 1, right, queryLeft, queryRight); } else if (queryRight <= mid) { return sumRange(leftChild(treeIndex), left, mid, queryLeft, queryRight); } int leftValue = sumRange(leftChild(treeIndex), left, mid, queryLeft, mid); int rightValue = sumRange(rightChild(treeIndex), mid + 1, right, mid + 1, queryRight); return merge(leftValue, rightValue); } }; #endif //CPP_0303__SOLUTION2_H_
ooooo-youwillsee/leetcode
1573-Number of Ways to Split a String/cpp_1573/Solution1.h
/** * @author ooooo * @date 2021/3/3 17:21 */ #ifndef CPP_1573__SOLUTION1_H_ #define CPP_1573__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: static constexpr int MOD = 1000000007; int numWays(string s) { long long n = s.size(); int cnt = 0; for (auto c : s) { if (c == '1') { cnt++; } } if (cnt % 3 != 0) { return 0; } if (cnt == 0) { return ((n - 2) * (n - 1) / 2) % MOD; } int l1 = 0, l2 = 0; int curCnt = 0; while (curCnt < cnt / 3) { if (s[l1] == '1') { curCnt++; } l1++; } l2 = l1; while (s[l2] != '1') l2++; int r1 = n - 1, r2 = n - 1; curCnt = 0; while (curCnt < cnt / 3) { if (s[r2] == '1') { curCnt++; } r2--; } r1 = r2; while (s[r1] != '1') r1--; return (long long) (r2 - r1 + 1) * (long long) (l2 - l1 + 1) % MOD; } }; #endif //CPP_1573__SOLUTION1_H_
ooooo-youwillsee/leetcode
0872-Leaf-Similar-Trees/cpp_0872/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0872_SOLUTION1_H #define CPP_0872_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { public: void dfs(TreeNode *node, vector<int> &vec) { if (!node) return; dfs(node->left, vec); if (!node->left && !node->right) { vec.push_back(node->val); } dfs(node->right, vec); } bool leafSimilar(TreeNode *root1, TreeNode *root2) { if (!root1 && !root2) return true; vector<int> vec1, vec2; dfs(root1, vec1); dfs(root2, vec2); return vec1 == vec2; } }; #endif //CPP_0872_SOLUTION1_H
ooooo-youwillsee/leetcode
lcof_050/cpp_050/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/7. // #ifndef CPP_050__SOLUTION1_H_ #define CPP_050__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: char firstUniqChar(string s) { unordered_map<char, int> m; for (auto &c: s) { m[c]++; } for (auto &c:s) { if (m[c] > 1) continue; return c; } return ' '; } }; #endif //CPP_050__SOLUTION1_H_
ooooo-youwillsee/leetcode
1870-Minimum Speed to Arrive on Time/cpp_1870/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/25 22:22 */ #ifndef CPP_1870__SOLUTION1_H_ #define CPP_1870__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int minSpeedOnTime(vector<int> &dist, double hour) { if (dist.size() - 1 >= hour) return -1; int l = 1, r = *max_element(dist.begin(), dist.end()) * 100; int ans = 0; while (l <= r) { int mid = l + (r - l) / 2; double sum = 0; for (int i = 0; i < dist.size() - 1; i++) { int d = dist[i]; int v = d % mid == 0 ? d / mid : d / mid + 1; sum += v; } sum += dist[dist.size() - 1] * 1.0 / mid; if (sum <= hour) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; }; #endif //CPP_1870__SOLUTION1_H_