repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
lcof_038/cpp_038/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/24. // #ifndef CPP_038__SOLUTION2_H_ #define CPP_038__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: void dfs(int i) { if (i == s.size()) ans.push_back(s); for (...
ooooo-youwillsee/leetcode
0091-Decode Ways/cpp_0091/Solution1.h
<filename>0091-Decode Ways/cpp_0091/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/4/21 14:03 */ #ifndef CPP_0091__SOLUTION1_H_ #define CPP_0091__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int dfs(string &s, int i) {...
ooooo-youwillsee/leetcode
1025-Divisor-Game/cpp_1025/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/6. // #ifndef CPP_1025__SOLUTION1_H_ #define CPP_1025__SOLUTION1_H_ #include <iostream> using namespace std; /** * 归纳法 */ class Solution { public: bool divisorGame(int N) { return N % 2 == 0; } }; #endif //CPP_1025__SOLUTION1_H_
ooooo-youwillsee/leetcode
1579-Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable/cpp_1579/Solution2.h
/** * @author ooooo * @date 2021/1/27 14:16 */ #ifndef CPP_1579__SOLUTION2_H_ #define CPP_1579__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: class UF { public: int n; vector<int> p; UF(int n) : n(n), p(n) { for (int i = 0; i < n; ++i) { p[i]...
ooooo-youwillsee/leetcode
1189-Maximum-Number-of-Balloons/cpp_1189/Solution1.h
<filename>1189-Maximum-Number-of-Balloons/cpp_1189/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/15. // #ifndef CPP_1189__SOLUTION1_H_ #define CPP_1189__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: int maxNumberOfBalloons(string text) { ...
ooooo-youwillsee/leetcode
lcof_062/cpp_062/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/22. // #ifndef CPP_062__SOLUTION1_H_ #define CPP_062__SOLUTION1_H_ #include <iostream> #include <list> using namespace std; /** * timeout */ class Solution { public: int lastRemaining(int n, int m) { list<int> l; for (int i = 0; i < n; ++i) { l.pus...
ooooo-youwillsee/leetcode
0052-N-Queens-II/cpp_0052/Solution2.h
// // Created by ooooo on 2019/11/14. // #ifndef CPP_0052_SOLUTION2_H #define CPP_0052_SOLUTION2_H #include <iostream> #include <vector> using namespace std; class Solution { private: void help(int n, int row, int &res, vector<bool> &cols, vector<bool> &pies, vector<bool> &nas) { if (row ...
ooooo-youwillsee/leetcode
0378-Kth-Smallest-Element-in-a-Sorted-Matrix/cpp_0378/Solution1.h
/** * @author ooooo * @date 2020/9/29 16:44 */ #ifndef CPP_0378__SOLUTION1_H_ #define CPP_0378__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int kthSmallest(vector<vector<int>> &matrix, int k) { int row = matrix.size(), col = matrix[0].size(); vector<int...
ooooo-youwillsee/leetcode
0376-Wiggle Subsequence/cpp_0376/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/12/12 13:53 */ #ifndef CPP_0376__SOLUTION2_H_ #define CPP_0376__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int wiggleMaxLength(vector<int> &nums) { int n = nums.size(); if (n <= 1) return n; vector<in...
ooooo-youwillsee/leetcode
0152-Maximum-Product-Subarray/cpp_0152/Solution1.h
// // Created by ooooo on 2019/12/19. // #ifndef CPP_0152_SOLUTION1_H #define CPP_0152_SOLUTION1_H #include <iostream> #include <climits> #include <vector> using namespace std; class Solution { public: int maxProduct(vector<int> &nums) { int res = INT_MIN; int iMax = 1; int iMin = 1; ...
ooooo-youwillsee/leetcode
lcof_056-2/cpp_056-2/Solution1.h
// // Created by ooooo on 2020/4/17. // #ifndef CPP_056_2__SOLUTION1_H_ #define CPP_056_2__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * hash */ class Solution { public: int singleNumber(vector<int> &nums) { unordered_map<int, int> m; for (auto &n...
ooooo-youwillsee/leetcode
0153-Find-Minimum-in-Rotated-Sorted-Array/cpp_0153/Solution1.h
<filename>0153-Find-Minimum-in-Rotated-Sorted-Array/cpp_0153/Solution1.h /** * @author ooooo * @date 2020/9/25 23:54 */ #ifndef CPP_0153__SOLUTION1_H_ #define CPP_0153__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findMin(vector<int> &nums) { return *m...
ooooo-youwillsee/leetcode
0682-Baseball-Game/cpp_0682/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0682_SOLUTION1_H #define CPP_0682_SOLUTION1_H #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: int calPoints(vector<string> &ops) { stack<int> s; for (auto item: ops) { ...
ooooo-youwillsee/leetcode
lcof_057/cpp_057/Solution1.h
// // Created by ooooo on 2020/4/15. // #ifndef CPP_057__SOLUTION1_H_ #define CPP_057__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> twoSum(vector<int> &nums, int target) { auto it = lower_bound(nums.begin(), nums.end(), target); auto l = nums...
ooooo-youwillsee/leetcode
1189-Maximum-Number-of-Balloons/cpp_1189/Solution2.h
// // Created by ooooo on 2020/1/15. // #ifndef CPP_1189__SOLUTION2_H_ #define CPP_1189__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: // balloon int maxNumberOfBalloons(string text) { unordered_map<char, int> m = { {'b', 0}...
ooooo-youwillsee/leetcode
0783-Minimum-Distance-Between-BST-Nodes/cpp_0783/Solution2.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_0783_SOLUTION2_H #define CPP_0783_SOLUTION2_H #include "TreeNode.h" #include <climits> class Solution { public: TreeNode *prev = nullptr; int min = INT_MAX; void dfs(TreeNode *node) { if (!node) return; dfs(node->left); if (prev)...
ooooo-youwillsee/leetcode
lcof_040/cpp_040/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/3/25. // #ifndef CPP_040__SOLUTION1_H_ #define CPP_040__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; /** * 堆 */ class Solution { public: vector<int> getLeastNumbers(vector<int> &arr, in...
ooooo-youwillsee/leetcode
0520-Detect-Capital/cpp_0520/Solution1.h
// // Created by ooooo on 2020/1/26. // #ifndef CPP_0520__SOLUTION1_H_ #define CPP_0520__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: bool detectCapitalUse(string word) { int i = 0; bool flag = false; for (int j = 0; j < word.size(); ++j) { if (isupper(word[j]))...
ooooo-youwillsee/leetcode
0179-Largest Number/cpp_0179/Solution1.h
/** * @author ooooo * @date 2021/4/12 17:16 */ #ifndef CPP_0179__SOLUTION1_H_ #define CPP_0179__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string largestNumber(vector<int> &nums) { int n = nums.size(); vector<string> s; for (int i = 0; i < n; i++) {...
ooooo-youwillsee/leetcode
lcof_055-1/cpp_055-1/Solution3.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/11. // #ifndef CPP_055_1__SOLUTION3_H_ #define CPP_055_1__SOLUTION3_H_ #include "TreeNode.h" #include <queue> /** * dfs */ class Solution { public: int maxDepth(TreeNode *root) { if (!root) return 0; return max(maxDepth(root->left), maxDepth(root->right)...
ooooo-youwillsee/leetcode
lcof_061/cpp_061/Solution1.h
// // Created by ooooo on 2020/4/21. // #ifndef CPP_061__SOLUTION1_H_ #define CPP_061__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool isStraight(vector<int> &nums) { sort(nums.begin(), nums.end()); int count_0 = 0, len = nums.size(); for (int i = ...
ooooo-youwillsee/leetcode
1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp_1290/Solution1.h
// // Created by ooooo on 2020/1/23. // #ifndef CPP_1290__SOLUTION1_H_ #define CPP_1290__SOLUTION1_H_ #include "ListNode.h" class Solution { public: int getDecimalValue(ListNode *head) { string s = ""; while (head) { s += to_string(head->val); head = head->next; } return stoi(s, 0, 2); ...
ooooo-youwillsee/leetcode
0234-Palindrome-Linked-List/cpp_0234/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/8. // #ifndef CPP_0234_SOLUTION1_H #define CPP_0234_SOLUTION1_H #include "ListNode.h" #include <vector> /** * 循环 */ class Solution { public: bool isPalindrome(ListNode *head) { vector<int> ans; while (head) { ans.push_back(head->val);...
ooooo-youwillsee/leetcode
lcof_053-1/cpp_053-1/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/9. // #ifndef CPP_053_1__SOLUTION2_H_ #define CPP_053_1__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int low_bound(vector<int> &nums, int target) { int l = 0, r = nums.size(); while (l < r) { int ...
ooooo-youwillsee/leetcode
0459-Repeated-Substring-Pattern/cpp_0459/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/26. // #ifndef CPP_0459__SOLUTION1_H_ #define CPP_0459__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: bool repeatedSubstringPattern(string s) { //for (int i = 1; i <= s.size() / 2; ++i) { // 从高向低 for ...
ooooo-youwillsee/leetcode
0874-Walking-Robot-Simulation/cpp_0874/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/30 14:19 */ #ifndef CPP_0874__SOLUTION1_H_ #define CPP_0874__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: vector<vector<int>> dx_dy = {{1, 0}, {0, 1}, {-1, 0}, {...
ooooo-youwillsee/leetcode
lcof_057-2/cpp_057-2/Solution1.h
// // Created by ooooo on 2020/4/16. // #ifndef CPP_057_2__SOLUTION1_H_ #define CPP_057_2__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> group(int l, int r) { vector<int> ans; for (; l < r; l++) { if (l != 0) ans.push_back(l); } ...
ooooo-youwillsee/leetcode
1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp_1577/Solution1.h
<filename>1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp_1577/Solution1.h /** * @author ooooo * @date 2020/9/7 15:09 */ #ifndef CPP_1577__SOLUTION1_H_ #define CPP_1577__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * timeout */ class Solution1 {...
ooooo-youwillsee/leetcode
0947-Most Stones Removed with Same Row or Column/cpp_0947/Solution1.h
/** * @author ooooo * @date 2021/1/15 18:53 */ #ifndef CPP_0947__SOLUTION1_H_ #define CPP_0947__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: int ans = 0; void bfs(int rootIndex, unordered_...
ooooo-youwillsee/leetcode
0842-Split Array into Fibonacci Sequence/cpp_0842/Solution1.h
/** * @author ooooo * @date 2020/12/8 19:08 */ #ifndef CPP_0842__SOLUTION1_H_ #define CPP_0842__SOLUTION1_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; class Solution { public: long max_v = (int) (pow(2, 31) - 1); bool dfs(vector<int> &nums, string &s, int i) { if (i >= ...
ooooo-youwillsee/leetcode
1784-Check if Binary String Has at Most One Segment of Ones/cpp_1784/Solution1.h
/** * @author ooooo * @date 2021/3/14 15:00 */ #ifndef CPP_1784__SOLUTION1_H_ #define CPP_1784__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: ...
ooooo-youwillsee/leetcode
1021-Remove-Outermost-Parentheses/cpp_1021/Solution1.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_1021_SOLUTION1_H #define CPP_1021_SOLUTION1_H #include <iostream> #include <stack> using namespace std; class Solution { public: string removeOuterParentheses(string S) { stack<char> ss; string ans = "", mid = ""; for (auto c: S) { ...
ooooo-youwillsee/leetcode
0018-4Sum/cpp_0018/Solution3.h
/** * @author ooooo * @date 2020/10/5 10:30 */ #ifndef CPP_0018__SOLUTION3_H_ #define CPP_0018__SOLUTION3_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: struct vec_hash { size_t operator()(vector<int> __v) const { size_t x = 0; for (in...
ooooo-youwillsee/leetcode
0061-Rotate List/cpp_0061/ListNode.h
/** * @author ooooo * @date 2020/10/5 18:03 */ #ifndef CPP_0061__LISTNODE_H_ #define CPP_0061__LISTNODE_H_ #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; #endif //CPP_0061__LISTNODE_H_
ooooo-youwillsee/leetcode
0563-Binary-Tree-Tilt/cpp_0563/Solution1.h
<filename>0563-Binary-Tree-Tilt/cpp_0563/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/3. // #ifndef CPP_0563_SOLUTION1_H #define CPP_0563_SOLUTION1_H #include "TreeNode.h" class Solution { public: int res = 0; int dfs(TreeNode *node) { if (!node) return 0; int leftSum = dfs(n...
ooooo-youwillsee/leetcode
0069-Sqrt-x/cpp_0069/Solution1.h
// // Created by ooooo on 2019/11/21. // #ifndef CPP_0069_SOLUTION1_H #define CPP_0069_SOLUTION1_H class Solution { public: int mySqrt(int x) { if (x == 0 ) return 0; for (int i = 1; i <= x; ++i) { if (i == x / i) return i; if (i > x / i) return i - 1; } retu...
ooooo-youwillsee/leetcode
0142-Linked-List-Cycle-II/cpp_0142/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/17. // #ifndef CPP_0142__SOLUTION1_H_ #define CPP_0142__SOLUTION1_H_ #include "LIstNode.h" #include <unordered_set> /** * 哈希 */ class Solution { public: ListNode *detectCycle(ListNode *head) { unordered_set<ListNode *> set; while(head) { if (set.cou...
ooooo-youwillsee/leetcode
0968-Binary-Tree-Cameras/cpp_0968/Solution1.h
<filename>0968-Binary-Tree-Cameras/cpp_0968/Solution1.h /** * @author ooooo * @date 2020/9/22 11:02 */ #ifndef CPP_0968__SOLUTION1_H_ #define CPP_0968__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: // 0:已被覆盖 // 1:需要安装 // 2: 已安装 int dfs(TreeNode *node) { if (!node) return 0; int l = dfs(no...
ooooo-youwillsee/leetcode
lcof_025/cpp_025/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/16. // #ifndef CPP_025__SOLUTION1_H_ #define CPP_025__SOLUTION1_H_ #include "ListNode.h" class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (!l1) return l2; if (!l2) return l1; ListNode *dummyHead = new ...
ooooo-youwillsee/leetcode
0012-Integer-to-Romanl/cpp_0012/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/25 13:43 */ #ifndef CPP_0012__SOLUTION1_H_ #define CPP_0012__SOLUTION1_H_ #include <iostream> #include <unordered_map> #include <vector> using namespace std; class Solution { public: string intToRoman(int num) { vector<string> vec; i...
ooooo-youwillsee/leetcode
lcof_047/cpp_047/Solution2.h
// // Created by ooooo on 2020/4/3. // #ifndef CPP_047__SOLUTION2_H_ #define CPP_047__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i][j] = max( dp[i][j-1], dp[i-1][j] ) + grid[i][j] */ class Solution { public: int maxValue(vector<vector<int>> &grid) { if (grid.empty()) r...
ooooo-youwillsee/leetcode
1716-Calculate Money in Leetcode Bank/cpp_1716/Solution1.h
/** * @author ooooo * @date 2021/1/20 12:14 */ #ifndef CPP_1716__SOLUTION1_H_ #define CPP_1716__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int totalMoney(int n) { int prev = 0, count = 1; int ans = 0; while (n) { ans += count + prev; count++; ...
ooooo-youwillsee/leetcode
1052-Grumpy Bookstore Owner/cpp_1052/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2021/2/23 13:40 */ #ifndef CPP_1052__SOLUTION1_H_ #define CPP_1052__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxSatisfied(vector<int> &customers, vector<int> &grumpy, int...
ooooo-youwillsee/leetcode
lcp-028/cpp_028/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/4/10 20:28 */ #ifndef CPP_028__SOLUTION2_H_ #define CPP_028__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; int purchasePlans(vector<int> nums, int target) { sort(nums.begin(), nums.end()); int n = nums.size(); int mod = 1e9 + 7; lon...
ooooo-youwillsee/leetcode
0144-Binary-Tree-Preorder-Traversal/cpp_0144/Solution1.h
/** * @author ooooo * @date 2020/9/25 23:15 */ #ifndef CPP_0144__SOLUTION1_H_ #define CPP_0144__SOLUTION1_H_ #include "TreeNode.h" /** * color mark */ class Solution { public: vector<int> preorderTraversal(TreeNode *root) { vector<int> ans; if (!root) return ans; stack<pair<TreeNode *, bool>> s; s.pu...
ooooo-youwillsee/leetcode
0589-N-ary-Tree-Preorder-Traversal/cpp_0589/Node.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0589_NODE_H #define CPP_0589_NODE_H #include <iostream> #include <vector> using namespace std; class Node { public: int val; vector<Node *> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node *> _children)...
ooooo-youwillsee/leetcode
1779-Find Nearest Point That Has the Same X or Y Coordinate/cpp_1779/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>1779-Find Nearest Point That Has the Same X or Y Coordinate/cpp_1779/Solution1.h /** * @author ooooo * @date 2021/3/14 14:53 */ #ifndef CPP_1779__SOLUTION1_H_ #define CPP_1779__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unord...
ooooo-youwillsee/leetcode
0403-Frog Jump/cpp_0403/Solution2.h
<reponame>ooooo-youwillsee/leetcode<filename>0403-Frog Jump/cpp_0403/Solution2.h /** * @author ooooo * @date 2021/5/1 20:05 */ #ifndef CPP_0403__SOLUTION2_H_ #define CPP_0403__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool canCross(vector<int> &stones) { ...
ooooo-youwillsee/leetcode
lcof_056-2/cpp_056-2/Solution2.h
// // Created by ooooo on 2020/4/17. // #ifndef CPP_056_2__SOLUTION2_H_ #define CPP_056_2__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int singleNumber(vector<int> &nums) { vector<int> bits(32, 0); for (auto &num: nums) { unsigned int bit_1 = 1; ...
ooooo-youwillsee/leetcode
1014-Best-Sightseeing-Pair/cpp_1014/Solution1.h
<filename>1014-Best-Sightseeing-Pair/cpp_1014/Solution1.h /** * @author ooooo * @date 2021/2/11 14:39 */ #ifndef CPP_1014__SOLUTION1_H_ #define CPP_1014__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxScoreSightseeingPair(const vector<int> &A) { int ...
ooooo-youwillsee/leetcode
1658-Minimum Operations to Reduce X to Zero/cpp_1658/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/1 17:21 */ #ifndef CPP_1658__SOLUTION1_H_ #define CPP_1658__SOLUTION1_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; class Solution { public: int minOperations(vector<int> &nums, int x) { int target = accumulate(nums.begi...
ooooo-youwillsee/leetcode
1546-Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/cpp_1546/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/9 17:55 */ #ifndef CPP_1546__SOLUTION2_H_ #define CPP_1546__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int maxNonOverlapping(vector<int> &nums, int target) { ...
ooooo-youwillsee/leetcode
0669-Trim-a-Binary-Search-Tree/cpp_0669/Solution2.h
<filename>0669-Trim-a-Binary-Search-Tree/cpp_0669/Solution2.h // // Created by ooooo on 2020/1/3. // #ifndef CPP_0669_SOLUTION2_H #define CPP_0669_SOLUTION2_H #include "TreeNode.h" class Solution { public: TreeNode *trimBST(TreeNode *root, int L, int R) { if (!root) return nullptr; if (root->val...
ooooo-youwillsee/leetcode
1802-Maximum Value at a Given Index in a Bounded Array/cpp_1802/Solution1.h
/** * @author ooooo * @date 2021/3/28 12:19 */ #ifndef CPP_1802__SOLUTION1_H_ #define CPP_1802__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...
ooooo-youwillsee/leetcode
lcof_059-1/cpp_059-1/Solution1.h
// // Created by ooooo on 2020/4/19. // #ifndef CPP_059_1__SOLUTION1_H_ #define CPP_059_1__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> maxSlidingWindow(vector<int> &nums, int k) { if (k == 0 && nums.empty()) return {}; vector<int> ans; ...
ooooo-youwillsee/leetcode
1024-Video Stitching/cpp_1024/Solution2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2020/10/24 11:55 */ #ifndef CPP_1024__SOLUTION2_H_ #define CPP_1024__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] 表示0到i区间的最小数目 */ class Solution { public: int videoStitching(vector<vector<i...
ooooo-youwillsee/leetcode
1706-Where Will the Ball Fall/cpp_1706/Solution1.h
/** * @author ooooo * @date 2021/1/24 17:15 */ #ifndef CPP_1706__SOLUTION1_H_ #define CPP_1706__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <numeric> using namespace std; class Solution { public: vector<int> f...
ooooo-youwillsee/leetcode
1047-Remove-All-Adjacent-Duplicates-In-String/cpp_1047/Solution1.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_1047_SOLUTION1_H #define CPP_1047_SOLUTION1_H #include <iostream> #include <stack> using namespace std; class Solution { public: string removeDuplicates(string S) { stack<char> ss; for (auto c:S) { if (!ss.empty() && c == ss.top()) { ...
ooooo-youwillsee/leetcode
0538-Convert-BST-to_Greater-Tree/cpp_0538/Solution3.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/9/21 13:16 */ #ifndef CPP_0538__SOLUTION3_H_ #define CPP_0538__SOLUTION3_H_ #include "TreeNode.h" class Solution { public: TreeNode *prev; TreeNode *convertBST(TreeNode *root) { if (!root) return root; convertBST(root->right); if (!prev) { prev = ro...
ooooo-youwillsee/leetcode
0316-Remove Duplicate Letters/cpp_0316/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0316-Remove Duplicate Letters/cpp_0316/Solution1.h /** * @author ooooo * @date 2020/12/20 12:05 */ #ifndef CPP_0316__SOLUTION1_H_ #define CPP_0316__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> #include <unordered_map> #include <unordered_set> us...
ooooo-youwillsee/leetcode
0264-Ugly Number II/cpp_0264/Solution1.h
/** * @author ooooo * @date 2021/4/11 15:12 */ #ifndef CPP_0264__SOLUTION1_H_ #define CPP_0264__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> #include <unordered_set> using namespace std; class Solution { public: vector<int> nums = {2, 3, 5}; using ll = long long; int nthUglyNumber(...
ooooo-youwillsee/leetcode
0083-Remove-Duplicates-from-Sorted-List/cpp_0083/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0083-Remove-Duplicates-from-Sorted-List/cpp_0083/Solution1.h // // Created by ooooo on 2020/1/23. // #ifndef CPP_0083__SOLUTION1_H_ #define CPP_0083__SOLUTION1_H_ #include "ListNode.h" /** * loop */ class Solution { public: ListNode *deleteDuplicates(ListNode *head) {...
ooooo-youwillsee/leetcode
1781-Sum of Beauty of All Substrings/cpp_1781/Solution1.h
/** * @author ooooo * @date 2021/3/14 14:57 */ #ifndef CPP_1781__SOLUTION1_H_ #define CPP_1781__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: ...
ooooo-youwillsee/leetcode
0108-Convert-Sorted-Array-to-Binary-Search-Tree/cpp_0108/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/2. // #ifndef CPP_0108_SOLUTION1_H #define CPP_0108_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { public: // 复制vector,时空复杂度变低 TreeNode *sortedArrayToBST2(vector<int> &nums) { if (nums.empty()) return nullptr;...
ooooo-youwillsee/leetcode
0059-Spiral Matrix II/cpp_0059/Solution1.h
/** * @author ooooo * @date 2020/10/5 17:51 */ #ifndef CPP_0059__SOLUTION1_H_ #define CPP_0059__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> generateMatrix(int n) { int cur = 1, max = n * n; vector<vector<int>> ans(n, vector<int>(n, 0...
ooooo-youwillsee/leetcode
0087-Scramble String/cpp_0087/Solution1.h
/** * @author ooooo * @date 2021/5/7 20:08 */ #ifndef CPP_0087__SOLUTION1_H_ #define CPP_0087__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool dfs(string &s1, string &s2, int i, int j, int len) { if (memo[i][j][len] != 0) return memo[i][j][len] == 1; ...
ooooo-youwillsee/leetcode
lcof_010-1/cpp_010-1/Solution2.h
<filename>lcof_010-1/cpp_010-1/Solution2.h // // Created by ooooo on 2020/3/8. // #ifndef CPP_010_1__SOLUTION2_H_ #define CPP_010_1__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * F(N) = F(N - 1) + F(N - 2) * * recursion + memo * */ class Solution { publi...
ooooo-youwillsee/leetcode
0404-Sum-of-Left-Leaves/cpp_0404/Solution1.h
// // Created by ooooo on 2020/1/2. // #ifndef CPP_0404_SOLUTION1_H #define CPP_0404_SOLUTION1_H #include "TreeNode.h" class Solution { public: int help(TreeNode *node, bool isLeft) { if (!node) return 0; int x = isLeft && !node->left && !node->right ? node->val : 0; return help(node->lef...
ooooo-youwillsee/leetcode
0897-Increasing-Order-Search-Tree/cpp_0897/Solution1.h
<filename>0897-Increasing-Order-Search-Tree/cpp_0897/Solution1.h // // Created by ooooo on 2020/1/5. // #ifndef CPP_0897_SOLUTION1_H #define CPP_0897_SOLUTION1_H #include "TreeNode.h" class Solution { public: TreeNode *prev = nullptr; void inOrder(TreeNode *node) { if (!node) return; inOrder...
ooooo-youwillsee/leetcode
0225-Implement-Stack-using-Queues/cpp_0225/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2019/10/30. // #ifndef CPP_0225_SOLUTION1_H #define CPP_0225_SOLUTION1_H #include <iostream> #include <queue> using namespace std; class MyStack { private: queue<int> q; public: /** Initialize your data structure here. */ MyStack() { } /** Push ele...
ooooo-youwillsee/leetcode
1668-Maximum Repeating Substring/cpp_1668/Solution1.h
<filename>1668-Maximum Repeating Substring/cpp_1668/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/1/24 17:52 */ #ifndef CPP_1668__SOLUTION1_H_ #define CPP_1668__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack> usi...
ooooo-youwillsee/leetcode
0139-Word-Break/cpp_0139/Solution3.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/17. // #ifndef CPP_0139__SOLUTION3_H_ #define CPP_0139__SOLUTION3_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * 记忆化 记录索引 i 的位置 */ class Solution { public: bool wordBreak(string s, vector<string> &wordDict) { un...
ooooo-youwillsee/leetcode
0038-Count-and-Say/cpp_0038/Solution1.h
// // Created by ooooo on 2020/1/24. // #ifndef CPP_0038__SOLUTION1_H_ #define CPP_0038__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: string help(string s) { int count = 1; string ans = ""; for (int i = 0; i < s.size(); ++i) { if (i == s.size() - 1 || s[i] != s...
ooooo-youwillsee/leetcode
0532-K-diff-Pairs-in-an-Array/cpp_0532/Solution4.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0532_SOLUTION4_H #define CPP_0532_SOLUTION4_H #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> using namespace std; /** * */ class Solution { public: int findPairs(vector<int> &nums, int k) { if (nums.empty() || ...
ooooo-youwillsee/leetcode
0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution3.h
// // Created by ooooo on 2020/2/15. // #ifndef CPP_0094__SOLUTION3_H_ #define CPP_0094__SOLUTION3_H_ #include "TreeNode.h" #include <stack> /** * iteration */ class Solution { public: vector<int> inorderTraversal(TreeNode *root) { vector<int> ans; stack<TreeNode *> stack; TreeNode *curNode = root; ...
ooooo-youwillsee/leetcode
0803-Bricks Falling When Hit/cpp_0803/Solution1.h
<filename>0803-Bricks Falling When Hit/cpp_0803/Solution1.h /** * @author ooooo * @date 2021/1/16 18:28 */ #ifndef CPP_0803__SOLUTION1_H_ #define CPP_0803__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <unordered_set> using namespace std; class Solution { ...
ooooo-youwillsee/leetcode
1143-Longest Common Subsequence/cpp_1143/Solution1.h
/** * @author ooooo * @date 2021/4/3 12:12 */ #ifndef CPP_1143__SOLUTION1_H_ #define CPP_1143__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int longestCommonSubsequence(string s1, string s2) { int m = s1.size(), n = s2.size(); vector<vector<int>> dp(m + ...
ooooo-youwillsee/leetcode
0518-Coin Change 2/cpp_0518/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2021/2/17 19:21 */ #ifndef CPP_0518__SOLUTION1_H_ #define CPP_0518__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int change(int amount, vector<int> &coins) { int n = coins.size...
ooooo-youwillsee/leetcode
0287-Find-the-Duplicate-Number/cpp_0287/Solution3.h
// // Created by ooooo on 2020/2/23. // #ifndef CPP_0287__SOLUTION3_H_ #define CPP_0287__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * binary search * 对区间[l, r]中查找 l <= X <= mid 的个数count, * 如果 count > mid - l + 1; 就说明重复的数字在 [l, mid] * 反之,重复的数字在 [mid+1, r] */ class Solution { p...
ooooo-youwillsee/leetcode
0521-Longest-Uncommon-Subsequence-I/cpp_0521/Solution1.h
// // Created by ooooo on 2020/1/26. // #ifndef CPP_0521__SOLUTION1_H_ #define CPP_0521__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: int findLUSlength(string a, string b) { if (a.size() > b.size()) return a.size(); else if (a.size() < b.size()) return b.size(); else ...
ooooo-youwillsee/leetcode
0547-Number of Provinces/cpp_0547/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/7 17:08 */ #ifndef CPP_0547__SOLUTION1_H_ #define CPP_0547__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findCircleNum(vector<vector<int>> &isConnected) { int n = isConnected.size(); vector<int> p(...
ooooo-youwillsee/leetcode
lcof_036/cpp_036/Node.h
<filename>lcof_036/cpp_036/Node.h // // Created by ooooo on 2020/3/23. // #ifndef CPP_036__NODE_H_ #define CPP_036__NODE_H_ #include <iostream> using namespace std; class Node { public: int val; Node *left; Node *right; Node() {} Node(int _val) { val = _val; left = NULL; right = NULL; } ...
ooooo-youwillsee/leetcode
0684-Redundant Connection/cpp_0684/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/13 20:10 */ #ifndef CPP_0684__SOLUTION1_H_ #define CPP_0684__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> findRedundantConnection(vector<vector<int>> &edges) { int n =...
ooooo-youwillsee/leetcode
1074-Number of Submatrices That Sum to Target/cpp_1074/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>1074-Number of Submatrices That Sum to Target/cpp_1074/Solution1.h // // Created by ooooo on 5/31/2021. // #ifndef CPP_1074_SOLUTION1_H #define CPP_1074_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { publ...
ooooo-youwillsee/leetcode
0645-Set-Mismatch/cpp_0645/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/13. // #ifndef CPP_0645__SOLUTION2_H_ #define CPP_0645__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> findErrorNums(vector<int> &nums) { unordered_map<int, int> m; for ...
ooooo-youwillsee/leetcode
0027-Remove-Element/cpp_0027/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0027_SOLUTION1_H #define CPP_0027_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: int removeElement(vector<int> &nums, int val) { int i = 0; for (int j = 0; j < nums.size(); ++j) { ...
ooooo-youwillsee/leetcode
0437-Path-Sum-III/cpp_0437/Solution2.h
<filename>0437-Path-Sum-III/cpp_0437/Solution2.h // // Created by ooooo on 2020/1/2. // #ifndef CPP_0437_SOLUTION2_H #define CPP_0437_SOLUTION2_H #include "TreeNode.h" #include <vector> class Solution { public: // prevSums变为数组可以加速,用一个p标记最后的元素位置。 int dfs(TreeNode *node, vector<int> prevSums, int sum) { ...
ooooo-youwillsee/leetcode
0970-Powerful-Integers/cpp_0970/Solution2.h
// // Created by ooooo on 2020/1/17. // #ifndef CPP_0970__SOLUTION2_H_ #define CPP_0970__SOLUTION2_H_ #include <iostream> #include <unordered_set> #include <unordered_map> #include <vector> using namespace std; /** * map缓存,2^20 > 1000000 */ class Solution { public: unordered_map<int, vector<int>> m; int cal...
ooooo-youwillsee/leetcode
0680-Valid-Palindrome-II/cpp_0680/Solution1.h
// // Created by ooooo on 2020/1/28. // #ifndef CPP_0680__SOLUTION1_H_ #define CPP_0680__SOLUTION1_H_ #include <iostream> using namespace std; /** * 超出内存限制 */ class Solution { public: bool isValid(string s, int pos) { for (int left = 0, right = s.size() - 1; left < right; ++left, --right) { if (left...
ooooo-youwillsee/leetcode
0056-Merge-Intervals/cpp_0056/Solution1.h
// // Created by ooooo on 2020/2/12. // #ifndef CPP_0056__SOLUTION1_H_ #define CPP_0056__SOLUTION1_H_ #include <iostream> #include <vector> #include <set> using namespace std; /** * 合并 => [1, n-1] */ class Solution { public: struct Range { int start, end; Range(int start, int an_end) : start(start), en...
ooooo-youwillsee/leetcode
0102-Binary-Tree-Level-Order-Traversal/cpp_0102/Solution1.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0102_SOLUTION1_H #define CPP_0102_SOLUTION1_H #include <iostream> #include "TreeNode.h" #include <queue> #include <vector> using namespace std; class Solution { public: vector<vector<int>> levelOrder(TreeNode *root) { vector<vector<int>> res; if...
ooooo-youwillsee/leetcode
lcof_022/cpp_022/Solution1.h
// // Created by ooooo on 2020/3/15. // #ifndef CPP_022__SOLUTION1_H_ #define CPP_022__SOLUTION1_H_ #include "ListNode.h" #include <vector> class Solution { public: ListNode *getKthFromEnd(ListNode *head, int k) { if (!head) return nullptr; vector<ListNode *> vec; while (head) { vec.push_back(hea...
ooooo-youwillsee/leetcode
lcof_058-2/cpp_058-2/Solution2.h
// // Created by ooooo on 2020/4/19. // #ifndef CPP_058_2__SOLUTION2_H_ #define CPP_058_2__SOLUTION2_H_ #include <iostream> using namespace std; class Solution { public: string reverseLeftWords(string s, int n) { string ans; reverse_copy(s.begin(), s.end(), back_inserter(ans)); reverse(ans.begin(), a...
ooooo-youwillsee/leetcode
1880-Check if Word Equals Summation of Two Words/cpp_1880/Solution1.h
<filename>1880-Check if Word Equals Summation of Two Words/cpp_1880/Solution1.h /** * @author ooooo * @date 2021/5/31 10:15 */ #ifndef CPP_1880__SOLUTION1_H_ #define CPP_1880__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: long long help(string &s) { long l...
ooooo-youwillsee/leetcode
1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/cpp_1365/Solution1.h
/** * @author ooooo * @date 2020/10/26 08:59 */ #ifndef CPP_1365__SOLUTION1_H_ #define CPP_1365__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int> &nums) { vector<int> copy = nums; sort(copy.begin(), copy.end(...
ooooo-youwillsee/leetcode
1679-Max Number of K-Sum Pairs/cpp_1679/Solution1.h
/** * @author ooooo * @date 2021/1/29 20:45 */ #ifndef CPP_1679__SOLUTION1_H_ #define CPP_1679__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxOperations(vector<int> &nums, int k) { sort(nums.begin(), nums.end()); int l = 0, r = nums.size() - 1; i...
ooooo-youwillsee/leetcode
lcci_17_13/cpp_17_13/Solution2.h
/** * @author ooooo * @date 2020/10/10 19:30 */ #ifndef CPP_17_13__SOLUTION2_H_ #define CPP_17_13__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * trie */ class Solution { public: struct Node { vector<Node *> children; bool isWord; Node() { this->children.assign(26, n...
ooooo-youwillsee/leetcode
0973-K Closest Points to Origin/cpp_0973/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/11/9 08:58 */ #ifndef CPP_0973__SOLUTION1_H_ #define CPP_0973__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; /** * heap */ class Solution { public: struct Item { vector<int> point; Item(vecto...
ooooo-youwillsee/leetcode
1663-Smallest String With A Given Numeric Value/cpp_1663/Solution1.h
/** * @author ooooo * @date 2020/12/11 10:29 */ #ifndef CPP_1663__SOLUTION1_H_ #define CPP_1663__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string getSmallestString(int n, int k) { string ans = ""; for (int i = 0; i < n; ++i) { ans += 'a'; } k -...
ooooo-youwillsee/leetcode
0055-Jump-Game/cpp_0055/Solution2.h
<filename>0055-Jump-Game/cpp_0055/Solution2.h // // Created by ooooo on 2020/2/12. // #ifndef CPP_0055__SOLUTION2_H_ #define CPP_0055__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 贪心 */ class Solution { public: bool canJump(vector<int> &nums) { int lastPos = nums.size() - 1;...