repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
0027-Remove-Element/cpp_0027/Solution2.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_0027_SOLUTION2_H #define CPP_0027_SOLUTION2_H #include <vector> #include <iostream> using namespace std; class Solution { public: int removeElement(vector<int> &nums, int val) { if (nums.size() < 1) return 0; int i = 0, j = nums.size() - 1; ...
ooooo-youwillsee/leetcode
0860-Lemonade-Change/cpp_0860/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/9/30 10:57 */ #ifndef CPP_0860__SOLUTION1_H_ #define CPP_0860__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool lemonadeChange(vector<int> &bills) { int bill_5 = 0, bill_10 = 0; for (auto &a :bills) { ...
ooooo-youwillsee/leetcode
lcof_045/cpp_045/Solution1.h
// // Created by ooooo on 2020/4/1. // #ifndef CPP_045__SOLUTION1_H_ #define CPP_045__SOLUTION1_H_ #include <iostream> #include <vector> #include <sstream> using namespace std; /** * 和 leetcode 不一样 */ class Solution { public: bool static compareTo(string &s1, string &s2) { int len1 = s1.size(), len2 = s2.s...
ooooo-youwillsee/leetcode
0116-Populating Next Right Pointers in Each Node/cpp_0116/Solution1.h
<filename>0116-Populating Next Right Pointers in Each Node/cpp_0116/Solution1.h /** * @author ooooo * @date 2020/10/15 19:35 */ #ifndef CPP_0116__SOLUTION1_H_ #define CPP_0116__SOLUTION1_H_ #include "Node.h" class Solution { public: Node *getNode(Node *node) { if (!node) return nullptr; if (node->left) re...
ooooo-youwillsee/leetcode
lcof_024/cpp_024/Solution1.h
// // Created by ooooo on 2020/3/16. // #ifndef CPP_024__SOLUTION1_H_ #define CPP_024__SOLUTION1_H_ #include "ListNode.h" class Solution { public: ListNode *reverseList(ListNode *head) { ListNode *cur = head, *prev = nullptr; while (cur) { ListNode *node = cur; cur = node->next; node->ne...
ooooo-youwillsee/leetcode
0976-Largest Perimeter Triangle/cpp_0976/Solution1.h
/** * @author ooooo * @date 2020/11/29 09:20 */ #ifndef CPP_0976__SOLUTION1_H_ #define CPP_0976__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int largestPerimeter(vector<int> &A) { sort(A.begin(), A.end()); for (int i = (int) A.size() - 1; i >= 2; --i) {...
ooooo-youwillsee/leetcode
0240-Search-a-2D-Matrix-II/cpp_0240/Solution1.h
// // Created by ooooo on 2020/2/22. // #ifndef CPP_0240__SOLUTION1_H_ #define CPP_0240__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool searchMatrix(vector<vector<int>> &matrix, int target) { if (matrix.empty()) return false; int m = matrix.size(), n ...
ooooo-youwillsee/leetcode
0376-Wiggle Subsequence/cpp_0376/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/12/12 11:31 */ #ifndef CPP_0376__SOLUTION1_H_ #define CPP_0376__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int wiggleMaxLength(vector<int> &nums) { int n = nums.size(); if (n <= 1) return n; vector<in...
ooooo-youwillsee/leetcode
0322-Coin-Change/cpp_0322/Solution3.h
// // Created by ooooo on 2020/2/26. // #ifndef CPP_0322__SOLUTION3_H_ #define CPP_0322__SOLUTION3_H_ #include <iostream> #include <vector> #include <queue> using namespace std; /** * bfs timeout */ class Solution { public: int coinChange(vector<int> &coins, int amount) { if (amount == 0) return 0; sor...
ooooo-youwillsee/leetcode
0416-Partition-Equal-Subset-Sum/cpp_0416/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/29. // #ifndef CPP_0416__SOLUTION2_H_ #define CPP_0416__SOLUTION2_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; /** * dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]] * * 背包问题 */ class Solution { public: bool canPartition(vector<...
ooooo-youwillsee/leetcode
lcof_065/cpp_065/Solution1.h
<filename>lcof_065/cpp_065/Solution1.h // // Created by ooooo on 2020/4/23. // #ifndef CPP_065__SOLUTION1_H_ #define CPP_065__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: int add(int a, int b) { int num1 = 0, num2 = 0; do { num1 = a ^ b; // 不进位 num2 = (unsigne...
ooooo-youwillsee/leetcode
0888-Fair-Candy-Swap/cpp_0888/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/30 15:42 */ #ifndef CPP_0888__SOLUTION1_H_ #define CPP_0888__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <numeric> using namespace std; class Solution { public: vector<int> fairCandySwap(vector<int>...
ooooo-youwillsee/leetcode
0746-Min-Cost-Climbing-Stairs/cpp_0746/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0746-Min-Cost-Climbing-Stairs/cpp_0746/Solution1.h // // Created by ooooo on 2020/2/5. // #ifndef CPP_0746__SOLUTION1_H_ #define CPP_0746__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] = min(dp[i-1] , dp[i-2]) + p (从第三个开始走) */ class...
ooooo-youwillsee/leetcode
0765-Couples Holding Hands/cpp_0765/Solution2.h
<filename>0765-Couples Holding Hands/cpp_0765/Solution2.h /** * @author ooooo * @date 2021/2/14 09:43 */ #ifndef CPP_0765__SOLUTION2_H_ #define CPP_0765__SOLUTION2_H_ #include <vector> #include <iostream> using namespace std; class Solution { public: class UF { public: vector<int> p; int n; UF(int n...
ooooo-youwillsee/leetcode
0200-Number-of-Islands/cpp_0200/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/19. // #ifndef CPP_0200__SOLUTION1_H_ #define CPP_0200__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dfs */ class Solution { public: void dfs(int i, int j) { if (i < 0 || i >= m || j < 0 || j >= n || mark[i][j])return; m...
ooooo-youwillsee/leetcode
lcci_17_12/cpp_17_12/Solution2.h
/** * @author ooooo * @date 2021/4/2 18:20 */ #ifndef CPP_17_12__SOLUTION2_H_ #define CPP_17_12__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int trap(vector<int> &height) { int n = height.size(); vector<int> left(n, 0), right(n, 0); for (int i = 1; i ...
ooooo-youwillsee/leetcode
1801-Number of Orders in the Backlog/cpp_1801/Solution1.h
/** * @author ooooo * @date 2021/3/28 12:18 */ #ifndef CPP_1801__SOLUTION1_H_ #define CPP_1801__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
0389-Find-the-Difference/cpp_0389/Solution1.h
// // Created by ooooo on 2020/1/11. // #ifndef CPP_0389__SOLUTION1_H_ #define CPP_0389__SOLUTION1_H_ #include <iostream> using namespace std; /** * sort */ class Solution { public: char findTheDifference(string s, string t) { sort(s.begin(), s.end()); sort(t.begin(), t.end()); for (int i = 0; i < s...
ooooo-youwillsee/leetcode
0224-Basic Calculator/cpp_0224/Solution1.h
<filename>0224-Basic Calculator/cpp_0224/Solution1.h ]/** * @author ooooo * @date 2021/3/10 11:52 */ #ifndef CPP_0224__SOLUTION1_H_ #define CPP_0224__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: void calc(stack<long long> &numStack, stack<c...
ooooo-youwillsee/leetcode
1482-Minimum Number of Days to Make m Bouquets/cpp_1482/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/9 17:35 */ #ifndef CPP_1482__SOLUTION1_H_ #define CPP_1482__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool check(vector<int> &bloomDay, int m, int k, int target) { int cnt = 0, cur_cnt = 0; for (int...
ooooo-youwillsee/leetcode
0169-Majority-Element/cpp_0169/Solution2.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0169_SOLUTION2_H #define CPP_0169_SOLUTION2_H #include <vector> #include <iostream> using namespace std; class Solution { public: int majorityElement(vector<int> &nums) { sort(nums.begin(), nums.end()); int size = nums.size(); if (size ...
ooooo-youwillsee/leetcode
lcof_018/cpp_018/ListNode.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/13. // #ifndef CPP_018__LISTNODE_H_ #define CPP_018__LISTNODE_H_ #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} ListNode(vector<int> nums) { ...
ooooo-youwillsee/leetcode
0345-Reverse-Vowels-of-a-String/cpp_0345/Solution1.h
<filename>0345-Reverse-Vowels-of-a-String/cpp_0345/Solution1.h // // Created by ooooo on 2020/1/8. // #ifndef CPP_0345_SOLUTION1_H #define CPP_0345_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: vector<char> vowels = {'a', 'e', 'i', 'o', 'u'}; bool isVowel(c...
ooooo-youwillsee/leetcode
0082-Remove Duplicates from Sorted List II/cpp_0082/Solution1.h
/** * @author ooooo * @date 2021/3/25 13:00 */ #ifndef CPP_0082__SOLUTION1_H_ #define CPP_0082__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode *deleteDupl...
ooooo-youwillsee/leetcode
0015-3Sum/cpp_0015/Solution2.h
// // Created by ooooo on 2019/10/31. // #ifndef CPP_0015_SOLUTION2_H #define CPP_0015_SOLUTION2_H #include <vector> #include <set> #include <algorithm> using namespace std; class Solution { public: vector<vector<int>> threeSum(vector<int> &nums) { sort(nums.begin(), nums.end()); vector<vector<i...
ooooo-youwillsee/leetcode
0718-Maximum-Length-of-Repeated-Subarray/cpp_0718/Solution1.h
/** * @author ooooo * @date 2020/10/5 13:13 */ #ifndef CPP_0718__SOLUTION1_H_ #define CPP_0718__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 二维数组 */ class Solution { public: int findLength(vector<int> &A, vector<int> &B) { int m = A.size(), n = B.size(); int ans = 0; v...
ooooo-youwillsee/leetcode
lcof_058-1/cpp_058-1/Solution2.h
<filename>lcof_058-1/cpp_058-1/Solution2.h<gh_stars>10-100 // // Created by ooooo on 2020/4/18. // #ifndef CPP_058_1__SOLUTION2_H_ #define CPP_058_1__SOLUTION2_H_ #include <iostream> #include <vector> #include <sstream> using namespace std; class Solution { public: void reverse(string &s, int i, int j) { whi...
ooooo-youwillsee/leetcode
0438-Find-All-Anagrams-in-a-String/cpp_0438/Solution2.h
// // Created by ooooo on 2020/3/1. // #ifndef CPP_0438__SOLUTION2_H_ #define CPP_0438__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 滑动窗口 */ class Solution { public: unordered_map<char, int> window, needs; vector<int> findAnagrams(string s, string p)...
ooooo-youwillsee/leetcode
0125-Valid-Palindrome/cpp_0125/Solution1.h
// // Created by ooooo on 2020/1/7. // #ifndef CPP_0125_SOLUTION1_H #define CPP_0125_SOLUTION1_H #include <iostream> using namespace std; class Solution { public: bool isPalindrome(string s) { if (s.size() <= 1) return true; int left = 0, right = s.size() - 1; while (left < right) { ...
ooooo-youwillsee/leetcode
0219-Contains-Duplicate-II/cpp_0219/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/6. // #ifndef CPP_0219_SOLUTION2_H #define CPP_0219_SOLUTION2_H #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * 滑动窗口 (窗口大小为k) */ class Solution { public: bool containsNearbyDuplicate(vector<int> &num...
ooooo-youwillsee/leetcode
1791-Find Center of Star Graph/cpp_1791/Solution1.h
/** * @author ooooo * @date 2021/3/28 12:11 */ #ifndef CPP_1791__SOLUTION1_H_ #define CPP_1791__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
0215-Kth-Largest-Element-in-an-Array/cpp_0215/Solution1.h
<filename>0215-Kth-Largest-Element-in-an-Array/cpp_0215/Solution1.h // // Created by ooooo on 2020/2/20. // #ifndef CPP_0215__SOLUTION1_H_ #define CPP_0215__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * sort */ class Solution { public: int findKthLargest(vector<int> &nums, int k)...
ooooo-youwillsee/leetcode
0231-Power of Two/cpp_0231/Solution1.h
// // Created by ooooo on 2019/12/5. // #ifndef CPP_0231_SOLUTION1_H #define CPP_0231_SOLUTION1_H #include <iostream> using namespace std; class Solution { public: bool isPowerOfTwo(int n) { return n > 0 && (n & n - 1) == 0; } }; #endif //CPP_0231_SOLUTION1_H
ooooo-youwillsee/leetcode
1031-Maximum Sum of Two Non-Overlapping Subarrays/cpp_1031/Solution1.h
<filename>1031-Maximum Sum of Two Non-Overlapping Subarrays/cpp_1031/Solution1.h /** * @author ooooo * @date 2021/2/15 17:39 */ #ifndef CPP_1031__SOLUTION1_H_ #define CPP_1031__SOLUTION1_H_ #include <vector> using namespace std; /** * 滑动窗口 */ class Solution { public: int maxSum(vector<int> &A, int l, int r...
ooooo-youwillsee/leetcode
lcof_043/cpp_043/Solution1.h
<filename>lcof_043/cpp_043/Solution1.h // // Created by ooooo on 2020/3/28. // #ifndef CPP_043__SOLUTION1_H_ #define CPP_043__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * timeout */ class Solution { public: int count1(int n) { int count = 0; while (n) { if (n % 10...
ooooo-youwillsee/leetcode
0002-Add-Two-Numbers/cpp_0002/ListNode.h
/** * @author ooooo * @date 2020/10/4 12:42 */ #ifndef CPP_0002__LISTNODE_H_ #define CPP_0002__LISTNODE_H_ #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; #endif //CPP_0002__LISTNODE_H_
ooooo-youwillsee/leetcode
0714-Best Time to Buy and Sell Stock with Transaction Fee/cpp_0714/Solution1.h
/** * @author ooooo * @date 2020/12/19 19:06 */ #ifndef CPP_0714__SOLUTION1_H_ #define CPP_0714__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxProfit(vector<int> &prices, int fee) { int n = prices.size(); vector<vector<int>> dp(n + 1, vector<int>(2...
ooooo-youwillsee/leetcode
lcof_064/cpp_064/Solution1.h
// // Created by ooooo on 2020/4/21. // #ifndef CPP_064__SOLUTION1_H_ #define CPP_064__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: int sumNums(int n) { bool f = n > 1 && (n += sumNums(n - 1)); return n; } }; #endif //CPP_064__SOLUTION1_H_
ooooo-youwillsee/leetcode
0115-Distinct Subsequences/cpp_0115/Solution1.h
/** * @author ooooo * @date 2021/3/17 18:04 */ #ifndef CPP_0115__SOLUTION1_H_ #define CPP_0115__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <numeric> using namespace std; class Solution { public: // dp[i][j] 表示以s中j位置结尾的子序列个数 int numDistinct(s...
ooooo-youwillsee/leetcode
0830-Positions-of-Large-Groups/cpp_0830/Solution1.h
/** * @author ooooo * @date 2020/9/30 09:30 */ #ifndef CPP_0830__SOLUTION1_H_ #define CPP_0830__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> largeGroupPositions(string s) { vector<vector<int>> ans; int prev = 0, cur = 0; while (cur ...
ooooo-youwillsee/leetcode
1805-Number of Different Integers in a String/cpp_1805/Solution1.h
/** * @author ooooo * @date 2021/4/4 08:28 */ #ifndef CPP_1805__SOLUTION1_H_ #define CPP_1805__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
lcci_17_13/cpp_17_13/Solution3.h
/** * @author ooooo * @date 2020/10/11 10:29 */ #ifndef CPP_17_13__SOLUTION3_H_ #define CPP_17_13__SOLUTION3_H_ #include <iostream> #include <vector> #include <unordered_set> #include <set> #include <unordered_map> #include <stack> #include <queue> using namespace std; class Solution { public: // 5535 https:...
ooooo-youwillsee/leetcode
0034-Search-for-a-Range/cpp_0034/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0034-Search-for-a-Range/cpp_0034/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/2/9. // #ifndef CPP_0034__SOLUTION1_H_ #define CPP_0034__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 二分法 */ class Solution { public: vector<i...
ooooo-youwillsee/leetcode
0115-Distinct Subsequences/cpp_0115/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/17 19:18 */ #ifndef CPP_0115__SOLUTION2_H_ #define CPP_0115__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <numeric> using namespace std; class Solution { public: int numDist...
ooooo-youwillsee/leetcode
0098-Validate-Binary-Search-Tree/cpp_0098/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2019/11/1. // #ifndef CPP_0098_SOLUTION1_H #define CPP_0098_SOLUTION1_H #include <iostream> #include <vector> #include "TreeNode.h" using namespace std; /** * 对于二叉搜索树, 中序遍历是有序的 */ class Solution { private: void help(TreeNode *node, vector<int> &vec...
ooooo-youwillsee/leetcode
0078-Subsets/cpp_0078/Solution1.h
// // Created by ooooo on 2020/2/14. // #ifndef CPP_0078__SOLUTION1_H_ #define CPP_0078__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 回溯 */ class Solution { public: void dfs(int cur_count, int count, int index) { if (cur_count == count) { ans.push_back(num); ret...
ooooo-youwillsee/leetcode
0852-Peak-Index-in-a-Mountain-Array/cpp_0852/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/22. // #ifndef CPP_0852__SOLUTION1_H_ #define CPP_0852__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int peakIndexInMountainArray(vector<int> &A) { // left和right 就是要搜索的区间 int left = 1, right = A.size() - ...
ooooo-youwillsee/leetcode
lcof_003/cpp_003/Solution2.h
<filename>lcof_003/cpp_003/Solution2.h<gh_stars>10-100 // // Created by ooooo on 2020/3/5. // #ifndef CPP_003__SOLUTION2_H_ #define CPP_003__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * sort */ class Solution { public: int findRepeatNumber(vector<int> &nums) { sort(nums.begi...
ooooo-youwillsee/leetcode
0033-Search-in-Rotated-Sorted-Array/cpp_0033/Solution2.h
// // Created by ooooo on 2020/2/9. // #ifndef CPP_0033__SOLUTION2_H_ #define CPP_0033__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 二分法 (两次, 第一次查找分隔点,第二次查找目标值) ??? */ class Solution { public: int search(vector<int> &nums, int target) { int left = 0, right = nums.size() - 1;...
ooooo-youwillsee/leetcode
0685-Redundant-Connection-II/cpp_0685/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/9/17 18:47 */ #ifndef CPP_0685__SOLUTION1_H_ #define CPP_0685__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution1 { public: int find(vector<int> uf, int i) { while (uf[i] != i) { i = uf[i]; } return i; } vector...
ooooo-youwillsee/leetcode
0877-Stone Game/cpp_0877/Solution1.h
// // Created by ooooo on 6/16/2021. // #ifndef CPP_0877_SOLUTION1_H #define CPP_0877_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: bool stoneGame(vector<int> &piles) { int n = piles.size(); // dp[i][j] 表示 i 到 j 之间的差值 vector<vector<int>> dp(n, ...
ooooo-youwillsee/leetcode
0139-Word-Break/cpp_0139/Solution1.h
<filename>0139-Word-Break/cpp_0139/Solution1.h // // Created by ooooo on 2020/2/17. // #ifndef CPP_0139__SOLUTION1_H_ #define CPP_0139__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <set> using namespace std; /** * 超时 */ class Solution { public: unordered_set<string> word...
ooooo-youwillsee/leetcode
1769-Minimum Number of Operations to Move All Balls to Each Box/cpp_1769/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/2/28 13:21 */ #ifndef CPP_1769__SOLUTION1_H_ #define CPP_1769__SOLUTION1_H_ #include <vector> #include <iostream> using namespace std; class Solution { vector<int> minOperations(string boxes) { int n = boxes.size(); vector<int> left(n, 0), right(n, 0); ...
ooooo-youwillsee/leetcode
lcof_010-1/cpp_010-1/Solution3.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/8. // #ifndef CPP_010_1__SOLUTION3_H_ #define CPP_010_1__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * F(N) = F(N - 1) + F(N - 2) * * dp * * O(n) */ class Solution { public: /*int fib(int n) { if (n <= 1...
ooooo-youwillsee/leetcode
0064-Minimum-Path-Sum/cpp_0064/Solution2.h
// // Created by ooooo on 2020/2/13. // #ifndef CPP_0064__SOLUTION2_H_ #define CPP_0064__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 暴力 dfs */ class Solution { public: void dfs(int i, int j, int sum) { if (i == m || j == n) return; sum += grid[i][j]; if (i == m - 1...
ooooo-youwillsee/leetcode
lcof_053-2/cpp_053-2/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/4/10. // #ifndef CPP_053_2__SOLUTION1_H_ #define CPP_053_2__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int missingNumber(vector<int> &nums) { vector<bool> marked(nums.size()...
ooooo-youwillsee/leetcode
0148-Sort-List/cpp_0148/Solution1.h
// // Created by ooooo on 2020/2/18. // #ifndef CPP_0148__SOLUTION1_H_ #define CPP_0148__SOLUTION1_H_ #include "ListNode.h" /** * 复杂度要求 O(nlogn) ==> 归并排序 * recursion */ class Solution { public: ListNode *sortList(ListNode *head) { if (!head || !head->next) return head; ListNode *low = head, *fast = head...
ooooo-youwillsee/leetcode
1656-Design an Ordered Stream/cpp_1656/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/1/24 17:58 */ #ifndef CPP_1656__SOLUTION1_H_ #define CPP_1656__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack> using namespace std; class OrderedStream { p...
ooooo-youwillsee/leetcode
0525-Contiguous Array/cpp_0525/Solution1.h
<filename>0525-Contiguous Array/cpp_0525/Solution1.h /** * @author ooooo * @date 2021/6/3 10:41 */ #ifndef CPP_0525__SOLUTION1_H_ #define CPP_0525__SOLUTION1_H_ #include <vector> #include <iostream> #include <unordered_map> using namespace std; class Solution { public: // sum[i] 表示 0 到 i 的前缀和 // sum[j] - su...
ooooo-youwillsee/leetcode
1686-Stone Game VI/cpp_1686/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/4/11 16:46 */ #ifndef CPP_1686__SOLUTION1_H_ #define CPP_1686__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int stoneGameVI(vector<int> &aliceValues, vector<int> &bobValues) { int n = aliceValues.size(); ...
ooooo-youwillsee/leetcode
1022-Sum-of-Root-To-Leaf-Binary-Numbers/cpp_1022/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/5. // #ifndef CPP_1022_SOLUTION1_H #define CPP_1022_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { public: vector<string> nums; void dfs(TreeNode *node, string s) { if (!node) return; s += to_string(no...
ooooo-youwillsee/leetcode
0394-Decode-String/cpp_0394/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/27. // #ifndef CPP_0394__SOLUTION1_H_ #define CPP_0394__SOLUTION1_H_ #include <iostream> #include <stack> #include <sstream> using namespace std; /** * stack */ class Solution { public: string decodeString(string s) { stack<char> stack; string ans = "", ...
ooooo-youwillsee/leetcode
0121-Best-Time-to-Buy-and-Sell-Stock/cpp_0121/Solution2.h
// // Created by ooooo on 2020/2/5. // #ifndef CPP_0121__SOLUTION2_H_ #define CPP_0121__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 动态规划 * * i: 第几天, k:第几次交易 * dp[i][k][0] = max(dp[i-1][k][1] + p , dp[i-1][k][0]) * dp[i][k][1] = max(dp[i-1][k-1][0] - p , dp[i-1][k][1]) */ class...
ooooo-youwillsee/leetcode
1310-XOR Queries of a Subarray/cpp_1310/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/12 19:44 */ #ifndef CPP_1310__SOLUTION1_H_ #define CPP_1310__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: struct SegmentTree { vector<int> tree; vector<int> arr; SegmentTree(vector<int> arr) { tr...
ooooo-youwillsee/leetcode
0114-Flatten-Binary-Tree-to-Linked-List/cpp_0114/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/16. // #ifndef CPP_0114__SOLUTION2_H_ #define CPP_0114__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: TreeNode *prev = nullptr; void flatten(TreeNode *root) { if (!root) return; flatten(root->right); flatten(root->left); root->right...
ooooo-youwillsee/leetcode
0073-Set Matrix Zeroes/cpp_0073/Solution2.h
/** * @author ooooo * @date 2020/10/11 20:13 */ #ifndef CPP_0073__SOLUTION2_H_ #define CPP_0073__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; // 用第一行和第一列存储 class Solution { public: void setZeroes(vector<vector<int>> &matrix) { if (matrix.empty()) return; int m = matrix.size(), n...
ooooo-youwillsee/leetcode
0051-N-Queens/cpp_0051/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2019/11/14. // #ifndef CPP_0051_SOLUTION1_H #define CPP_0051_SOLUTION1_H #include <iostream> #include <vector> #include <set> using namespace std; class Solution { private: vector<int> result; void solve(vector<vector<string>> &res, ...
ooooo-youwillsee/leetcode
lcci_17_12/cpp_17_12/Solution1.h
/** * @author ooooo * @date 2021/4/2 18:20 */ #ifndef CPP_17_12__SOLUTION1_H_ #define CPP_17_12__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int trap(vector<int> &height) { int n = height.size(); int ans = 0; for (int i = 0; i < n; i++) { int l = 0...
ooooo-youwillsee/leetcode
0107-Binary-Tree-Level-Order-Traversal-II/cpp_0107/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/6 01:06 */ #ifndef CPP_0107__SOLUTION2_H_ #define CPP_0107__SOLUTION2_H_ #include <iostream> #include <vector> #include "TreeNode.h" #include <queue> using namespace std; class Solution2 { public: vector<vector<int>> levelOrderBottom(Tree...
ooooo-youwillsee/leetcode
1758-Minimum Changes To Make Alternating Binary String/cpp_1758/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/2/21 19:05 */ #ifndef CPP_1758__SOLUTION1_H_ #define CPP_1758__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int help(string s) { int count = 0; for (int i = 1; i < s.size(); ++i) { if (s[i] == s[i - 1]...
ooooo-youwillsee/leetcode
0628-Maximum Product of Three Numbers/cpp_0628/Solution1.h
/** * @author ooooo * @date 2021/1/20 12:08 */ #ifndef CPP_0628__SOLUTION1_H_ #define CPP_0628__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maximumProduct(vector<int> &nums) { int n = nums.size(); sort(nums.begin(), nums.end()); return max(nums[n ...
ooooo-youwillsee/leetcode
lcof_052/cpp_052/Solution2.h
<filename>lcof_052/cpp_052/Solution2.h // // Created by ooooo on 2020/4/9. // #ifndef CPP_052__SOLUTION2_H_ #define CPP_052__SOLUTION2_H_ #include "ListNode.h" class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *nodeA = headA, *nodeB = headB; while (nodeA &&...
ooooo-youwillsee/leetcode
0026-Remove-Duplicates-from-Sorted-Array/cpp_0026/Solution1.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_0026_SOLUTION1_H #define CPP_0026_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: int removeDuplicates(vector<int> &nums) { if (nums.size() <= 1) return nums.size(); int i = 0; for (int j = ...
ooooo-youwillsee/leetcode
0066-Plus-One/cpp_0066/Solution1.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0066_SOLUTION1_H #define CPP_0066_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> plusOne(vector<int> &digits) { if (digits.empty()) return {}; for (int i = digits.size() - 1; i >= 0; -...
ooooo-youwillsee/leetcode
0104-Maximum-Depth-of-Binary-Tree/cpp_0104/Solution2.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0104_SOLUTION2_H #define CPP_0104_SOLUTION2_H #include "TreeNode.h" using namespace std; class Solution { public: int maxDepth(TreeNode *root) { if (!root) return 0; return 1 + max(maxDepth(root->left), maxDepth(root->right)); } }; #endif...
ooooo-youwillsee/leetcode
0127-Word-Ladder/cpp_0127/Solution1.h
// // Created by ooooo on 2019/12/30. // #ifndef CPP_0127_SOLUTION1_H #define CPP_0127_SOLUTION1_H #include <iostream> #include <string> #include <climits> #include <vector> #include <unordered_map> using namespace std; /** * note: 超时 */ class Solution { public: int min = INT_MAX; // 加速计算 unordered_m...
ooooo-youwillsee/leetcode
lcof_015/cpp_015/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/12. // #ifndef CPP_015__SOLUTION1_H_ #define CPP_015__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; while (n) { ans += 1; n &= (n - 1); } r...
ooooo-youwillsee/leetcode
1738-Find Kth Largest XOR Coordinate Value/cpp_1738/Solution1.h
/** * @author ooooo * @date 2021/2/28 13:14 */ #ifndef CPP_1738__SOLUTION1_H_ #define CPP_1738__SOLUTION1_H_ #include <iostream> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <vector> #include <numeric> using namespace std; class Solution { public: int kthLarg...
ooooo-youwillsee/leetcode
lcof_006/cpp_006/ListNode.h
// // Created by ooooo on 2020/3/7. // #ifndef CPP_006__LISTNODE_H_ #define CPP_006__LISTNODE_H_ #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} ListNode(vector<int> nums) { if (nums.empty()) return; this->v...
ooooo-youwillsee/leetcode
1869-Longer Contiguous Segments of Ones than Zeros/cpp_1869/Solution1.h
<filename>1869-Longer Contiguous Segments of Ones than Zeros/cpp_1869/Solution1.h /** * @author ooooo * @date 2021/5/25 22:20 */ #ifndef CPP_1869__SOLUTION1_H_ #define CPP_1869__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int help(string &s, char c) { in...
ooooo-youwillsee/leetcode
0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/cpp_0235/Solution1.h
// // Created by ooooo on 2019/11/3. // #ifndef CPP_0235_SOLUTION1_H #define CPP_0235_SOLUTION1_H #include "TreeNode.h" #include <queue> class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { if (!root) return NULL; if (root == p || root == q) return ...
ooooo-youwillsee/leetcode
0048-Rotate-Image/cpp_0048/Solution1.h
// // Created by ooooo on 2020/2/11. // #ifndef CPP_0048__SOLUTION1_H_ #define CPP_0048__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: void rotate(vector<vector<int>> &matrix) { int n = matrix.size(); vector<vector<int>> data(n, vector<int>(n)); for (...
ooooo-youwillsee/leetcode
1578-Minimum-Deletion-Cost-to-Avoid-Repeating-Letters/cpp_1578/Solution1.h
/** * @author ooooo * @date 2020/9/7 16:00 */ #ifndef CPP_1578__SOLUTION1_H_ #define CPP_1578__SOLUTION1_H_ #include <iostream> #include <vector> #include <string> #include <numeric> using namespace std; class Solution { public: int minCost(string s, vector<int> &cost) { int i = -1, ans = 0; s += '$'; f...
ooooo-youwillsee/leetcode
lcof_044/cpp_044/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/3/29. // #ifndef CPP_044__SOLUTION1_H_ #define CPP_044__SOLUTION1_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; class Solution { public: long countNums(int digit) { if (digit == 1) return 10; return 9 * powl(10, digit - 1);...
ooooo-youwillsee/leetcode
1733-Minimum Number of People to Teach/cpp_1733/Solution1.h
/** * @author ooooo * @date 2021/3/8 20:04 */ #ifndef CPP_1733__SOLUTION1_H_ #define CPP_1733__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int minimumTeachings(int n, vector<vector<int>> &languages, vector<vector<int>> &friendships) { int pNum = languag...
ooooo-youwillsee/leetcode
0103-Binary-Tree-Zigzag-Level-Order-Traversal/cpp_0103/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2019/12/29. // #ifndef CPP_0103_SOLUTION1_H #define CPP_0103_SOLUTION1_H #include "TreeNode.h" #include <vector> #include <queue> class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode *root) { if (!root) return {}; que...
ooooo-youwillsee/leetcode
0404-Sum-of-Left-Leaves/cpp_0404/Solution2.h
/** * @author ooooo * @date 2020/9/19 13:38 */ #ifndef CPP_0404__SOLUTION2_H_ #define CPP_0404__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: int sumOfLeftLeaves(TreeNode *root, bool is_left) { if (!root) return 0; if (is_left && !root->left && !root->right) return root->val; return sumOfL...
ooooo-youwillsee/leetcode
0035-Search-Insert-Position/cpp_0035/Solution2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/1/6. // #ifndef CPP_0035_SOLUTION2_H #define CPP_0035_SOLUTION2_H #include <iostream> #include <vector> using namespace std; class Solution { public: int searchInsert(vector<int> &nums, int target) { if (nums.empty()) retu...
ooooo-youwillsee/leetcode
0917-Reverse-Only-Letters/cpp_0917/Solution2.h
// // Created by ooooo on 2020/2/1. // #ifndef CPP_0917__SOLUTION2_H_ #define CPP_0917__SOLUTION2_H_ #include <iostream> using namespace std; /** * 存入数组,倒序填入原字符串 */ class Solution { public: string reverseOnlyLetters(string S) { if (S.size() <= 1) return S; int *arr = new int[S.size()], count = 0; //...
ooooo-youwillsee/leetcode
1071-Greatest-Common-Divisor-of-Strings/cpp_1071/Solution1.h
// // Created by ooooo on 2020/2/2. // #ifndef CPP_1071__SOLUTION1_H_ #define CPP_1071__SOLUTION1_H_ #include <iostream> using namespace std; /** * 暴力法 */ class Solution { public: // a % b bool div(string a, string b) { if (a.size() % b.size() != 0) return false; for (int i = 0; i < a.size(); i += b....
ooooo-youwillsee/leetcode
1672-Richest Customer Wealth/cpp_1672/Solution1.h
<filename>1672-Richest Customer Wealth/cpp_1672/Solution1.h /** * @author ooooo * @date 2020/12/11 11:16 */ #ifndef CPP_1672__SOLUTION1_H_ #define CPP_1672__SOLUTION1_H_ #include <iostream> #include <vector> #include <numeric> #include <unordered_map> using namespace std; class Solution { public: int maximumW...
ooooo-youwillsee/leetcode
0228-Summary Ranges/cpp_0228/Solution1.h
<filename>0228-Summary Ranges/cpp_0228/Solution1.h /** * @author ooooo * @date 2021/1/10 上午9:27 */ #ifndef CPP_0228__SOLUTION1_H_ #define CPP_0228__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<string> summaryRanges(vector<int> &nums) { if (nums.empt...
ooooo-youwillsee/leetcode
0315-Count of Smaller Numbers After Self/cpp_0315/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/11/27 11:10 */ #ifndef CPP_0315__SOLUTION1_H_ #define CPP_0315__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> countSmaller(vector<int> &nums) { int n = nums.size(); vector<int> ans(n), insert; ...
ooooo-youwillsee/leetcode
1442-Count Triplets That Can Form Two Arrays of Equal XOR/cpp_1442/Solution2.h
/** * @author ooooo * @date 2021/5/22 11:37 */ #ifndef CPP_1442__SOLUTION2_H_ #define CPP_1442__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int countTriplets(vector<int> &arr) { int n = arr.size(); vector<int> pre(n + 1); for (int i = 0; i < n; i++) {...
ooooo-youwillsee/leetcode
0055-Jump-Game/cpp_0055/Solution1.h
// // Created by ooooo on 2020/2/12. // #ifndef CPP_0055__SOLUTION1_H_ #define CPP_0055__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 超时 */ class Solution { public: bool canJump(vector<int> &nums) { vector<bool> dp(nums.size(), false); dp[0] = true; for (int i = 0; i...
ooooo-youwillsee/leetcode
0232-Implement-Queue-using-Stacks/cpp_0232/Solution1.h
// // Created by ooooo on 2019/10/30. // #ifndef CPP_0232_SOLUTION1_H #define CPP_0232_SOLUTION1_H #include <stack> #include <iostream> using namespace std; class MyQueue { private: stack<int> in; stack<int> out; int front; public: /** Initialize your data structure here. */ MyQueue() { } ...
ooooo-youwillsee/leetcode
0559-Maximum Depth of N-ary Tree/cpp_0559/Node.h
// // Created by ooooo on 2019/12/29. // #ifndef CPP_0599_NODE_H #define CPP_0599_NODE_H #include <istream> #include <vector> using namespace std; class Node { public: int val; vector<Node *> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node *> _children) { val = _...
ooooo-youwillsee/leetcode
0706-Design-HashMap/cpp_0706/Solution1.h
// // Created by ooooo on 2020/1/14. // #ifndef CPP_0706__SOLUTION1_H_ #define CPP_0706__SOLUTION1_H_ class MyHashMap { private: struct Node { int key; int value; Node *next; Node(int key, int value, Node *next) : key(key), value(value), next(next) {} Node() { this->next = nullptr; }...
ooooo-youwillsee/leetcode
lcof_058-2/cpp_058-2/Solution1.h
<filename>lcof_058-2/cpp_058-2/Solution1.h // // Created by ooooo on 2020/4/19. // #ifndef CPP_058_2__SOLUTION1_H_ #define CPP_058_2__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: string reverseLeftWords(string s, int n) { return s.substr(n, s.size() - n) + s.substr(0, n); }...
ooooo-youwillsee/leetcode
lcof_029/cpp_029/Solution1.h
<filename>lcof_029/cpp_029/Solution1.h // // Created by ooooo on 2020/3/18. // #ifndef CPP_029__SOLUTION1_H_ #define CPP_029__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> spiralOrder(vector<vector<int>> &matrix) { if (matrix.empty()) return {}; ...