repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
0064-Minimum-Path-Sum/cpp_0064/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/13. // #ifndef CPP_0064__SOLUTION1_H_ #define CPP_0064__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i][j] = cur_num + min(dp[i-1][j] + dp[i][j-1]) */ class Solution { public: int minPathSum(vector<vector<int>> &grid) { if...
ooooo-youwillsee/leetcode
1817-Finding the Users Active Minutes/cpp_1817/Solution1.h
<filename>1817-Finding the Users Active Minutes/cpp_1817/Solution1.h /** * @author ooooo * @date 2021/4/11 15:54 */ #ifndef CPP_1817__SOLUTION1_H_ #define CPP_1817__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <nume...
ooooo-youwillsee/leetcode
1609-Even Odd Tree/cpp_1609/Solution1.h
<filename>1609-Even Odd Tree/cpp_1609/Solution1.h /** * @author ooooo * @date 2020/10/11 13:41 */ #ifndef CPP_1609__SOLUTION1_H_ #define CPP_1609__SOLUTION1_H_ #include "TreeNode.h" bool isEvenOddTree(TreeNode *root) { queue<TreeNode *> q; q.push(root); int level = 0; while (!q.empty()) { bool incr = level...
ooooo-youwillsee/leetcode
0572-Subtree-of-Another-Tree/cpp_0572/Solution1.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0572_SOLUTION1_H #define CPP_0572_SOLUTION1_H #include "TreeNode.h" class Solution { public: bool isSameTree(TreeNode *s, TreeNode *t) { if (!s && !t) return true; if (!s || !t) return false; return s->val == t->val && isSameTree(s->left,...
ooooo-youwillsee/leetcode
0049-Group-Anagrams/cpp_0049/Solution1.h
<filename>0049-Group-Anagrams/cpp_0049/Solution1.h // // Created by ooooo on 2020/2/11. // #ifndef CPP_0049__SOLUTION1_H_ #define CPP_0049__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 哈希表 */ class Solution { public: vector<vector<string>> groupAnagrams...
ooooo-youwillsee/leetcode
1816-Truncate Sentence/cpp_1816/Solution1.h
/** * @author ooooo * @date 2021/4/11 15:50 */ #ifndef CPP_1816__SOLUTION1_H_ #define CPP_1816__SOLUTION1_H_ #include <iostream> #include <sstream> using namespace std; class Solution { public: string truncateSentence(string s, int k) { istringstream in(s); string w; string ans = ""; while (k && (in >...
ooooo-youwillsee/leetcode
0098-Validate-Binary-Search-Tree/cpp_0098/Solution2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2019/11/1. // #ifndef CPP_0098_SOLUTION2_H #define CPP_0098_SOLUTION2_H #include <iostream> #include "TreeNode.h" using namespace std; /** * 比较左孩子的val和当前节点的值 */ class Solution { private: TreeNode *prev; bool help(TreeNode *node...
ooooo-youwillsee/leetcode
lcof_059-2/cpp_059-2/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/25. // #ifndef CPP_059_2__SOLUTION1_H_ #define CPP_059_2__SOLUTION1_H_ #include <iostream> #include <deque> #include <unordered_map> using namespace std; class MaxQueue { public: deque<int> q, help; MaxQueue() { q.clear(); help.clear(); } int max_v...
ooooo-youwillsee/leetcode
1221-Split-a-String-in-Balanced-Strings/cpp_1221/Solution1.h
// // Created by ooooo on 2020/2/3. // #ifndef CPP_1221__SOLUTION1_H_ #define CPP_1221__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: int balancedStringSplit(string s) { int R_count = 0, ans = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'R') R_count++; ...
ooooo-youwillsee/leetcode
0242-Valid-Anagram/cpp_0242/Solution2.h
// // Created by ooooo on 2019/11/1. // #ifndef CPP_0242_SOLUTION2_H #define CPP_0242_SOLUTION2_H #include <iostream> #include <string> #include <algorithm> #include <map> using namespace std; class Solution { public: bool isAnagram(string s, string t) { map<char, int> m1; map<char, int> m2; ...
ooooo-youwillsee/leetcode
0496-Next-Greater-Element-I/cpp_0496/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/5. // #ifndef CPP_0496_SOLUTION1_H #define CPP_0496_SOLUTION1_H #include <iostream> #include <vector> using namespace std; /** * 暴力破解 */ class Solution { public: vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2) { ...
ooooo-youwillsee/leetcode
0145-Binary-Tree-Postorder-Traversal/cpp_0145/TreeNode.h
<filename>0145-Binary-Tree-Postorder-Traversal/cpp_0145/TreeNode.h /** * @author ooooo * @date 2020/9/25 23:33 */ #ifndef CPP_0145__TREENODE_H_ #define CPP_0145__TREENODE_H_ #include <iostream> #include <vector> #include <stack> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right;...
ooooo-youwillsee/leetcode
0377-Combination Sum IV/cpp_0377/Solution1.h
<filename>0377-Combination Sum IV/cpp_0377/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/4/24 08:34 */ #ifndef CPP_0377__SOLUTION1_H_ #define CPP_0377__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int dfs(vector<int> ...
ooooo-youwillsee/leetcode
0073-Set Matrix Zeroes/cpp_0073/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/10/11 20:09 */ #ifndef CPP_0073__SOLUTION1_H_ #define CPP_0073__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: void setZeroes(vector<vector<int>> &matrix) { if (matrix.empty()) return; int m = matrix.size()...
ooooo-youwillsee/leetcode
0046-Permutations/cpp_0046/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/10. // #ifndef CPP_0046__SOLUTION1_H_ #define CPP_0046__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: void dfs() { if (marked.size() == nums.size()) { ans.pus...
ooooo-youwillsee/leetcode
lcof_033/cpp_033/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>lcof_033/cpp_033/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/3/21. // #ifndef CPP_033__SOLUTION1_H_ #define CPP_033__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool help(int l, int r) { if (l >= ...
ooooo-youwillsee/leetcode
0559-Maximum Depth of N-ary Tree/cpp_0559/Solution2.h
// // Created by ooooo on 2019/12/29. // #ifndef CPP_0599_SOLUTION2_H #define CPP_0599_SOLUTION2_H #include "Node.h" class Solution { public: int maxDepth(Node *root) { if (!root) return 0; int max = 0; for (int i = 0; i < root->children.size(); ++i) { int depth = maxDepth(root->children[i]); ...
ooooo-youwillsee/leetcode
0227-Basic Calculator II/cpp_0227/Solution1.h
/** * @author ooooo * @date 2021/3/11 09:25 */ #ifndef CPP_0227__SOLUTION1_H_ #define CPP_0227__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: void calc(stack<long long> &numStack, stack<char> &opStack) { int x = numStack.top(); numStack....
ooooo-youwillsee/leetcode
0021-Merge-Two-Sorted-Lists/cpp_0021/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/23. // #ifndef CPP_0021__SOLUTION2_H_ #define CPP_0021__SOLUTION2_H_ #include "ListNode.h" /** * 使用原先的节点 (效率高) */ class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *dummyHead = new ListNode(0), *cur = d...
ooooo-youwillsee/leetcode
0861-Score After Flipping Matrix/cpp_0861/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/12/7 16:07 */ #ifndef CPP_0861__SOLUTION2_H_ #define CPP_0861__SOLUTION2_H_ #include <iostream> #include <math.h> #include <vector> using namespace std; class Solution { public: int matrixScore(vector<vector<int>> &A) { int m = A.size(), n = A[0].size(); i...
ooooo-youwillsee/leetcode
0977-Squares-of-a-Sorted-Arrays/cpp_0977/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0977-Squares-of-a-Sorted-Arrays/cpp_0977/Solution1.h // // Created by ooooo on 2020/1/7. // #ifndef CPP_0977_SOLUTION1_H #define CPP_0977_SOLUTION1_H #include <iostream> #include <vector> #include <cmath> using namespace std; /** * sort */ class Solution { public: ...
ooooo-youwillsee/leetcode
0160-Intersection-of-Two-Linked-Lists/cpp_0160/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/23. // #ifndef CPP_0160__SOLUTION1_H_ #define CPP_0160__SOLUTION1_H_ #include "ListNode.h" #include <unordered_set> /** * 哈希表 */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { unordered_set<ListNode *> s; while ...
ooooo-youwillsee/leetcode
0590-N-ary-Tree-Postorder-Transversal/cpp_0590/Solution1.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0590_SOLUTION1_H #define CPP_0590_SOLUTION1_H #include "Node.h" class Solution { public: void dfs(Node *node, vector<int> &vec) { if (!node) return; for (auto child: node->children) { dfs(child, vec); } vec.push_back(n...
ooooo-youwillsee/leetcode
lcof_046/cpp_046/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/2. // #ifndef CPP_046__SOLUTION2_H_ #define CPP_046__SOLUTION2_H_ #include <iostream> #include <unordered_map> #include <vector> using namespace std; /** * dp[i] = dp[i+1] + ( valid ? dp[i+2] : 0 ) */ class Solution { public: int valid(int i) { if (i + 1 >...
ooooo-youwillsee/leetcode
0129-Sum Root to Leaf Numbers/cpp_0129/TreeNode.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/10/29 09:31 */ #ifndef CPP_0129__TREENODE_H_ #define CPP_0129__TREENODE_H_ #include <iostream> #include <vector> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(N...
ooooo-youwillsee/leetcode
lcof_007/cpp_007/Solution1.h
// // Created by ooooo on 2020/3/7. // #ifndef CPP_007__SOLUTION1_H_ #define CPP_007__SOLUTION1_H_ #include "TreeNode.h" #include <unordered_map> class Solution { public: TreeNode *dfs(TreeNode *node, int l, int r) { if (l > r) return nullptr; int root_value = preorder[i++]; int root_index = in_map[ro...
ooooo-youwillsee/leetcode
lcof_041/cpp_041/Solution2.h
// // Created by ooooo on 2020/3/26. // #ifndef CPP_041__SOLUTION2_H_ #define CPP_041__SOLUTION2_H_ #include <iostream> #include <queue> using namespace std; class MedianFinder { private: vector<int> nums; public: /** initialize your data structure here. */ MedianFinder() { } void addNum(int num) { ...
ooooo-youwillsee/leetcode
lcof_049/cpp_049/Solution3.h
<filename>lcof_049/cpp_049/Solution3.h // // Created by ooooo on 2020/4/6. // #ifndef CPP_049__SOLUTION3_H_ #define CPP_049__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * dp */ class Solution { public: int nthUglyNumber(int n) { vector<int> nums(n, 1); int count = 1, num...
ooooo-youwillsee/leetcode
lcof_010-2/cpp_010-2/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/9. // #ifndef CPP_010_2__SOLUTION1_H_ #define CPP_010_2__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[n] = dp[n-1] + dp[n-2] */ class Solution { public: int numWays(int n) { if (n ==0) return 1; if (...
ooooo-youwillsee/leetcode
1812-Determine Color of a Chessboard Square/cpp_1812/Solution1.h
/** * @author ooooo * @date 2021/4/11 07:57 */ #ifndef CPP_1812__SOLUTION1_H_ #define CPP_1812__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool squareIsWhite(string s) { return (s[1] - (s[0] - 'a')) % 2 == 0; } }; #endif //CPP_1812__SOLUTION1_H_
ooooo-youwillsee/leetcode
0461-Hamming-Distance/cpp_0461/Solution2.h
// // Created by ooooo on 2020/2/6. // #ifndef CPP_0461__SOLUTION2_H_ #define CPP_0461__SOLUTION2_H_ #include <iostream> #include <sstream> using namespace std; /** * ^操作, i & (i-1) --> 去掉最后一个1 */ class Solution { public: int hammingDistance(int x, int y) { int res = x ^y, ans = 0; while (res) { ...
ooooo-youwillsee/leetcode
1629-Slowest Key/cpp_1629/Solution1.h
/** * @author ooooo * @date 2020/11/15 09:39 */ #ifndef CPP_1629__SOLUTION1_H_ #define CPP_1629__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: char slowestKey(vector<int> &releaseTimes, string keysPressed) { ...
ooooo-youwillsee/leetcode
0917-Reverse-Only-Letters/cpp_0917/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/1. // #ifndef CPP_0917__SOLUTION1_H_ #define CPP_0917__SOLUTION1_H_ #include <iostream> using namespace std; /** * 双指针 */ class Solution { public: string reverseOnlyLetters(string S) { if (S.size() <= 1) return S; int left = 0, right =...
ooooo-youwillsee/leetcode
0017-Letter-Combinations-of-a-Phone-Number/cpp_0017/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/7. // #ifndef CPP_0017__SOLUTION1_H_ #define CPP_0017__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<string>> letters = { {"a", "b", "c"}, {"d", "e", "f"}, {"g",...
ooooo-youwillsee/leetcode
0120-Triangle/cpp_0120/Solution2.h
<filename>0120-Triangle/cpp_0120/Solution2.h // // Created by ooooo on 2019/12/16. // #ifndef CPP_0120_SOLUTION2_H #define CPP_0120_SOLUTION2_H #include <iostream> #include <vector> #include <climits> using namespace std; /** * 自底向上 * * res[i] = min (res[i] , res[j]) + res[row][i] */ class Solution { public: ...
ooooo-youwillsee/leetcode
0226-Invert-Binary-Tree/cpp_0226/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/9/15. // #ifndef CPP_0226_SOLUTION1_H #define CPP_0226_SOLUTION1_H #include "TreeNode.h" class Solution { public: TreeNode *invertTree(TreeNode *root) { if (!root) return nullptr; TreeNode *rightNode = root->right; root->right = invertTree(ro...
ooooo-youwillsee/leetcode
0018-4Sum/cpp_0018/Solution1.h
<filename>0018-4Sum/cpp_0018/Solution1.h // // Created by ooooo on 2019/11/1. // #ifndef CPP_0018_SOLUTION1_H #define CPP_0018_SOLUTION1_H #include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; /** * 两层循环, 加上双指针 */ class Solution { public: vector<vector<int>> fourSum(vec...
ooooo-youwillsee/leetcode
1473-Paint House III/cpp_1473/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/4 09:15 */ #ifndef CPP_1473__SOLUTION1_H_ #define CPP_1473__SOLUTION1_H_ #include <vector> #include <iostream> using namespace std; class Solution { public: /** dp[m][n][target] 表示第m个房子,涂第n个颜色,为target的neighborhood的值 */ int minCost(vector<int> &houses, ve...
ooooo-youwillsee/leetcode
0744-Find-Smallest-Letter-Greater-Than-Target/cpp_0744/Solution2.h
// // Created by ooooo on 2020/1/20. // #ifndef CPP_0744__SOLUTION2_H_ #define CPP_0744__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * loop */ class Solution { public: char nextGreatestLetter(vector<char> &letters, char target) { for (auto c: letters) { if (c > target)...
ooooo-youwillsee/leetcode
0605-Can-Place-Flowers/cpp_0605/Solution1.h
/** * @author ooooo * @date 2021/1/1 09:57 */ #ifndef CPP_0605__SOLUTION1_H_ #define CPP_0605__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool canPlaceFlowers(vector<int> &f, int n) { if (n == 0) return true; int x = f.size(); if (x == 1 && f[0] == 0...
ooooo-youwillsee/leetcode
lcof_031/cpp_031/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/3/19. // #ifndef CPP_031__SOLUTION1_H_ #define CPP_031__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: bool validateStackSequences(vector<int> &pushed, vector<int> &popped) { stack<int> s; fo...
ooooo-youwillsee/leetcode
0720-Longest-Word-in-Dictionary/cpp_0720/Solution1.h
<filename>0720-Longest-Word-in-Dictionary/cpp_0720/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/14. // #ifndef CPP_0720__SOLUTION1_H_ #define CPP_0720__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 超时 */ class Solution { public: string an...
ooooo-youwillsee/leetcode
0279-Perfect-Squares/cpp_0279/Solution1.h
// // Created by ooooo on 2020/2/23. // #ifndef CPP_0279__SOLUTION1_H_ #define CPP_0279__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * dfs timeout */ class Solution { public: void dfs(int n, int count) { if (n == 0) { ans = min(ans, count); ...
ooooo-youwillsee/leetcode
1356-Sort-Integers-by-The-Number-of-1-Bits/cpp_1356/Solution1.h
/** * @author ooooo * @date 2020/11/6 20:38 */ #ifndef CPP_1356__SOLUTION1_H_ #define CPP_1356__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> sortByBits(vector<int> &arr) { auto fn = [](int n) { int i = 0; while (n) { i++; n = n &...
ooooo-youwillsee/leetcode
0836-Rectangle-Overlap/cpp_0836/Solution1.h
/** * @author ooooo * @date 2020/9/30 10:19 */ #ifndef CPP_0836__SOLUTION1_H_ #define CPP_0836__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool isRectangleOverlap(vector<int> &rec1, vector<int> &rec2) { return !( rec1[2] <= rec2[0] || // left rec...
ooooo-youwillsee/leetcode
0117-Populating-Next-Right-Pointers-in-Each-Node-II/cpp_0117/Solution1.h
/** * @author ooooo * @date 2020/9/28 09:19 */ #ifndef CPP_0117__SOLUTION1_H_ #define CPP_0117__SOLUTION1_H_ #include "Node.h" class Solution { public: Node *connect(Node *root) { if (!root) return root; queue<Node *> q; q.push(root); vector<vector<Node *>> rows; while (!q.empty()) { vector<Node *...
ooooo-youwillsee/leetcode
0088-Merge-Sorted-Array/cpp_0088/Solution1.h
<filename>0088-Merge-Sorted-Array/cpp_0088/Solution1.h // // Created by ooooo on 2019/12/5. // #ifndef CPP_0088_SOLUTION1_H #define CPP_0088_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: void merge(vector<int> &nums1, int m, vector<int> &nums2, int n) { ...
ooooo-youwillsee/leetcode
1773-Count Items Matching a Rule/cpp_1773/Solution1.h
/** * @author ooooo * @date 2021/3/14 14:52 */ #ifndef CPP_1773__SOLUTION1_H_ #define CPP_1773__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <numeric> #include <queue> using namespace std; class Solution { public: ...
ooooo-youwillsee/leetcode
1546-Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/cpp_1546/Solution1.h
/** * @author ooooo * @date 2021/3/9 11:59 */ #ifndef CPP_1546__SOLUTION1_H_ #define CPP_1546__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; // timeout class Solution { public: int maxNonOverlapping(vector<int> &nums, int target) { int n = nums.size(); vector<int> preSum(n + 1);...
ooooo-youwillsee/leetcode
0160-Intersection-of-Two-Linked-Lists/cpp_0160/Solution2.h
// // Created by ooooo on 2020/1/23. // #ifndef CPP_0160__SOLUTION2_H_ #define CPP_0160__SOLUTION2_H_ #include "ListNode.h" #include <unordered_set> /** * 双指针 (循环走A-B) */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *pA = headA, *pB = headB; while (p...
ooooo-youwillsee/leetcode
0264-Ugly Number II/cpp_0264/Solution2.h
<filename>0264-Ugly Number II/cpp_0264/Solution2.h /** * @author ooooo * @date 2021/4/11 15:17 */ #ifndef CPP_0264__SOLUTION2_H_ #define CPP_0264__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: using ll = long long; int nthUglyNumber(int n) { vector<ll> dp...
ooooo-youwillsee/leetcode
0001-Two-Sum/cpp_0001/Solution1.h
#include <vector> #include <iostream> using namespace std; /** * 暴力破解法 */ class Solution1 { public: static vector<int> twoSum(vector<int> &nums, int target) { vector<int> result; for (int i = 0; i < nums.size(); ++i) { for (int j = 1; j < nums.size(); ++j) { if (num...
ooooo-youwillsee/leetcode
0403-Frog Jump/cpp_0403/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/4/30 19:57 */ #ifndef CPP_0403__SOLUTION1_H_ #define CPP_0403__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: /* * i 表示第几个位置, step 表示上一步的步数 */ bool dfs(int i, i...
ooooo-youwillsee/leetcode
1269-Number of Ways to Stay in the Same Place After Some Steps/cpp_1269/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/14 21:06 */ #ifndef CPP_1269__SOLUTION2_H_ #define CPP_1269__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: const int mod = 1e9 + 7; int numWays(int steps, int arrLen) { int maxJ = min(arrLen, steps); ve...
ooooo-youwillsee/leetcode
0554-Brick Wall/cpp_0554/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/2 08:14 */ #ifndef CPP_0554__SOLUTION1_H_ #define CPP_0554__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int leastBricks(vector<vector<int>> &wall) { unordered_map<int, int> m; ...
ooooo-youwillsee/leetcode
0279-Perfect-Squares/cpp_0279/Solution3.h
<filename>0279-Perfect-Squares/cpp_0279/Solution3.h // // Created by ooooo on 2020/2/23. // #ifndef CPP_0279__SOLUTION3_H_ #define CPP_0279__SOLUTION3_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; /** * dp[i] = min(dp[i], dp[i - j*j] + 1 ) */ class Solution { public: int numSq...
ooooo-youwillsee/leetcode
0307-Range-Sum-Query-Mutable/cpp_0307/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0307-Range-Sum-Query-Mutable/cpp_0307/Solution1.h // // Created by ooooo on 2020/2/2. // #ifndef CPP_0307__SOLUTION1_H_ #define CPP_0307__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class NumArray { private: template<typename T> struct Se...
ooooo-youwillsee/leetcode
1122-Relative-Sort-Array/cpp_1122/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/11/14 20:22 */ #ifndef CPP_1122__SOLUTION1_H_ #define CPP_1122__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> relativeSortArray(vector<int> &arr1, vector...
ooooo-youwillsee/leetcode
1319-Number of Operations to Make Network Connected/cpp_1319/Solution1.h
/** * @author ooooo * @date 2021/1/23 12:45 */ #ifndef CPP_1319__SOLUTION1_H_ #define CPP_1319__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: struct UF { int n; vector<int> p; UF(int n) : n(n), p(n) { for (int i = 0; i < n; ++i) { p[i] = i; }...
ooooo-youwillsee/leetcode
0167-Two-Sum-II-Input-array-is-sorted/cpp_0167/Solution1.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0167_SOLUTION1_H #define CPP_0167_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> twoSum(vector<int> &numbers, int target) { for (int i = 0, j = numbers.size() - 1; i < j;) { int sum...
ooooo-youwillsee/leetcode
0078-Subsets/cpp_0078/Solution3.h
/** * @author ooooo * @date 2020/9/20 12:14 */ #ifndef CPP_0078__SOLUTION3_H_ #define CPP_0078__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> ans; vector<int> nums; void dfs(vector<int> &num, int i) { ans.push_back(num); if (i == n...
ooooo-youwillsee/leetcode
1793-Maximum Score of a Good Subarray/cpp_1793/Solution1.h
/** * @author ooooo * @date 2021/3/28 12:14 */ #ifndef CPP_1793__SOLUTION1_H_ #define CPP_1793__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include <stack> #include <numeric> #include <queue> using namespace std; class Solution { public: ...
ooooo-youwillsee/leetcode
0083-Remove-Duplicates-from-Sorted-List/cpp_0083/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/23. // #ifndef CPP_0083__SOLUTION2_H_ #define CPP_0083__SOLUTION2_H_ #include "ListNode.h" /** * recursion */ class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if (!head) return nullptr; head->next = deleteDuplicates(head->next); r...
ooooo-youwillsee/leetcode
lcof_054/cpp_054/Solution1.h
// // Created by ooooo on 2020/4/11. // #ifndef CPP_054__SOLUTION1_H_ #define CPP_054__SOLUTION1_H_ #include "TreeNode.h" #include <vector> class Solution { public: void dfs(TreeNode *node) { if (!node) return; dfs(node->left); nums.push_back(node->val); dfs(node->right); } vector<int> nums; ...
ooooo-youwillsee/leetcode
0169-Majority-Element/cpp_0169/Solution1.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0169_SOLUTION1_H #define CPP_0169_SOLUTION1_H #include <vector> #include <iostream> #include <map> using namespace std; class Solution { public: int majorityElement(vector<int> &nums) { map<int, int> m; int size = nums.size(); for (auto ...
ooooo-youwillsee/leetcode
1108-Defanging-an-IP-Address/cpp_1108/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>1108-Defanging-an-IP-Address/cpp_1108/Solution1.h // // Created by ooooo on 2020/2/2. // #ifndef CPP_1108__SOLUTION1_H_ #define CPP_1108__SOLUTION1_H_ #include <iostream> #include <sstream> using namespace std; class Solution { public: string defangIPaddr(string addres...
ooooo-youwillsee/leetcode
0086-Partition List/cpp_0086/Solution1.h
/** * @author ooooo * @date 2021/1/4 16:16 */ #ifndef CPP_0086__SOLUTION1_H_ #define CPP_0086__SOLUTION1_H_ #include "ListNode.h" class Solution { public: ListNode *partition(ListNode *head, int x) { ListNode *dummyHead = new ListNode(-1); dummyHead->next = head; ListNode *cur = dummyHead; while (cur ...
ooooo-youwillsee/leetcode
0783-Minimum-Distance-Between-BST-Nodes/cpp_0783/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/5. // #ifndef CPP_0783_SOLUTION1_H #define CPP_0783_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { public: void dfs(TreeNode *node, vector<int> &vec) { if (!node) return; dfs(node->left, vec); vec.p...
ooooo-youwillsee/leetcode
0315-Count of Smaller Numbers After Self/cpp_0315/Solution2.h
/** * @author ooooo * @date 2020/11/27 13:54 */ #ifndef CPP_0315__SOLUTION2_H_ #define CPP_0315__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> nums, tmp, indexes, ans, tmp_indexes; void merge(int l, int mid, int r) { copy(nums.begin() + l, nu...
ooooo-youwillsee/leetcode
0409-Longest-Palindrome/cpp_0409/Solution1.h
<filename>0409-Longest-Palindrome/cpp_0409/Solution1.h // // Created by ooooo on 2020/1/11. // #ifndef CPP_0409__SOLUTION1_H_ #define CPP_0409__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: int longestPalindrome(string s) { if (s.empty()) return 0; ...
ooooo-youwillsee/leetcode
0300-Longest-Increasing-Subsequence/cpp_0300/Solution1.h
// // Created by ooooo on 2020/2/24. // #ifndef CPP_0300__SOLUTION1_H_ #define CPP_0300__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] = num[j] < num[i] ==> max( dp[i] , dp[j] + 1 ) */ class Solution { public: int lengthOfLIS(vector<int> &nums) { int n = nums.size(), ...
ooooo-youwillsee/leetcode
0119-Pascals-Triangle-II/cpp_0119/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/6. // #ifndef CPP_0119_SOLUTION1_H #define CPP_0119_SOLUTION1_H #include <iostream> #include <vector> using namespace std; /** * 动态规划 */ class Solution { public: vector<int> getRow2(int rowIndex) { if (rowIndex == 0) return {1}; ...
ooooo-youwillsee/leetcode
1237-Find-Positive-Integer-Solution-for-a-Given-Equation/cpp_1237/CustomFunction.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/22. // #ifndef CPP_1237__CUSTOMFUNCTION_H_ #define CPP_1237__CUSTOMFUNCTION_H_ #include <iostream> #include <vector> using namespace std; class CustomFunction { public: // Returns f(x, y) for any given positive integers x and y. // Note that f...
ooooo-youwillsee/leetcode
0034-Search-for-a-Range/cpp_0034/Solution2.h
<filename>0034-Search-for-a-Range/cpp_0034/Solution2.h // // Created by ooooo on 2020/2/9. // #ifndef CPP_0034__SOLUTION2_H_ #define CPP_0034__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 二分法 (找到左边界和右边界) */ class Solution { public: int findBound(vector<int> nums, int target, bo...
ooooo-youwillsee/leetcode
1813-Sentence Similarity III/cpp_1813/Solution1.h
/** * @author ooooo * @date 2021/4/11 15:31 */ #ifndef CPP_1813__SOLUTION1_H_ #define CPP_1813__SOLUTION1_H_ #include <iostream> #include <vector> #include <sstream> #include <queue> using namespace std; class Solution { public: bool areSentencesSimilar(string sentence1, string sentence2) { auto split = []...
ooooo-youwillsee/leetcode
0014-Longest-Common-Prefix/cpp_0014/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/24. // #ifndef CPP_0014__SOLUTION1_H_ #define CPP_0014__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string check(string s1, string s2) { int len = s1.size() < s2.size() ? s1.size() : s2.s...
ooooo-youwillsee/leetcode
0395-Longest Substring with At Least K Repeating Characters/cpp_0395/Solution2.h
/** * @author ooooo * @date 2021/2/27 11:48 */ #ifndef CPP_0395__SOLUTION2_H_ #define CPP_0395__SOLUTION2_H_ #include <vector> #include <iostream> #include <unordered_map> using namespace std; // 分治法 class Solution { public: int dfs(string &s, int l, int r, int k) { if (l > r) return 0; int n = r + 1; ...
ooooo-youwillsee/leetcode
1806-Minimum Number of Operations to Reinitialize a Permutation/cpp_1806/Solution1.h
<filename>1806-Minimum Number of Operations to Reinitialize a Permutation/cpp_1806/Solution1.h /** * @author ooooo * @date 2021/4/4 08:30 */ #ifndef CPP_1806__SOLUTION1_H_ #define CPP_1806__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <set> #include...
ooooo-youwillsee/leetcode
1684-Count the Number of Consistent Strings/cpp_1684/Solution1.h
/** * @author ooooo * @date 2020/12/20 13:03 */ #ifndef CPP_1684__SOLUTION1_H_ #define CPP_1684__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int countConsistentStrings(string allowed, vector<string> &words) { int ans = 0; unord...
ooooo-youwillsee/leetcode
lcof_040/cpp_040/Solution2.h
// // Created by ooooo on 2020/3/25. // #ifndef CPP_040__SOLUTION2_H_ #define CPP_040__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> getLeastNumbers(vector<int> &arr, int k) { if (k == 0) return {}; sort(arr.begin(), arr.end()); return vec...
ooooo-youwillsee/leetcode
1726-Tuple with Same Product/cpp_1726/Solution1.h
/** * @author ooooo * @date 2021/1/24 17:07 */ #ifndef CPP_1726__SOLUTION1_H_ #define CPP_1726__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <unordered_set> #include <set> using namespace std; class Solution { public: int tupleSameProduct(vector<int> &...
ooooo-youwillsee/leetcode
0477-Total Hamming Distance/cpp_0477/Solution1.h
/** * @author ooooo * @date 2021/5/28 11:13 */ #ifndef CPP_0477__SOLUTION1_H_ #define CPP_0477__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int totalHammingDistance(vector<int> &nums) { int ans = 0; int n = nums.size(); for (int i = 0; i < 32; i++) { ...
ooooo-youwillsee/leetcode
0219-Contains-Duplicate-II/cpp_0219/Solution1.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0219_SOLUTION1_H #define CPP_0219_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 存在 索引差为k 就返回true */ class Solution { public: bool containsNearbyDuplicate(vector<int> &nums, int k) { unordered_map<...
ooooo-youwillsee/leetcode
0206-Reverse-Linked-List/cpp_0206/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2019/10/29. // #ifndef CPP_0206_SOLUTION2_H #define CPP_0206_SOLUTION2_H #include <iostream> #include "ListNode.h" using namespace std; class Solution2 { public: ListNode *reverseList(ListNode *head) { if (head == NULL || head->next == NULL)...
ooooo-youwillsee/leetcode
0122-Best-Time-to-Buy-and-Sell-Stock-II/cpp_0122/Solution1.h
<filename>0122-Best-Time-to-Buy-and-Sell-Stock-II/cpp_0122/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2019/11/4. // #ifndef CPP_0122_SOLUTION1_H #define CPP_0122_SOLUTION1_H #include <iostream> #include <vector> using namespace std; /** * 贪心算法 */ class Solution { public: int maxProfit(vector<int> &p...
ooooo-youwillsee/leetcode
lcof_039/cpp_039/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/24. // #ifndef CPP_039__SOLUTION1_H_ #define CPP_039__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * hash */ class Solution { public: int majorityElement(vector<int> &nums) { unordere...
ooooo-youwillsee/leetcode
0347-Top-K-Frequent-Elements/cpp_0347/Solution2.h
<filename>0347-Top-K-Frequent-Elements/cpp_0347/Solution2.h<gh_stars>10-100 /** * @author ooooo * @date 2020/9/7 11:08 */ #ifndef CPP_0347__SOLUTION2_H_ #define CPP_0347__SOLUTION2_H_ #include <vector> #include <queue> #include <map> #include <iostream> using namespace std; class Solution { public: template ...
ooooo-youwillsee/leetcode
0104-Maximum-Depth-of-Binary-Tree/cpp_0104/Solution1.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0104_SOLUTION1_H #define CPP_0104_SOLUTION1_H #include "TreeNode.h" using namespace std; class Solution { private: int max = 0; void dfs(int level, TreeNode *node) { if (!node) return; if (level > max) max = level; dfs(level + 1,...
ooooo-youwillsee/leetcode
0441-Arranging-Coins/cpp_0441/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/19. // #ifndef CPP_0441__SOLUTION1_H_ #define CPP_0441__SOLUTION1_H_ #include <iostream> using namespace std; /** * loop */ class Solution { public: int arrangeCoins(int n) { long long sum = 0, i = 0; while (true) { if (sum == n)...
ooooo-youwillsee/leetcode
0989-Add to Array-Form of Integer/cpp_0989/Solution1.h
/** * @author ooooo * @date 2021/1/22 22:45 */ #ifndef CPP_0989__SOLUTION1_H_ #define CPP_0989__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> addToArrayForm(vector<int> &A, int K) { vector<int> ans; int carry = 0, i = A.size() - 1; while (K)...
ooooo-youwillsee/leetcode
0701-Insert-into-a-Binary-Search-Tree/cpp_0701/TreeNode.h
<reponame>ooooo-youwillsee/leetcode<filename>0701-Insert-into-a-Binary-Search-Tree/cpp_0701/TreeNode.h /** * @author ooooo * @date 2020/9/30 09:16 */ #ifndef CPP_0701__TREENODE_H_ #define CPP_0701__TREENODE_H_ #include <iostream> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right...
ooooo-youwillsee/leetcode
0053-Maximum-Subarray/cpp_0053/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/2/4. // #ifndef CPP_0053__SOLUTION1_H_ #define CPP_0053__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] 表示数组长度为i的最大连续数组 * dp[i] = nums[i] (dp[i-1] < 0) * dp[i] = dp[i-1] + nums[i] (dp[i-1] >= 0)...
ooooo-youwillsee/leetcode
0649-Dota2 Senate/cpp_0649/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/12/11 09:11 */ #ifndef CPP_0649__SOLUTION1_H_ #define CPP_0649__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; class Solution { public: string predictPartyVictory(string senate) { queue<int> R, D; ...
ooooo-youwillsee/leetcode
1642-Furthest Building You Can Reach/cpp_1642/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/6 16:09 */ #ifndef CPP_1642__SOLUTION1_H_ #define CPP_1642__SOLUTION1_H_ #include <iostream> #include <queue> #include <vector> using namespace std; // 堆 class Solution { public: int furthestBuilding(vector<int> &heights, int bricks, int l...
ooooo-youwillsee/leetcode
0024-Swap-Nodes-in-Pairs/cpp_0024/ListNode.h
// // Created by ooooo on 2019/10/30. // #ifndef CPP_0024_LISTNODE_H #define CPP_0024_LISTNODE_H #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; #endif //CPP_0024_LISTNODE_H
ooooo-youwillsee/leetcode
0303-Range-Sum-Query-Immutable/cpp_0303/Solution2.h
// // Created by ooooo on 2020/1/29. // #ifndef CPP_0303__SOLUTION2_H_ #define CPP_0303__SOLUTION2_H_ #include <iostream> #include <vector> /** * 线段树 */ using namespace std; class NumArray { private: int *tree; int *data; int size; public: NumArray(vector<int> &nums) { data = new int[nums.size()];...
ooooo-youwillsee/leetcode
1573-Number of Ways to Split a String/cpp_1573/Solution1.h
/** * @author ooooo * @date 2021/3/3 17:21 */ #ifndef CPP_1573__SOLUTION1_H_ #define CPP_1573__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: static constexpr int MOD = 1000000007; int numWays(string s) { long long n = s.size(); int cnt = 0; for (auto c...
ooooo-youwillsee/leetcode
0872-Leaf-Similar-Trees/cpp_0872/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0872_SOLUTION1_H #define CPP_0872_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { public: void dfs(TreeNode *node, vector<int> &vec) { if (!node) return; dfs(node->left, vec...
ooooo-youwillsee/leetcode
lcof_050/cpp_050/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/7. // #ifndef CPP_050__SOLUTION1_H_ #define CPP_050__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: char firstUniqChar(string s) { unordered_map<char, int> m; for (auto &c: s) { m[c]++; } ...
ooooo-youwillsee/leetcode
1870-Minimum Speed to Arrive on Time/cpp_1870/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/25 22:22 */ #ifndef CPP_1870__SOLUTION1_H_ #define CPP_1870__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int minSpeedOnTime(vector<int> &dist, double hour) { if (dist.size() - 1 >= hour) return -1; in...