repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
lcof_068-2/cpp_068-2/Solution1.h
// // Created by ooooo on 2020/4/26. // #ifndef CPP_068_2__SOLUTION1_H_ #define CPP_068_2__SOLUTION1_H_ #include "TreeNode.h" #include <vector> class Solution { public: bool dfs(TreeNode *node, TreeNode *p, vector<TreeNode *> &pNodes) { if (!node) return false; if (node->val == p->val || dfs(node->left, p...
ooooo-youwillsee/leetcode
0052-N-Queens-II/cpp_0052/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2019/12/7. // #ifndef CPP_0052_SOLUTION1_H #define CPP_0052_SOLUTION1_H class Solution { private: int total = 0; int n; void dfs(int row, int col, int pie, int na) { if (row >= n) { total += 1; return; } int bits ...
ooooo-youwillsee/leetcode
0054-Spiral Matrix/cpp_0054/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/10/5 17:16 */ #ifndef CPP_0054__SOLUTION1_H_ #define CPP_0054__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /* 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,3,6,9,8,7,4,5] */ class Solution { public: ve...
ooooo-youwillsee/leetcode
0086-Partition List/cpp_0086/ListNode.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/4 16:15 */ #ifndef CPP_0086__LISTNODE_H_ #define CPP_0086__LISTNODE_H_ #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; #endif //CPP_0086__LISTNODE_H_
ooooo-youwillsee/leetcode
0987-Vertical Order Traversal of a Binary Tree/cpp_0987/TreeNode.h
/** * @author ooooo * @date 2021/2/17 17:19 */ #ifndef CPP_0987__TREENODE_H_ #define CPP_0987__TREENODE_H_ #include <vector> #include <map> #include <set> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) :...
ooooo-youwillsee/leetcode
1701-Average Waiting Time/cpp_1701/Solution1.h
/** * @author ooooo * @date 2021/1/24 18:08 */ #ifndef CPP_1701__SOLUTION1_H_ #define CPP_1701__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> #include <queue> using namespace std; class Solution2 { double averageWaitingTime(vector<vector<int>> &customers) { long long total_wait = 0; ...
ooooo-youwillsee/leetcode
0303-Range-Sum-Query-Immutable/cpp_0303/Solution1.h
<filename>0303-Range-Sum-Query-Immutable/cpp_0303/Solution1.h // // Created by ooooo on 2020/1/29. // #ifndef CPP_0303__SOLUTION1_H_ #define CPP_0303__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class NumArray { public: int *sum; NumArray(vector<int> &nums) { sum = new int[nums.s...
ooooo-youwillsee/leetcode
0140-Word Break II/cpp_0140/Solution1.h
/** * @author ooooo * @date 2020/11/1 10:24 */ #ifndef CPP_0140__SOLUTION1_H_ #define CPP_0140__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int min_len = INT_MAX, max_len = INT_MIN; unordered_set<string> set; vector<string> dfs(...
ooooo-youwillsee/leetcode
lcof_018/cpp_018/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/13. // #ifndef CPP_018__SOLUTION2_H_ #define CPP_018__SOLUTION2_H_ #include "ListNode.h" /** * 把后面的值逐一复制到 delNode 的 val */ class Solution { public: ListNode *deleteNode(ListNode *head, int val) { ListNode *dummyHead = new ListNode(0), *prev...
ooooo-youwillsee/leetcode
0349-Intersection-of-Two-Arrays/cpp_0349/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2019/12/29. // #ifndef CPP_0349_SOLUTION1_H #define CPP_0349_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: vector<int> intersection(vector<int> &nums1, vector<int> &nums2) { unordered_set...
ooooo-youwillsee/leetcode
1737-Change Minimum Characters to Satisfy One of Three Conditions/cpp_1737/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/2/28 13:13 */ #ifndef CPP_1737__SOLUTION1_H_ #define CPP_1737__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
lcof_004/cpp_004/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/6. // #ifndef CPP_004__SOLUTION1_H_ #define CPP_004__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * O(m+n) */ class Solution { public: bool findNumberIn2DArray(vector<vector<int>> &matrix, int target) { if (m...
ooooo-youwillsee/leetcode
lcof_048/cpp_048/Solution2.h
// // Created by ooooo on 2020/4/5. // #ifndef CPP_048__SOLUTION2_H_ #define CPP_048__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] = dp[i-1] + 1 * * dp[i] 表示以第 i 个字符为尾的长度 */ class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.size(), ans = 0; ...
ooooo-youwillsee/leetcode
0211-Add-and-Search-Word-Data-structure-design/cpp_0211/Solution1.h
// // Created by ooooo on 2020/2/7. // #ifndef CPP_0211__SOLUTION1_H_ #define CPP_0211__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class WordDictionary { private: struct Node { bool isWord; vector<Node *> next; Node() : isWord(false), next(26, nullptr) {} }; Node *r...
ooooo-youwillsee/leetcode
0279-Perfect-Squares/cpp_0279/Solution2.h
// // Created by ooooo on 2020/2/23. // #ifndef CPP_0279__SOLUTION2_H_ #define CPP_0279__SOLUTION2_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; /** * dp[i] = min (dp[i-1] + dp[1]), ( dp[i-2] + dp[2] ) ,,, (dp[0] + dp[i]) * timeout */ class Solution { public: int numSquares(...
ooooo-youwillsee/leetcode
0045-Jump Game II/cpp_0045/Solution2.h
/** * @author ooooo * @date 2021/6/4 20:21 */ #ifndef CPP_0045__SOLUTION2_H_ #define CPP_0045__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 从0到n-1 循环获取,贪心 */ class Solution { public: int jump(vector<int> &nums) { int n = nums.size(); int last = n - 1; int ans = 0; wh...
ooooo-youwillsee/leetcode
0377-Combination Sum IV/cpp_0377/Solution2.h
<filename>0377-Combination Sum IV/cpp_0377/Solution2.h /** * @author ooooo * @date 2021/4/24 11:13 */ #ifndef CPP_0377__SOLUTION2_H_ #define CPP_0377__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int combinationSum4(vector<int> &nums, int target) { int n =...
ooooo-youwillsee/leetcode
0696-Count-Binary-Substrings/cpp_0696/Solution1.h
// // Created by ooooo on 2020/1/30. // #ifndef CPP_0696__SOLUTION1_H_ #define CPP_0696__SOLUTION1_H_ #include <iostream> using namespace std; /** * 暴力法 */ class Solution { public: bool valid(string &s, int left, int right) { char leftC = s[left++], rightC = s[right--]; if (leftC == rightC) return fals...
ooooo-youwillsee/leetcode
0135-Candy/cpp_0135/Solution2.h
/** * @author ooooo * @date 2020/12/24 17:12 */ #ifndef CPP_0135__SOLUTION2_H_ #define CPP_0135__SOLUTION2_H_ #include <iostream> #include <map> #include <vector> #include <numeric> using namespace std; class Solution { public: int candy(vector<int> &ratings) { int n = ratings.size(); vector<int> candy(n,...
ooooo-youwillsee/leetcode
lcof_068-2/cpp_068-2/Solution2.h
<filename>lcof_068-2/cpp_068-2/Solution2.h<gh_stars>10-100 // // Created by ooooo on 2020/4/26. // #ifndef CPP_068_2__SOLUTION2_H_ #define CPP_068_2__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { if (!root) return nullptr...
ooooo-youwillsee/leetcode
0368-Largest Divisible Subset/cpp_0368/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/4/23 19:52 */ #ifndef CPP_0368__SOLUTION1_H_ #define CPP_0368__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> largestDivisibleSubset(vector<int> &nums) { sort(nums.begin(), nums...
ooooo-youwillsee/leetcode
0325-Maximum Size Subarray Sum Equals k/cpp_0325/Solution1.h
/** * @author ooooo * @date 2021/6/4 20:52 */ #ifndef CPP_0325__SOLUTION1_H_ #define CPP_0325__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> class Solution { public: int maxSubArrayLen(vector<int> &nums, int k) { int n = nums.size(); unordered_map<int, int> m; m[0] = -1; i...
ooooo-youwillsee/leetcode
0299-Bulls-and-Cows/cpp_0299/Solution1.h
<filename>0299-Bulls-and-Cows/cpp_0299/Solution1.h // // Created by ooooo on 2020/1/10. // #ifndef CPP_0299__SOLUTION1_H_ #define CPP_0299__SOLUTION1_H_ #include <iostream> #include <unordered_set> #include <unordered_map> using namespace std; class Solution { public: string getHint(string secret, string guess) {...
ooooo-youwillsee/leetcode
0987-Vertical Order Traversal of a Binary Tree/cpp_0987/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/2/17 17:19 */ #ifndef CPP_0987__SOLUTION1_H_ #define CPP_0987__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: void dfs(TreeNode *root, vector<vector<vector<int>>> &ans, int col, int row) { if (!root) { return; } ans[col]...
ooooo-youwillsee/leetcode
0861-Score After Flipping Matrix/cpp_0861/Solution1.h
/** * @author ooooo * @date 2020/12/7 15:01 */ #ifndef CPP_0861__SOLUTION1_H_ #define CPP_0861__SOLUTION1_H_ #include <iostream> #include <math.h> #include <vector> using namespace std; /** * 判断左上角是否为1, 如果为0,可以flipX或者flipY, 后面的行只能flipX */ class Solution { public: vector<vector<int>> A; int m, n; int cal...
ooooo-youwillsee/leetcode
lcof_036/cpp_036/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/3/23. // #ifndef CPP_036__SOLUTION1_H_ #define CPP_036__SOLUTION1_H_ #include "Node.h" class Solution { public: void dfs(Node *node) { if (!node) return; dfs(node->left); if (!head) { head = node; prev = nod...
ooooo-youwillsee/leetcode
0100-Same-Tree/cpp_0100/Solution1.h
// // Created by ooooo on 2019/12/6. // #ifndef CPP_0100_SOLUTION1_H #define CPP_0100_SOLUTION1_H #include "TreeNode.h" class Solution { private: bool dfs(TreeNode *p, TreeNode *q) { if (!p && !q) { return true; } else if (!p || !q) { return false; } retur...
ooooo-youwillsee/leetcode
0155-Min-Stack/cpp_0155/Solution1.h
// // Created by ooooo on 2019/12/30. // #ifndef CPP_0155_SOLUTION1_H #define CPP_0155_SOLUTION1_H #include <iostream> #include <vector> #include <climits> using namespace std; class MinStack { private: vector<int> data; int min = INT_MAX; public: /** initialize your data structure here. */ MinStack...
ooooo-youwillsee/leetcode
lcof_017/cpp_017/Solution1.h
// // Created by ooooo on 2020/3/13. // #ifndef CPP_017__SOLUTION1_H_ #define CPP_017__SOLUTION1_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; class Solution { public: vector<int> printNumbers(int n) { int num = 1; while (n--) num *= 10; vector<int> ans(num - 1); ...
ooooo-youwillsee/leetcode
1780-Check if Number is a Sum of Powers of Three/cpp_1780/Solution1.h
<filename>1780-Check if Number is a Sum of Powers of Three/cpp_1780/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/3/14 14:56 */ #ifndef CPP_1780__SOLUTION1_H_ #define CPP_1780__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #inclu...
ooooo-youwillsee/leetcode
0461-Hamming-Distance/cpp_0461/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0461-Hamming-Distance/cpp_0461/Solution1.h // // Created by ooooo on 2020/2/6. // #ifndef CPP_0461__SOLUTION1_H_ #define CPP_0461__SOLUTION1_H_ #include <iostream> #include <sstream> using namespace std; /** * 将数字转化为字符串,然后^操作 */ class Solution { public: string help(i...
ooooo-youwillsee/leetcode
0690-Employee-Importance/cpp_0690/Employee.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/13. // #ifndef CPP_0690__EMPLOYEE_H_ #define CPP_0690__EMPLOYEE_H_ #include <iostream> #include <vector> using namespace std; class Employee { public: // It's the unique ID of each node. // unique id of this employee int id; // the importance value of this e...
ooooo-youwillsee/leetcode
0557-Reverse-Words-in-a-String-III/cpp_0557/Solution1.h
// // Created by ooooo on 2020/1/28. // #ifndef CPP_0557__SOLUTION1_H_ #define CPP_0557__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: string reverseWords(string s) { s += " "; int i = 0, j = s.find(" ", i); while (j != -1) { reverse(s.begin() + i, s.begin() + j)...
ooooo-youwillsee/leetcode
0188-Best Time to Buy and Sell Stock IV/cpp_0188/Solution1.h
/** * @author ooooo * @date 2020/12/28 16:31 */ #ifndef CPP_0188__SOLUTION1_H_ #define CPP_0188__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i][j][n] 表示第i天进行第j次交易,持有n股 */ class Solution { public: int maxProfit(int k, vector<int> &prices) { int n = prices.size(); int ...
ooooo-youwillsee/leetcode
1685-Sum of Absolute Differences in a Sorted Array/cpp_1685/Solution1.h
<filename>1685-Sum of Absolute Differences in a Sorted Array/cpp_1685/Solution1.h /** * @author ooooo * @date 2020/12/20 13:07 */ #ifndef CPP_1685__SOLUTION1_H_ #define CPP_1685__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> #include <queue> #include <set> #include <unordered_map> #include <...
ooooo-youwillsee/leetcode
0748-Shortest-Completing-Word/cpp_0748/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/14. // #ifndef CPP_0748__SOLUTION1_H_ #define CPP_0748__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: bool check(string word, unordered_map<char, int> &m) { unordered_map<char, int> w...
ooooo-youwillsee/leetcode
lcp-033/cpp_033/Solution1.h
<filename>lcp-033/cpp_033/Solution1.h /** * @author ooooo * @date 2021/4/10 21:05 */ #ifndef CPP_033__SOLUTION1_H_ #define CPP_033__SOLUTION1_H_ class Solution { public: int storeWater(vector<int> &bucket, vector<int> &vat) { int n = bucket.size(); int a = 1; int b = 0; for (int i = 0; i < n; i++) { ...
ooooo-youwillsee/leetcode
0354-Russian Doll Envelopes/cpp_0354/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/4 12:09 */ #ifndef CPP_0354__SOLUTION1_H_ #define CPP_0354__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxEnvelopes(vector<vector<int>> &envelopes) { sort(envelopes.begin(), envelopes.end(), [](cons...
ooooo-youwillsee/leetcode
0304-Range Sum Query 2D - Immutable/cpp_0304/Solution1.h
<filename>0304-Range Sum Query 2D - Immutable/cpp_0304/Solution1.h /** * @author ooooo * @date 2021/3/2 09:03 */ #ifndef CPP_0304__SOLUTION1_H_ #define CPP_0304__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class NumMatrix { public: vector<vector<int>> preSum; NumMatrix(vector<vec...
ooooo-youwillsee/leetcode
0331-Verify Preorder Serialization of a Binary Tree/cpp_0331/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/12 20:00 */ #ifndef CPP_0331__SOLUTION2_H_ #define CPP_0331__SOLUTION2_H_ #include <iostream> #include <vector> #include <stack> using namespace std; // 插槽栈 class Solution { public: bool isValidSerialization(string preorder) { stack<int> s; s.push(1); ...
ooooo-youwillsee/leetcode
0045-Jump Game II/cpp_0045/Solution3.h
/** * @author ooooo * @date 2021/6/4 20:38 */ #ifndef CPP_0045__SOLUTION3_H_ #define CPP_0045__SOLUTION3_H_ #include <vector> #include <iostream> using namespace std; class Solution { public: int jump(vector<int> &nums) { int n = nums.size(); int ans = 0, end = 0, pos = 0; for (int i = 0; i < n - 1; i++...
ooooo-youwillsee/leetcode
0129-Sum Root to Leaf Numbers/cpp_0129/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/10/29 09:31 */ #ifndef CPP_0129__SOLUTION1_H_ #define CPP_0129__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: int ans = 0; void dfs(TreeNode *node, int cur) { if (!node) return; cur = cur * 10 + node->val; if (!node->le...
ooooo-youwillsee/leetcode
0456-132 Pattern/cpp_0456/Solution2.h
/** * @author ooooo * @date 2021/3/24 16:38 */ #ifndef CPP_0456__SOLUTION2_H_ #define CPP_0456__SOLUTION2_H_ #include <iostream> #include <vector> #include <stack> using namespace std; // o(n) class Solution { public: bool find132pattern(vector<int> &nums) { int n = nums.size(); if (n < 3) { return fal...
ooooo-youwillsee/leetcode
1178-Number of Valid Words for Each Puzzle/cpp_1178/Solution1.h
/** * @author ooooo * @date 2021/2/26 13:28 */ #ifndef CPP_1178__SOLUTION1_H_ #define CPP_1178__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> findNumOfValidWords(vector<string> &words, vector<string> &puzzles) { // 对word...
ooooo-youwillsee/leetcode
1489-Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree/cpp_1489/Solution1.h
<filename>1489-Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree/cpp_1489/Solution1.h /** * @author ooooo * @date 2021/1/21 20:36 */ #ifndef CPP_1489__SOLUTION1_H_ #define CPP_1489__SOLUTION1_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; class Solution { public...
ooooo-youwillsee/leetcode
1702-Maximum Binary String After Change/cpp_1702/Solution1.h
<filename>1702-Maximum Binary String After Change/cpp_1702/Solution1.h /** * @author ooooo * @date 2021/1/24 18:09 */ #ifndef CPP_1702__SOLUTION1_H_ #define CPP_1702__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> #include <queue> using namespace std; class Solution2 { string maximumBinar...
ooooo-youwillsee/leetcode
0718-Maximum-Length-of-Repeated-Subarray/cpp_0718/Solution2.h
<filename>0718-Maximum-Length-of-Repeated-Subarray/cpp_0718/Solution2.h /** * @author ooooo * @date 2020/10/5 13:15 */ #ifndef CPP_0718__SOLUTION2_H_ #define CPP_0718__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findLength(vector<int> &A, vector<int> &B...
ooooo-youwillsee/leetcode
0399-Evaluate Division/cpp_0399/Solutoin1.h
<filename>0399-Evaluate Division/cpp_0399/Solutoin1.h /** * @author ooooo * @date 2021/1/6 09:33 */ #ifndef CPP_0399__SOLUTOIN1_H_ #define CPP_0399__SOLUTOIN1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> using namespace std; class Solution { public...
ooooo-youwillsee/leetcode
0720-Longest-Word-in-Dictionary/cpp_0720/Solution2.h
// // Created by ooooo on 2020/1/14. // #ifndef CPP_0720__SOLUTION2_H_ #define CPP_0720__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; /** * 哈希表 */ class Solution { public: bool check(string word, unordered_set<string> &s) { for (i...
ooooo-youwillsee/leetcode
0139-Word-Break/cpp_0139/Solution2.h
<filename>0139-Word-Break/cpp_0139/Solution2.h // // Created by ooooo on 2020/2/17. // #ifndef CPP_0139__SOLUTION2_H_ #define CPP_0139__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * 记忆化 记录索引 i 的位置 */ class Solution { public: unordered_set<string> word_se...
ooooo-youwillsee/leetcode
0090-Subsets II/cpp_0090/Solution1.h
<filename>0090-Subsets II/cpp_0090/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/3/31 09:58 */ #ifndef CPP_0090__SOLUTION1_H_ #define CPP_0090__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> ans; void dfs(vector<int> &nums...
ooooo-youwillsee/leetcode
0136-Single-Number/cpp_0136/Solution2.h
// // Created by ooooo on 2019/12/30. // #ifndef CPP_0136_SOLUTION2_H #define CPP_0136_SOLUTION2_H #include <iostream> #include <vector> using namespace std; class Solution { public: int singleNumber(vector<int> &nums) { int a = 0; for (auto num: nums) { a ^= num; } r...
ooooo-youwillsee/leetcode
0441-Arranging-Coins/cpp_0441/Solution3.h
<filename>0441-Arranging-Coins/cpp_0441/Solution3.h // // Created by ooooo on 2020/1/19. // #ifndef CPP_0441__SOLUTION3_H_ #define CPP_0441__SOLUTION3_H_ #include <iostream> #include <cmath> using namespace std; /** * 数学公式求解 k(k+1) /2 = n ==> 其正数解为 k = sqrt(2n+1/4) - 1/2 * * => k = sqrt(2) * sqrt(n + 1/8) - 1/2 ...
ooooo-youwillsee/leetcode
0092-Reverse Linked List II/cpp_0092/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/18 17:59 */ #ifndef CPP_0092__SOLUTION1_H_ #define CPP_0092__SOLUTION1_H_ #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ...
ooooo-youwillsee/leetcode
1576-Replace-All-XLetter-to-Avoid-Consecutive-Repeating-Characters/cpp_1576/Solution2.h
/** * @author ooooo * @date 2020/9/7 14:56 */ #ifndef CPP_1576__SOLUTION2_H_ #define CPP_1576__SOLUTION2_H_ #include <iostream> #include <string> using namespace std; class Solution { public: string modifyString(string s) { for (int i = 0; i < s.size(); ++i) { if (s[i] != '?') continue; char c = 'a';...
ooooo-youwillsee/leetcode
0970-Powerful-Integers/cpp_0970/Solution1.h
// // Created by ooooo on 2020/1/17. // #ifndef CPP_0970__SOLUTION1_H_ #define CPP_0970__SOLUTION1_H_ #include <iostream> #include <unordered_set> #include <vector> using namespace std; class Solution { public: int calcMaxBound(int x, int bound) { if (x == 1) return bound; int ans = 0, sum = 1; whil...
ooooo-youwillsee/leetcode
1744-Can You Eat Your Favorite Candy on Your Favorite Day/cpp_1744/Solution1.h
/** * @author ooooo * @date 2021/6/1 14:17 */ #ifndef CPP_1744__SOLUTION1_H_ #define CPP_1744__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: using LL = long long; vector<bool> canEat(vector<int> &candiesCount, vector<vector<int>> &queries) { int n = candi...
ooooo-youwillsee/leetcode
0501-Find-Mode-in-Binary-Search-Tree/cpp_0501/Solution2.h
/** * @author ooooo * @date 2020/9/24 10:46 */ #ifndef CPP_0501__SOLUTION2_H_ #define CPP_0501__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: unordered_map<int, int> m; int c = 0; // 最高 void dfs(TreeNode *node) { if (!node) return; dfs(node->left); m[node->val]++; c = max(c, m[node->v...
ooooo-youwillsee/leetcode
0283-Move-Zeroes/cpp_0283/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/6. // #ifndef CPP_0283_SOLUTION1_H #define CPP_0283_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: void moveZeroes(vector<int> &nums) { if (nums.size() <= 1) return; int i = 0, j = 0; while ...
ooooo-youwillsee/leetcode
lcof_041/cpp_041/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>lcof_041/cpp_041/Solution1.h // // Created by ooooo on 2020/3/25. // #ifndef CPP_041__SOLUTION1_H_ #define CPP_041__SOLUTION1_H_ #include <iostream> #include <queue> using namespace std; /** * solution1 : AVL树,每个节点记录子节点个数 * solution2 : 最大堆 < 最小堆 */ class MedianFinder {...
ooooo-youwillsee/leetcode
0107-Binary-Tree-Level-Order-Traversal-II/cpp_0107/Solution1.h
<filename>0107-Binary-Tree-Level-Order-Traversal-II/cpp_0107/Solution1.h // // Created by ooooo on 2019/12/28. // #ifndef CPP_0107_SOLUTION1_H #define CPP_0107_SOLUTION1_H #include "TreeNode.h" #include <stack> #include <vector> #include <queue> class Solution { private: vector<vector<int>> levelOrder(TreeNode *...
ooooo-youwillsee/leetcode
lcof_032-1/cpp_032-1/Solution1.h
// // Created by ooooo on 2020/3/20. // #ifndef CPP_032_1__SOLUTION1_H_ #define CPP_032_1__SOLUTION1_H_ #include "TreeNode.h" #include <queue> class Solution { public: vector<int> levelOrder(TreeNode *root) { if (!root) return {}; vector<int> ans; queue<TreeNode *> q; q.push(root); while (!q.em...
ooooo-youwillsee/leetcode
lcof_059-1/cpp_059-1/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/19. // #ifndef CPP_059_1__SOLUTION2_H_ #define CPP_059_1__SOLUTION2_H_ #include <iostream> #include <deque> #include <vector> using namespace std; class Solution { public: vector<int> maxSlidingWindow(vector<int> &nums, int k) { if (k == 0 && nums.empty()) re...
ooooo-youwillsee/leetcode
0659-Split Array into Consecutive Subsequences/cpp_0659/Solution2.h
/** * @author ooooo * @date 2020/12/4 21:35 */ #ifndef CPP_0659__SOLUTION2_H_ #define CPP_0659__SOLUTION2_H_ #include <iostream> #include <vector> #include <queue> #include <unordered_map> using namespace std; class Solution { public: bool isPossible(vector<int> &nums) { // key是以x结尾的子序列, value是子序列长度的最小堆 u...
ooooo-youwillsee/leetcode
1608-Special Array With X Elements Greater Than or Equal X/cpp_1608/Solution1.h
/** * @author ooooo * @date 2020/10/11 13:40 */ #ifndef CPP_1608__SOLUTION1_H_ #define CPP_1608__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <queue> using namespace std; int specialArray(vector<int> &nums) { sort(nums.begin(), nums.end()); for (int i = 1; i <= nums.siz...
ooooo-youwillsee/leetcode
0190-Reverse Bits/cpp_0190/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/29 16:35 */ #ifndef CPP_0190__SOLUTION1_H_ #define CPP_0190__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t x = 0; for (int i = 0; i < 32; i++) { if (n & (1 << i)) { x |= ...
ooooo-youwillsee/leetcode
0344-Reverse-String/cpp_0344/Solution1.h
<filename>0344-Reverse-String/cpp_0344/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/8. // #ifndef CPP_0344_SOLUTION1_H #define CPP_0344_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: void reverseString(vector<char> &s) { if (s.size() <= 1)...
ooooo-youwillsee/leetcode
0709-To-Lower-Case/cpp_0709/Solution1.h
// // Created by ooooo on 2020/1/30. // #ifndef CPP_0709__SOLUTION1_H_ #define CPP_0709__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: string toLowerCase(string str) { for (int i = 0; i < str.size(); ++i) { str[i] = tolower(str[i]); } return str; } }; #endif /...
ooooo-youwillsee/leetcode
lcof_055-2/cpp_055-2/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/11. // #ifndef CPP_055_2__SOLUTION2_H_ #define CPP_055_2__SOLUTION2_H_ #include "TreeNode.h" #include <math.h> #include <unordered_map> /** * 后序遍历 */ class Solution { public: bool isBalanced(TreeNode *node, int &depth) { if (!node) { depth = 0; r...
ooooo-youwillsee/leetcode
0637-Average-of-Levels-in-Binary-Tree/cpp_0637/Solution1.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0637_SOLUTION1_H #define CPP_0637_SOLUTION1_H #include "TreeNode.h" #include <queue> #include <vector> class Solution { public: vector<double> averageOfLevels(TreeNode *root) { if (!root) return {}; vector<double> res; queue<TreeNode *> q;...
ooooo-youwillsee/leetcode
0538-Convert-BST-to_Greater-Tree/cpp_0538/Solution2.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0538_SOLUTION2_H #define CPP_0538_SOLUTION2_H #include "TreeNode.h" class Solution { public: int sum = 0; void inOrder(TreeNode *node) { if (!node) return; inOrder(node->right); node->val += sum; sum = node->val; inO...
ooooo-youwillsee/leetcode
0268-Missing-Number/cpp_0268/Solution1.h
<filename>0268-Missing-Number/cpp_0268/Solution1.h // // Created by ooooo on 2020/1/6. // #ifndef CPP_0268_SOLUTION1_H #define CPP_0268_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int missingNumber(vector<int> &nums) { if (nums....
ooooo-youwillsee/leetcode
0033-Search-in-Rotated-Sorted-Array/cpp_0033/Solution1.h
// // Created by ooooo on 2020/2/9. // #ifndef CPP_0033__SOLUTION1_H_ #define CPP_0033__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 二分法 (一次) */ class Solution { public: int search(vector<int> &nums, int target) { if (nums.empty()) return -1; int left = 0, right = nums.s...
ooooo-youwillsee/leetcode
0136-Single-Number/cpp_0136/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2019/12/30. // #ifndef CPP_0136_SOLUTION1_H #define CPP_0136_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int singleNumber(vector<int> &nums) { unordered_map<int, int>...
ooooo-youwillsee/leetcode
0074-Search a 2D Matrix/cpp_0074/Solution1.h
/** * @author ooooo * @date 2021/3/30 16:09 */ #ifndef CPP_0074__SOLUTION1_H_ #define CPP_0074__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(), n = matrix[0].size(); int i =...
ooooo-youwillsee/leetcode
0068-Text Justification/cpp_0068/Solution1.h
/** * @author ooooo * @date 2021/2/13 14:48 */ #ifndef CPP_0068__SOLUTION1_H_ #define CPP_0068__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string convert(vector<string> &words, int maxWidth, int lastI, int i, bool lastLine) { int spaceCount = maxWidth; ...
ooooo-youwillsee/leetcode
lcp-18/cpp_018/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/9/27 11:23 */ #ifndef CPP_018__SOLUTION2_H_ #define CPP_018__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int breakfastNumber(vector<int> &staple, vector<int> &drinks, int x) { vector<int> prices(x + 1, 0);...
ooooo-youwillsee/leetcode
0518-Coin Change 2/cpp_0518/Solution2.h
/** * @author ooooo * @date 2021/2/17 19:22 */ #ifndef CPP_0518__SOLUTION2_H_ #define CPP_0518__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int dfs(int amount, vector<int> &coins, int i) { if (visited[amount][i]) { return memo[amount][i]; } if (amo...
ooooo-youwillsee/leetcode
1759-Count Number of Homogenous Substrings/cpp_1759/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/2/21 19:07 */ #ifndef CPP_1759__SOLUTION1_H_ #define CPP_1759__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: static const long MOD = 1000000007; int countHomogenous(string s) { s += "#"; int n = s.size()...
ooooo-youwillsee/leetcode
0125-Valid-Palindrome/cpp_0125/Solution2.h
// // Created by ooooo on 2020/1/7. // #ifndef CPP_0125_SOLUTION2_H #define CPP_0125_SOLUTION2_H #include <iostream> using namespace std; class Solution { public: bool isPalindrome(string s) { string ans = ""; for (auto &c: s) { if (isalnum(c)) ans += tolower(c); } ...
ooooo-youwillsee/leetcode
1818-Minimum Absolute Sum Difference/cpp_1818/Solution1.h
/** * @author ooooo * @date 2021/4/11 15:56 */ #ifndef CPP_1818__SOLUTION1_H_ #define CPP_1818__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
0746-Min-Cost-Climbing-Stairs/cpp_0746/Solution2.h
// // Created by ooooo on 2020/2/5. // #ifndef CPP_0746__SOLUTION2_H_ #define CPP_0746__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] = min(dp[i-1] , dp[i-2]) + p */ class Solution { public: int minCostClimbingStairs(vector<int> &cost) { int n = cost.size(), *dp = new ...
ooooo-youwillsee/leetcode
1031-Maximum Sum of Two Non-Overlapping Subarrays/cpp_1031/SolutIon2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2021/2/15 17:56 */ #ifndef CPP_1031__SOLUTION2_H_ #define CPP_1031__SOLUTION2_H_ #include <vector> using namespace std; class Solution { public: vector<int> preSum; int maxSum(vector<int> &A, int L, int M) { int n = A.size();...
ooooo-youwillsee/leetcode
1624-Largest Substring Between Two Equal Characters/cpp_1624/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/11/15 09:01 */ #ifndef CPP_1624__SOLUTION1_H_ #define CPP_1624__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <numeric> using namespace std; class Solution { publi...
ooooo-youwillsee/leetcode
0035-Search-Insert-Position/cpp_0035/Solution1.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0035_SOLUTION1_H #define CPP_0035_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: int searchInsert(vector<int> &nums, int target) { if (nums.empty() || target < nums[0]) return 0; if (target > nums[...
ooooo-youwillsee/leetcode
1004-Max Consecutive Ones III/cpp_1004/Solution2.h
/** * @author ooooo * @date 2021/2/19 09:53 */ #ifndef CPP_1004__SOLUTION2_H_ #define CPP_1004__SOLUTION2_H_ #include <iostream> #include <vector> #include <queue> using namespace std; class Solution { public: int longestOnes(vector<int> &A, int K) { int n = A.size(); int ans = 0; int l = 0, r = 0; in...
ooooo-youwillsee/leetcode
0993-Cousins-in-Binary-Tree/cpp_0993/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0993_SOLUTION1_H #define CPP_0993_SOLUTION1_H #include "TreeNode.h" #include <queue> /** * bfs */ class Solution { public: bool isCousins(TreeNode *root, int x, int y) { if (!root) return false; queue<TreeNode *> q; queu...
ooooo-youwillsee/leetcode
1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp_1577/Solution2.h
/** * @author ooooo * @date 2020/9/7 15:17 */ #ifndef CPP_1577__SOLUTION2_H_ #define CPP_1577__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution1 { public: int numTriplets(vector<int> &nums1, vector<int> &nums2) { int count = 0; unordered_map<...
ooooo-youwillsee/leetcode
0207-Course-Schedule/cpp_0207/Solution1.h
// // Created by ooooo on 2020/2/19. // #ifndef CPP_0207__SOLUTION1_H_ #define CPP_0207__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dfs + 邻接矩阵 */ class Solution { public: vector<vector<bool>> graph; // 1 表示已访问, -1 表示在上次 dfs 中已经访问过,说明这个节点没有问题, 0 表示没有访问过 vector<int> mark; ...
ooooo-youwillsee/leetcode
1720-Decode XORed Array/cpp_1720/Solution1.h
/** * @author ooooo * @date 2021/1/20 12:18 */ #ifndef CPP_1720__SOLUTION1_H_ #define CPP_1720__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> using namespace std; class Solution { public: vector<int> decode(vector<int> &...
ooooo-youwillsee/leetcode
lcof_046/cpp_046/Solution1.h
// // Created by ooooo on 2020/4/2. // #ifndef CPP_046__SOLUTION1_H_ #define CPP_046__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; 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; ...
ooooo-youwillsee/leetcode
1024-Video Stitching/cpp_1024/Solution1.h
/** * @author ooooo * @date 2020/10/24 09:55 */ #ifndef CPP_1024__SOLUTION1_H_ #define CPP_1024__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i][j] 表示i到j区间的最小数目 */ class Solution { public: int videoStitching(vector<vector<int>> &clips, int T) { vector<vector<int>> dp(T ...
ooooo-youwillsee/leetcode
0383-Ransom-Note/cpp_0383/Solution1.h
// // Created by ooooo on 2020/1/25. // #ifndef CPP_0383__SOLUTION1_H_ #define CPP_0383__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: bool canConstruct(string ransomNote, string magazine) { unordered_map<char, int> m1, m2; for (auto c: ransomNote)...
ooooo-youwillsee/leetcode
1705-Maximum Number of Eaten Apples/cpp_1705/Solution1.h
/** * @author ooooo * @date 2021/1/24 17:17 */ #ifndef CPP_1705__SOLUTION1_H_ #define CPP_1705__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; class Solution { public: int eatenApples(vector<int> &apples, vector<int> &days) { int n = apples.size(); priority_queue<...
ooooo-youwillsee/leetcode
0338-Counting Bits/cpp_0338/Solution2.h
// // Created by ooooo on 2019/12/5. // #ifndef CPP_0338_SOLUTION2_H #define CPP_0338_SOLUTION2_H #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> countBits(int num) { vector<int> res = vector<int>(num + 1, 0); for (int i = 1; i <= num; i++) { ...
ooooo-youwillsee/leetcode
0475-Heaters/cpp_0475/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/19. // #ifndef CPP_0475__SOLUTION1_H_ #define CPP_0475__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findRadius(vector<int> &houses, vector<int> &heaters) { vector<int> ans; sort(houses.begin(), house...
ooooo-youwillsee/leetcode
0039-Combination-Sum/cpp_0039/Solution1.h
// // Created by ooooo on 2020/2/10. // #ifndef CPP_0039__SOLUTION1_H_ #define CPP_0039__SOLUTION1_H_ #include <iostream> #include <vector> #include <set> #include <unordered_set> using namespace std; /** * vector#push_back() 会复制数据 * * 允许重复数字 --> i可以再次取到,利用排序保证结果不重复 */ class Solution { public: void help(vect...
ooooo-youwillsee/leetcode
0395-Longest Substring with At Least K Repeating Characters/cpp_0395/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0395-Longest Substring with At Least K Repeating Characters/cpp_0395/Solution1.h /** * @author ooooo * @date 2021/2/27 10:57 */ #ifndef CPP_0395__SOLUTION1_H_ #define CPP_0395__SOLUTION1_H_ #include <vector> #include <iostream> #include <unordered_map> using namespace...
ooooo-youwillsee/leetcode
0079-Word-Search/cpp_0079/Solution2.h
/** * @author ooooo * @date 2020/9/13 09:34 */ #ifndef CPP_0079__SOLUTION2_H_ #define CPP_0079__SOLUTION2_H_ #include <vector> #include <iostream> using namespace std; class Solution2 { public: vector<vector<char>> board; vector<vector<bool>> visited; vector<vector<int>> dx_dy = {{0, 1}, {0, -1}, {1, 0}, {...
ooooo-youwillsee/leetcode
0876-Middle-of-the-Linked-List/cpp_0876/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/23. // #ifndef CPP_0876__SOLUTION1_H_ #define CPP_0876__SOLUTION1_H_ #include "ListNode.h" /** * 快慢指针 */ class Solution { public: ListNode *middleNode(ListNode *head) { ListNode *slow = head, *fast = head; while (fast && fast->next) { slow = slow->n...