repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
0236-Lowest-Common-Ancestor-of-a-Binary-Tree/cpp_0236/Solution1.h
// // Created by ooooo on 2019/11/3. // #ifndef CPP_0236_SOLUTION1_H #define CPP_0236_SOLUTION1_H #include "TreeNode.h" class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { if (!root) return NULL; if (root == p || root == q) return root; TreeN...
ooooo-youwillsee/leetcode
0415-Add-Strings/cpp_0415/Solution1.h
// // Created by ooooo on 2020/1/25. // #ifndef CPP_0415__SOLUTION1_H_ #define CPP_0415__SOLUTION1_H_ #include <iostream> #include <string> using namespace std; class Solution { public: string addStrings(string num1, string num2) { string ans = ""; int c1, c2, more = 0; for (int i = num1.size() - 1, j ...
ooooo-youwillsee/leetcode
1760-Minimum Limit of Balls in a Bag/cpp_1760/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/4/14 20:21 */ #ifndef CPP_1760__SOLUTION1_H_ #define CPP_1760__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int minimumSize(vector<int> &nums, int maxOperations) { int l = 1, r = *max_element(nums.begin(),...
ooooo-youwillsee/leetcode
0437-Path-Sum-III/cpp_0437/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/2. // #ifndef CPP_0437_SOLUTION1_H #define CPP_0437_SOLUTION1_H #include "TreeNode.h" #include <vector> #include <queue> class Solution { public: int pathSum(TreeNode *root, int sum) { if (!root) return 0; queue<TreeNode *> q; ...
ooooo-youwillsee/leetcode
0075-Sort-Colors/cpp_0075/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/14. // #ifndef CPP_0075__SOLUTION2_H_ #define CPP_0075__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 双指针 */ class Solution { public: void sortColors(vector<int> &nums) { if (nums.size() <= 1) return; int i = 0, j = nums....
ooooo-youwillsee/leetcode
0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution4.h
/** * @author ooooo * @date 2020/9/14 18:19 */ #ifndef CPP_0094__SOLUTION4_H_ #define CPP_0094__SOLUTION4_H_ #include "TreeNode.h" #include <stack> /** * color marker */ class Solution { public: vector<int> inorderTraversal(TreeNode *root) { stack<pair<TreeNode *, bool>> s; s.push({root, false}); vect...
ooooo-youwillsee/leetcode
0559-Maximum Depth of N-ary Tree/cpp_0559/Solution1.h
// // Created by ooooo on 2019/12/29. // #ifndef CPP_0599_SOLUTION1_H #define CPP_0599_SOLUTION1_H #include "Node.h" class Solution { private: int max = 0; void dfs(Node *node, int level) { if (!node) return; max = std::max(max, level); for (int i = 0; i < node->children.size(); ++i) { dfs(n...
ooooo-youwillsee/leetcode
1603-Design Parking System/cpp_1603/Solution1.h
<filename>1603-Design Parking System/cpp_1603/Solution1.h /** * @author ooooo * @date 2021/3/19 19:25 */ #ifndef CPP_1603__SOLUTION1_H_ #define CPP_1603__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class ParkingSystem { public: int big; int medium; int small; ParkingSystem(int b...
ooooo-youwillsee/leetcode
0883-Projection-Area-of-3D-Shapes/cpp_0883/Solution1.h
// // Created by ooooo on 2020/1/15. // #ifndef CPP_0883__SOLUTION1_H_ #define CPP_0883__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; /** * 计数 等于一次的单词 */ class Solution { public: vector<string> split(string s) { vector<string> an...
ooooo-youwillsee/leetcode
1725-Number Of Rectangles That Can Form The Largest Square/cpp_1725/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/1/24 17:06 */ #ifndef CPP_1725__SOLUTION1_H_ #define CPP_1725__SOLUTION1_H_ #include <iostream> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <vector> #include <numeric> using namespace std; class...
ooooo-youwillsee/leetcode
0788-Rotated-Digits/cpp_0788/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/30. // #ifndef CPP_0788__SOLUTION2_H_ #define CPP_0788__SOLUTION2_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: bool valid(int v) { bool valid = false; while (v) { int mod = v % 10; ...
ooooo-youwillsee/leetcode
0394-Decode-String/cpp_0394/Solution2.h
<filename>0394-Decode-String/cpp_0394/Solution2.h // // Created by ooooo on 2020/2/27. // #ifndef CPP_0394__SOLUTION2_H_ #define CPP_0394__SOLUTION2_H_ #include <iostream> using namespace std; /** * dfs */ class Solution { public: string multipleStr(string s, int count) { string ans = ""; for (int i = ...
ooooo-youwillsee/leetcode
1734-Decode XORed Permutation/cpp_1734/Solution1.h
/** * @author ooooo * @date 2021/3/22 19:55 */ #ifndef CPP_1734__SOLUTION1_H_ #define CPP_1734__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> decode(vector<int> &encoded) { int n = encoded.size(); int sum = 0; for (int i = 1; i <= n + 1; ++i...
ooooo-youwillsee/leetcode
lcof_013/cpp_013/Solution1.h
// // Created by ooooo on 2020/3/10. // #ifndef CPP_013__SOLUTION1_H_ #define CPP_013__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int calc_num(int i, int j) { int sum = 0; while (i || j) { sum += i % 10 + j % 10; i /= 10; j /= 10; ...
ooooo-youwillsee/leetcode
0078-Subsets/cpp_0078/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/14. // #ifndef CPP_0078__SOLUTION2_H_ #define CPP_0078__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 回溯 */ class Solution { public: void dfs(int i) { ans.push_back(num); for (; i < nums.size(); ++i) { ...
ooooo-youwillsee/leetcode
0112-Path-Sum/cpp_0112/Solution1.h
// // Created by ooooo on 2019/12/29. // #ifndef CPP_0112_SOLUTION1_H #define CPP_0112_SOLUTION1_H #include "TreeNode.h" class Solution { private: bool dfs(TreeNode *node, int currentSum, int sum) { if (!node) return false; currentSum += node->val; if (!node->left && !node->right && curr...
ooooo-youwillsee/leetcode
0022-Generate-Parentheses/cpp_0022/Solution1.h
// // Created by ooooo on 2019/11/5. // #ifndef CPP_0022_SOLUTION1_H #define CPP_0022_SOLUTION1_H #include <vector> #include <iostream> #include <string> using namespace std; class Solution { private: void gen(vector<string> &vec, int left, int right, int n, string result) { if (left == n && right == n...
ooooo-youwillsee/leetcode
0947-Most Stones Removed with Same Row or Column/cpp_0947/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/1/15 19:27 */ #ifndef CPP_0947__SOLUTION2_H_ #define CPP_0947__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int removeStones(vector<vector<int>> &stones) { vecto...
ooooo-youwillsee/leetcode
1649-Create Sorted Array through Instructions/cpp_1649/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/20 18:52 */ #ifndef CPP_1649__SOLUTION2_H_ #define CPP_1649__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; // 树状数组 class Solution { public: struct BIT { vector<int> data, tree; BIT(vector<int> data) : data(data.size()), tree(d...
ooooo-youwillsee/leetcode
0189-Rotate-Array/cpp_0189/Solution2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2019/12/5. // #ifndef CPP_0189_SOLUTION2_H #define CPP_0189_SOLUTION2_H #include <iostream> #include <vector> using namespace std; class Solution { public: void rotate(vector<int> &nums, int k) { if (nums.size() == 0) return;...
ooooo-youwillsee/leetcode
lcof_066/cpp_066/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/25. // #ifndef CPP_066__SOLUTION1_H_ #define CPP_066__SOLUTION1_H_ #include <iostream> #include <numeric> #include <vector> using namespace std; class Solution { public: vector<int> constructArr(vector<int> &a) { vector<int> ans(a.size(), 0); int num = 1; ...
ooooo-youwillsee/leetcode
0690-Employee-Importance/cpp_0690/Solution2.h
<filename>0690-Employee-Importance/cpp_0690/Solution2.h // // Created by ooooo on 2020/1/13. // #ifndef CPP_0690__SOLUTION2_H_ #define CPP_0690__SOLUTION2_H_ #include "Employee.h" #include <unordered_map> /** * dfs */ class Solution { public: int dfs(int id, unordered_map<int, Employee *> &map) { int sum =...
ooooo-youwillsee/leetcode
lcof_030/cpp_030/Solution2.h
// // Created by ooooo on 2020/3/19. // #ifndef CPP_030__SOLUTION2_H_ #define CPP_030__SOLUTION2_H_ #include <iostream> #include <stack> using namespace std; /** * 使用一个辅助栈:最小栈 */ class MinStack { private: stack<int> s, min_s; public: /** initialize your data structure here. */ MinStack() { } void pu...
ooooo-youwillsee/leetcode
1128-Number of Equivalent Domino Pairs/cpp_1128/Solution1.h
/** * @author ooooo * @date 2021/1/26 13:53 */ #ifndef CPP_1128__SOLUTION1_H_ #define CPP_1128__SOLUTION1_H_ #include <iostream> #include <vector> #include <set> using namespace std; class Solution { public: int numEquivDominoPairs(vector<vector<int>> &dominoes) { int ans = 0; for (int i = 0; i < dominoe...
ooooo-youwillsee/leetcode
lcof_055-2/cpp_055-2/Solution1.h
// // Created by ooooo on 2020/4/11. // #ifndef CPP_055_2__SOLUTION1_H_ #define CPP_055_2__SOLUTION1_H_ #include "TreeNode.h" #include <math.h> #include <unordered_map> class Solution { public: int getHeight(TreeNode *node) { if (map.find(node) != map.end()) return map[node]; if (!node) return 0; retu...
ooooo-youwillsee/leetcode
0669-Trim-a-Binary-Search-Tree/cpp_0669/Solution1.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0669_SOLUTION1_H #define CPP_0669_SOLUTION1_H #include "TreeNode.h" #include <vector> /** * leetcode heap-use-after-free ??? */ class Solution { public: TreeNode *minimum(TreeNode *node) { if (!node->left) return node; return minimum(node->le...
ooooo-youwillsee/leetcode
0842-Split Array into Fibonacci Sequence/cpp_0842/Solution2.h
<filename>0842-Split Array into Fibonacci Sequence/cpp_0842/Solution2.h /** * @author ooooo * @date 2020/12/8 20:07 */ #ifndef CPP_0842__SOLUTION2_H_ #define CPP_0842__SOLUTION2_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; class Solution { public: long max_v = (int) (pow(2,...
ooooo-youwillsee/leetcode
0532-K-diff-Pairs-in-an-Array/cpp_0532/Solution2.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0532_SOLUTION2_H #define CPP_0532_SOLUTION2_H #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> using namespace std; /** * k=0, map */ class Solution { public: int findPairs(vector<int> &nums, int k) { if (nums.emp...
ooooo-youwillsee/leetcode
0443-String-Compression/cpp_0443/Solution1.h
// // Created by ooooo on 2020/1/26. // #ifndef CPP_0443__SOLUTION1_H_ #define CPP_0443__SOLUTION1_H_ #include <iostream> #include <vector> #include <map> using namespace std; /** * loop 相邻两项比较 */ class Solution { public: int compress(vector<char> &chars) { int count = 1, ans = 0; for (int i = 0; i < ch...
ooooo-youwillsee/leetcode
1721-Swapping Nodes in a Linked List/cpp_1721/Solution1.h
/** * @author ooooo * @date 2021/1/20 13:13 */ #ifndef CPP_1721__SOLUTION1_H_ #define CPP_1721__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> using namespace std; class Solution { public: struct ListNode { int val; L...
ooooo-youwillsee/leetcode
lcof_016/cpp_016/Solution1.h
// // Created by ooooo on 2020/3/12. // #ifndef CPP_016__SOLUTION1_H_ #define CPP_016__SOLUTION1_H_ #include <iostream> using namespace std; /** * o(n) timeout */ class Solution { public: double myPow(double x, int n) { double ans = 1.0; if (n < 0) return 1.0 / myPow(x, -n); while (n) { ans *=...
ooooo-youwillsee/leetcode
lcof_014/cpp_014-2/Solution1.h
// // Created by ooooo on 2020/3/11. // #ifndef CPP_014_2__SOLUTION1_H_ #define CPP_014_2__SOLUTION1_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; /** * greed, 尽可能的划分为3 和 2 ==> 7 = 2*3 +1 = 3 + 2*2 */ class Solution { public: int cuttingRope(int n) { if (n <= 3) return n - ...
ooooo-youwillsee/leetcode
0845-Longest Mountain in Array/cpp_0845/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/10/25 13:00 */ #ifndef CPP_0845__SOLUTION2_H_ #define CPP_0845__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int longestMountain(vector<int> &A) { if (A.size() < 3)return 0; int m = A.s...
ooooo-youwillsee/leetcode
0058-Length-of-Last-Word/cpp_0058/Solution2.h
// // Created by ooooo on 2020/1/25. // #ifndef CPP_0058__SOLUTION2_H_ #define CPP_0058__SOLUTION2_H_ #include <iostream> using namespace std; /** * 从尾遍历头 */ class Solution { public: int lengthOfLastWord(string s) { int end = s.size() - 1; while (end >= 0 && s[end] == ' ') end--; if (end < 0) return...
ooooo-youwillsee/leetcode
1822-Sign of the Product of an Array/cpp_1822/Solution1.h
/** * @author ooooo * @date 2021/4/16 19:09 */ #ifndef CPP_1822__SOLUTION1_H_ #define CPP_1822__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
0421-Maximum XOR of Two Numbers in an Array/cpp_0421/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/5/16 09:30 */ #ifndef CPP_0421__SOLUTION1_H_ #define CPP_0421__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: struct Node { vector<Node *> children; Node() : children(2) { } }; Node...
ooooo-youwillsee/leetcode
0127-Word-Ladder/cpp_0127/Solution4.h
/** * @author ooooo * @date 2020/11/5 17:13 */ #ifndef CPP_0127__SOLUTION4_H_ #define CPP_0127__SOLUTION4_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> using namespace std; /** * timeout */ class Solution { public: // 返回符合条件的字符串数组,是超时的 vector<s...
ooooo-youwillsee/leetcode
0012-Integer-to-Romanl/cpp_0012/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/5/14 18:17 */ #ifndef CPP_0012__SOLUTION2_H_ #define CPP_0012__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string intToRoman(int num) { int values[] = {1000, 900, 500, 400, 100, 90, 50, ...
ooooo-youwillsee/leetcode
1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp_1290/Solution2.h
// // Created by ooooo on 2020/1/23. // #ifndef CPP_1290__SOLUTION2_H_ #define CPP_1290__SOLUTION2_H_ #include "ListNode.h" /** * 二进制加法 i <<1 | xx */ class Solution { public: int getDecimalValue(ListNode *head) { int ans = 0; while (head) { ans = ans << 1 | head->val; head = head->next; ...
ooooo-youwillsee/leetcode
0050-Pow-x-n/cpp_0050/Solution1.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0050_SOLUTION1_H #define CPP_0050_SOLUTION1_H class Solution { private: double fastPow(double x, long long n) { if (n == 0) return 1.0; if (n & 1) return x * fastPow(x, n - 1); return fastPow(x * x, n / 2); } public: double myPow...
ooooo-youwillsee/leetcode
1649-Create Sorted Array through Instructions/cpp_1649/Solution1.h
<filename>1649-Create Sorted Array through Instructions/cpp_1649/Solution1.h /** * @author ooooo * @date 2021/3/20 12:34 */ #ifndef CPP_1649__SOLUTION1_H_ #define CPP_1649__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; // 线段树 class Solution { public: struct SegTree { vector<int> d...
ooooo-youwillsee/leetcode
0859-Buddy-Strings/cpp_0859/Solution1.h
<filename>0859-Buddy-Strings/cpp_0859/Solution1.h // // Created by ooooo on 2020/1/31. // #ifndef CPP_0859__SOLUTION1_H_ #define CPP_0859__SOLUTION1_H_ #include <iostream> #include <sstream> #include <unordered_set> using namespace std; class Solution { public: string simplify(string s) { stringstream ss; ...
ooooo-youwillsee/leetcode
0406-Queue-Reconstruction-by-Height/cpp_0406/Solution1.h
<filename>0406-Queue-Reconstruction-by-Height/cpp_0406/Solution1.h // // Created by ooooo on 2020/2/29. // #ifndef CPP_0406__SOLUTION1_H_ #define CPP_0406__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> reconstructQueue(vector<vector<int>> &peo...
ooooo-youwillsee/leetcode
0674-Longest Continuous Increasing Subsequence/cpp_0674/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/24 09:19 */ #ifndef CPP_0674__SOLUTION1_H_ #define CPP_0674__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findLengthOfLCIS(vector<int> &nums) { if (nums.empty()) return 0; int ans = 1, cur = 1; fo...
ooooo-youwillsee/leetcode
0213-House Robber II/cpp_0213/Solution1.h
<filename>0213-House Robber II/cpp_0213/Solution1.h /** * @author ooooo * @date 2021/4/15 14:11 */ #ifndef CPP_0213__SOLUTION1_H_ #define CPP_0213__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int rob(vector<int> &nums, int l, int r) { vector<vector<int>>...
ooooo-youwillsee/leetcode
0424-Longest Repeating Character Replacement/cpp_0424/Solution1.h
/** * @author ooooo * @date 2021/2/27 12:18 */ #ifndef CPP_0424__SOLUTION1_H_ #define CPP_0424__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int characterReplacement(string s, int k) { int n = s.size(); int l = 0, r = 0; int maxCnt = 0; int ans = 0; ...
ooooo-youwillsee/leetcode
0058-Length-of-Last-Word/cpp_0058/Solution1.h
// // Created by ooooo on 2020/1/25. // #ifndef CPP_0058__SOLUTION1_H_ #define CPP_0058__SOLUTION1_H_ #include <iostream> using namespace std; /** * 从头遍历尾 */ class Solution { public: int lengthOfLastWord(string s) { s += " "; string ans = ""; int i = 0, j = s.find_first_of(" "); while (j != -1) ...
ooooo-youwillsee/leetcode
0062-Unique-Paths/cpp_0062/Solution2.h
<filename>0062-Unique-Paths/cpp_0062/Solution2.h // // Created by ooooo on 2020/2/13. // #ifndef CPP_0062__SOLUTION2_H_ #define CPP_0062__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * dp[j] = dp[j] + dp[j-1] * 当前行和上一行的 dp */ class Solution { public: // n 行 m 列 int uniquePaths(...
ooooo-youwillsee/leetcode
0238-Product-of-Array-Except-Self/cpp_0238/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/22. // #ifndef CPP_0238__SOLUTION1_H_ #define CPP_0238__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * left 左边的乘积; right 右边的乘积 */ class Solution { public: vector<int> productExceptSelf(vector<int> &nums) { int n = nums.size(), ...
ooooo-youwillsee/leetcode
0381-Insert Delete GetRandom O(1) - Duplicates allowed/cpp_0381/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/10/31 17:20 */ #ifndef CPP_0381__SOLUTION1_H_ #define CPP_0381__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class RandomizedCollection { public: vector<int> nums; // key -> 值, valu...
ooooo-youwillsee/leetcode
lcof_033/cpp_033/Solution2.h
<filename>lcof_033/cpp_033/Solution2.h // // Created by ooooo on 2020/3/21. // #ifndef CPP_033__SOLUTION2_H_ #define CPP_033__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool help(int l, int r) { if (l >= r) return true; int left = 0; for (; left <...
ooooo-youwillsee/leetcode
1768-Merge Strings Alternately/cpp_1768/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/2/28 13:16 */ #ifndef INC_1768_MERGE_STRINGS_ALTERNATELY__SOLUTION1_H_ #define INC_1768_MERGE_STRINGS_ALTERNATELY__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string mergeAlternately(string word1, string wo...
ooooo-youwillsee/leetcode
1004-Max Consecutive Ones III/cpp_1004/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/2/19 09:26 */ #ifndef CPP_1004__SOLUTION1_H_ #define CPP_1004__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; // 用队列存储位置,空间换时间 class Solution { public: int longestOnes(vector<int> &A, int K) { queue<int> q; int n =...
ooooo-youwillsee/leetcode
1673-Find the Most Competitive Subsequence/cpp_1673/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>1673-Find the Most Competitive Subsequence/cpp_1673/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2020/12/26 12:01 */ #ifndef CPP_1673__SOLUTION1_H_ #define CPP_1673__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; c...
ooooo-youwillsee/leetcode
1669-Merge In Between Linked Lists/cpp_1669/Solution1.h
<filename>1669-Merge In Between Linked Lists/cpp_1669/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/1/24 17:54 */ #ifndef CPP_1669__SOLUTION1_H_ #define CPP_1669__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack>...
ooooo-youwillsee/leetcode
0137-Single Number II/cpp_0137/Solution2.h
/** * @author ooooo * @date 2021/4/30 22:47 */ #ifndef CPP_0137__SOLUTION2_H_ #define CPP_0137__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int singleNumber(vector<int> &nums) { vector<int> bits(32, 0); for (int i = 0; i < nums.size(); i++) { int num...
ooooo-youwillsee/leetcode
0834-Sum of Distances in Tree/cpp_0834/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/10/6 13:17 */ #ifndef CPP_0834__SOLUTION2_H_ #define CPP_0834__SOLUTION2_H_ #include <iostream> #include <vector> #include <queue> #include <numeric> using namespace std; class Solution { public: // dp[i]:以i为根的距离总和 // sz[i]: 以i为根的子节点大小(包括自...
ooooo-youwillsee/leetcode
1542-Find Longest Awesome Substring/cpp_1542/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/4/6 14:58 */ #ifndef CPP_1542__SOLUTION1_H_ #define CPP_1542__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int longestAwesome(string s) { int x = 0; int n = s.size(); unordered_map<in...
ooooo-youwillsee/leetcode
0494-Target-Sum/cpp_0494/Solution3.h
<filename>0494-Target-Sum/cpp_0494/Solution3.h /** * @author ooooo * @date 2020/10/6 23:07 */ #ifndef CPP_0494__SOLUTION3_H_ #define CPP_0494__SOLUTION3_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; class Solution { public: int findTargetSumWays(vector<int> &nums, int S) { ...
ooooo-youwillsee/leetcode
0005-Longest Palindromic Substring/cpp_0005/Solution1.h
/** * @author ooooo * @date 2020/10/4 22:20 */ #ifndef CPP_0005__SOLUTION1_H_ #define CPP_0005__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string longestPalindrome(string s) { int n = s.size(); vector<vector<bool>> dp(n + 1, vector<bool>(n + 1, false))...
ooooo-youwillsee/leetcode
lcof_020/cpp_020/Solution1.h
// // Created by ooooo on 2020/3/14. // #ifndef CPP_020__SOLUTION1_H_ #define CPP_020__SOLUTION1_H_ #include <iostream> #include <vector> #include <strings.h> using namespace std; /** * A[.[B]][e|EC] */ class Solution { public: bool scanUnsignedInteger(string &s, int &i) { int j = i; while (s[i] >= '0'...
ooooo-youwillsee/leetcode
0337-House-Robber-III/cpp_0337/Solution3.h
// // Created by ooooo on 2020/2/25. // #ifndef CPP_0337__SOLUTION3_H_ #define CPP_0337__SOLUTION3_H_ #include "TreeNode.h" #include <unordered_map> using namespace std; /** * max money = max(根节点 + 四个孙子 , 两个儿子) * A * / \ * B B * / \ / \ * C C C C * * dp: 0 表示不偷, 1 表示偷 * * root[0] = max(ro...
ooooo-youwillsee/leetcode
0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/15. // #ifndef CPP_0094__SOLUTION2_H_ #define CPP_0094__SOLUTION2_H_ #include "TreeNode.h" #include <stack> #include <unordered_set> /** * iteration */ class Solution { public: vector<int> inorderTraversal(TreeNode *root) { if (!root) retu...
ooooo-youwillsee/leetcode
0077-Combinations/cpp_0077/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/9/8 12:06 */ #ifndef CPP_0077__SOLUTION1_H_ #define CPP_0077__SOLUTION1_H_ #include <iostream> #include <vector> #include <string> using namespace std; class Solution { public: vector<vector<int>> ans; void dfs(vector<int> &vec, int i, int n, int k) { if ...
ooooo-youwillsee/leetcode
0686-Repeated-String-Match/cpp_0686/Solution1.h
<filename>0686-Repeated-String-Match/cpp_0686/Solution1.h // // Created by ooooo on 2020/1/29. // #ifndef CPP_0686__SOLUTION1_H_ #define CPP_0686__SOLUTION1_H_ #include <iostream> #include <sstream> #include <math.h> using namespace std; class Solution { public: int repeatedStringMatch(string A, string B) { i...
ooooo-youwillsee/leetcode
0131-Palindrome Partitioning/cpp_0131/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/7 07:55 */ #ifndef CPP_0131__SOLUTION1_H_ #define CPP_0131__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<string>> dfs(string &s, int i) { if (i == s.size()) return {}; vector<vector<strin...
ooooo-youwillsee/leetcode
0771-Jewels-and-Stones/cpp_0771/SolutIon1.h
// // Created by ooooo on 2020/1/15. // #ifndef CPP_0771__SOLUTION1_H_ #define CPP_0771__SOLUTION1_H_ #include <iostream> #include <unordered_set> #include <numeric> using namespace std; class Solution { public: int numJewelsInStones(string J, string S) { unordered_set<char> s(J.begin(), J.end()); return ...
ooooo-youwillsee/leetcode
0148-Sort-List/cpp_0148/Solution2.h
<filename>0148-Sort-List/cpp_0148/Solution2.h // // Created by ooooo on 2020/2/18. // #ifndef CPP_0148__SOLUTION2_H_ #define CPP_0148__SOLUTION2_H_ #include "ListNode.h" /** * 复杂度要求 O(nlogn) ==> 归并排序 * 自底向上 */ class Solution { public: ListNode *sortList(ListNode *head) { ListNode *cur = head; int size =...
ooooo-youwillsee/leetcode
0057-Insert Interval/cpp_0057/Solution1.h
/** * @author ooooo * @date 2020/11/4 17:26 */ #ifndef CPP_0057__SOLUTION1_H_ #define CPP_0057__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval) { if (intervals.empty()) retur...
ooooo-youwillsee/leetcode
0081-Search in Rotated Sorted Array II/cpp_0081/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/4/7 10:16 */ #ifndef CPP_0081__SOLUTION1_H_ #define CPP_0081__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool search(vector<int> &nums, int target) { int n = nums.size(); int l = 0, r = n - 1; while ...
ooooo-youwillsee/leetcode
0040-Combination-Sum-II/cpp_0040/Solution1.h
/** * @author ooooo * @date 2020/9/10 14:39 */ #ifndef CPP_0040__SOLUTION1_H_ #define CPP_0040__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: struct Vec_Hash { size_t operator()(vector<int> vec) const { size_t ret = 0; for (i...
ooooo-youwillsee/leetcode
0897-Increasing-Order-Search-Tree/cpp_0897/Solution2.h
/** * @author ooooo * @date 2021/4/25 09:47 */ #ifndef CPP_0897__SOLUTION2_H_ #define CPP_0897__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: TreeNode *increasingBST(TreeNode *root) { if (!root) return nullptr; if (!root->left && !root->right) return root; TreeNode *l = increasingBST(root->...
ooooo-youwillsee/leetcode
0120-Triangle/cpp_0120/Solution1.h
// // Created by ooooo on 2019/12/16. // #ifndef CPP_0120_SOLUTION1_H #define CPP_0120_SOLUTION1_H #include <iostream> #include <vector> #include <climits> using namespace std; /** * 可能会超出时间限制 */ class Solution { private: int min = INT_MAX; void dfs(vector<vector<int>> &vec, int row, int col, int sum) {...
ooooo-youwillsee/leetcode
0021-Merge-Two-Sorted-Lists/cpp_0021/Solution1.h
// // Created by ooooo on 2020/1/23. // #ifndef CPP_0021__SOLUTION1_H_ #define CPP_0021__SOLUTION1_H_ #include "ListNode.h" /** * 每个节点重新new */ class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *dummyHead = new ListNode(0), *cur = dummyHead; while (l1 && l2) { i...
ooooo-youwillsee/leetcode
0987-Vertical Order Traversal of a Binary Tree/cpp_0987/Solution2.h
/** * @author ooooo * @date 2021/2/17 17:21 */ #ifndef CPP_0987__SOLUTION2_H_ #define CPP_0987__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: void dfs(TreeNode *root, int col, int row) { if (!root) { return; } m[col][row].push_back(root->val); dfs(root->left, col - 1, row + 1); dfs(r...
ooooo-youwillsee/leetcode
0392-Is-Subsequence/cpp_0392/Solution2.h
// // Created by ooooo on 2020/1/18. // #ifndef CPP_0392__SOLUTION2_H_ #define CPP_0392__SOLUTION2_H_ #include <iostream> using namespace std; /** * find_first_of */ class Solution { public: bool isSubsequence(string s, string t) { int i = 0; for (auto c:s) { int j = t.find_first_of(c, i); i...
ooooo-youwillsee/leetcode
lcof_063/cpp_063/Solution2.h
// // Created by ooooo on 2020/4/24. // #ifndef CPP_063__SOLUTION2_H_ #define CPP_063__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxProfit(vector<int> &prices) { if (prices.empty()) return 0; int n = prices.size(), ans = 0, min = prices[0]; fo...
ooooo-youwillsee/leetcode
0110-Balanced Binary Tree/cpp_0110/Solution1.h
<filename>0110-Balanced Binary Tree/cpp_0110/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2019/12/28. // #ifndef CPP_0110_SOLUTION1_H #define CPP_0110_SOLUTION1_H #include "TreeNode.h" class Solution { private: int depth(TreeNode *node) { if (!node) return 0; return max(depth(node->left...
ooooo-youwillsee/leetcode
0447-Number-of-Boomerangs/cpp_0447/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/11. // #ifndef CPP_0447__SOLUTION1_H_ #define CPP_0447__SOLUTION1_H_ #include <iostream> #include <vector> #include <cmath> #include <unordered_map> using namespace std; class Solution { public: int distance(vector<int> a, vector<int> b) { return pow(b[0] - a...
ooooo-youwillsee/leetcode
1002-Find-Common-Characters/cpp_1002/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/16. // #ifndef CPP_1002__SOLUTION1_H_ #define CPP_1002__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> using namespace std; class Solution { public: vector<string> commonChars(vector<string> &A) { vector<un...
ooooo-youwillsee/leetcode
lcof_039/cpp_039/Solution2.h
// // Created by ooooo on 2020/3/24. // #ifndef CPP_039__SOLUTION2_H_ #define CPP_039__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * partition timeout */ class Solution { public: int partition(int i, int j, vector<int> &nums) { int k = i, item = nums[i]; for (int p = i ...
ooooo-youwillsee/leetcode
lcof_047/cpp_047/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/4/3. // #ifndef CPP_047__SOLUTION1_H_ #define CPP_047__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dfs timeout */ class Solution { public: void dfs(int i, int j, int sum) { if (i >= m || j >= n) return; ...
ooooo-youwillsee/leetcode
0065-Valid Number/cpp_0065/Solution1.h
<filename>0065-Valid Number/cpp_0065/Solution1.h // // Created by ooooo on 6/17/2021. // #ifndef CPP_0065_SOLUTION1_H #define CPP_0065_SOLUTION1_H #include <iostream> #include <vector> using namespace std; /** * 直接复制的 */ class Solution { public: enum State { STATE_INITIAL, STATE_INT_SIGN, ...
ooooo-youwillsee/leetcode
lcof_058-1/cpp_058-1/Solution1.h
// // Created by ooooo on 2020/4/18. // #ifndef CPP_058_1__SOLUTION1_H_ #define CPP_058_1__SOLUTION1_H_ #include <iostream> #include <vector> #include <sstream> using namespace std; class Solution { public: int findFirstLetter(string &s, int i) { for (; i < s.size(); i++) { if (s[i] != ' ') return i; ...
ooooo-youwillsee/leetcode
0113-Path-Sum-II/cpp_0113/TreeNode.h
<filename>0113-Path-Sum-II/cpp_0113/TreeNode.h /** * @author ooooo * @date 2020/9/26 00:12 */ #ifndef CPP_0113__TREENODE_H_ #define CPP_0113__TREENODE_H_ #include <iostream> #include <vector> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL...
ooooo-youwillsee/leetcode
0657-Robot-Return-to-Origin/cpp_0657/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/28. // #ifndef CPP_0657__SOLUTION1_H_ #define CPP_0657__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: bool judgeCircle(string moves) { int i = 0, j = 0; for (auto move: moves) { if (move == 'U') i++; else if(mov...
ooooo-youwillsee/leetcode
0142-Linked-List-Cycle-II/cpp_0142/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/17. // #ifndef CPP_0142__SOLUTION2_H_ #define CPP_0142__SOLUTION2_H_ #include "LIstNode.h" /** * 快慢指针 */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *low = head, *fast = head; bool hasCycle = false; while (fast && fast->n...
ooooo-youwillsee/leetcode
0645-Set-Mismatch/cpp_0645/Solution1.h
// // Created by ooooo on 2020/1/13. // #ifndef CPP_0645__SOLUTION1_H_ #define CPP_0645__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: vector<int> findErrorNums(vector<int> &nums) { unordered_set<int> s; int i = 0, sum = 0; f...
ooooo-youwillsee/leetcode
0114-Flatten-Binary-Tree-to-Linked-List/cpp_0114/Solution1.h
<filename>0114-Flatten-Binary-Tree-to-Linked-List/cpp_0114/Solution1.h // // Created by ooooo on 2020/2/16. // #ifndef CPP_0114__SOLUTION1_H_ #define CPP_0114__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: TreeNode *help(TreeNode *node) { if (!node) return nullptr; TreeNode *left = help(node...
ooooo-youwillsee/leetcode
0023-Merge-k-Sorted-Lists/cpp_0023/Solution1.h
<filename>0023-Merge-k-Sorted-Lists/cpp_0023/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2020/9/25 22:36 */ #ifndef CPP_0023__SOLUTION1_H_ #define CPP_0023__SOLUTION1_H_ #endif //CPP_0023__SOLUTION1_H_ #include "ListNode.h" class Solution { public: struct Comp { bool operator()(const ListNode *...
ooooo-youwillsee/leetcode
0599-Minimum-Index-Sum-of-Two-Lists/cpp_0599/Solution1.h
// // Created by ooooo on 2020/1/12. // #ifndef CPP_0599__SOLUTION1_H_ #define CPP_0599__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<string> findRestaurant(vector<string> &list1, vector<string> &list2) { unordered_map<string,...
ooooo-youwillsee/leetcode
0530-Minimum-Absolute-Difference-in-BST/cpp_0530/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/2. // #ifndef CPP_0530_SOLUTION1_H #define CPP_0530_SOLUTION1_H #include "TreeNode.h" #include <vector> #include <climits> class Solution { public: void dfs(TreeNode *node, vector<int> &vec) { if (!node) return; dfs(node->left, vec); vec...
ooooo-youwillsee/leetcode
0671-Second-Minimum-Node-In-a-Binary-Tree/cpp_0671/Solution2.h
// // Created by ooooo on 2020/1/4. // #ifndef CPP_0671_SOLUTION2_H #define CPP_0671_SOLUTION2_H #include "TreeNode.h" #include <queue> class Solution { public: int findSecondMinimumValue(TreeNode *root) { if (!root) return -1; queue<TreeNode *> q; q.push(root); // flag 定义最小值是否有INT...
ooooo-youwillsee/leetcode
1006-Clumsy Factorial/cpp_1006/Solution2.h
<reponame>ooooo-youwillsee/leetcode<filename>1006-Clumsy Factorial/cpp_1006/Solution2.h /** * @author ooooo * @date 2021/4/1 18:03 */ #ifndef CPP_1006__SOLUTION2_H_ #define CPP_1006__SOLUTION2_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: int clumsy(i...
ooooo-youwillsee/leetcode
0150-Evaluate Reverse Polish Notation/cpp_0150/Solution1.h
/** * @author ooooo * @date 2021/3/20 12:13 */ #ifndef CPP_0150__SOLUTION1_H_ #define CPP_0150__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: int evalRPN(vector<string> &tokens) { stack<int> s; for (int i = 0; i < tokens.size(); ++i) { ...
ooooo-youwillsee/leetcode
lcci_10_03/cpp_10_03/Solution1.h
/** * @author ooooo * @date 2021/4/10 11:48 */ #ifndef CPP_10_03__SOLUTION1_H_ #define CPP_10_03__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int search(vector<int> &arr, int target) { int n = arr.size(); int l = 0, r = n - 1; int ans = INT_MAX; whi...
ooooo-youwillsee/leetcode
0929-Unique-Email-Addresses/cpp_0929/Solution1.h
// // Created by ooooo on 2020/2/1. // #ifndef CPP_0929__SOLUTION1_H_ #define CPP_0929__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <sstream> using namespace std; class Solution { public: int numUniqueEmails(vector<string> &emails) { unordered_set<string> set; stri...
ooooo-youwillsee/leetcode
lcof_014/cpp_014-1/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>lcof_014/cpp_014-1/Solution1.h // // Created by ooooo on 2020/3/11. // #ifndef CPP_014_1__SOLUTION1_H_ #define CPP_014_1__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; /** * dfs + memo */ class Solution { public: /** * @param flag...
ooooo-youwillsee/leetcode
0028-Implement-strStr/cpp_0028/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/7. // #ifndef CPP_0028_SOLUTION1_H #define CPP_0028_SOLUTION1_H #include <iostream> using namespace std; class Solution { public: int strStr(string haystack, string needle) { if (needle == "") return 0; if (haystack == "" || hay...
ooooo-youwillsee/leetcode
lcof_056-1/cpp_056-1/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/4/14. // #ifndef CPP_056_1__SOLUTION1_H_ #define CPP_056_1__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findFirstBit(int num) { int ans = 1; while ((ans & num) == 0) { ans <<=...