repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
1865-Finding Pairs With a Certain Sum/cpp_1865/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/25 22:18 */ #ifndef CPP_1865__SOLUTION1_H_ #define CPP_1865__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class FindSumPairs { public: unordered_map<int, int> m; vector<int> nums1, nums2; FindSumPairs(vector<int> &nums1, vector<i...
ooooo-youwillsee/leetcode
1332-Remove-Palindromic-Subsequences/cpp_1332/Solution1.h
// // Created by ooooo on 2020/2/4. // #ifndef CPP_1332__SOLUTION1_H_ #define CPP_1332__SOLUTION1_H_ #include <iostream> using namespace std; /** * 做多为两次。 因为n个a本身就是子序列,所以先删除所有的a,再删除所有的b */ class Solution { public: int removePalindromeSub(string s) { for (int left = 0, right = s.size() - 1; left < right; ++...
ooooo-youwillsee/leetcode
0925-Long-Pressed-Name/cpp_0925/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/8. // #ifndef CPP_0925_SOLUTION2_H #define CPP_0925_SOLUTION2_H #include <iostream> #include <vector> using namespace std; /** * 分组 */ class Solution { private: struct Group { char c; int count; Group(char c) { ...
ooooo-youwillsee/leetcode
1792-Maximum Average Pass Ratio/cpp_1792/Solution1.h
/** * @author ooooo * @date 2021/3/28 12:13 */ #ifndef CPP_1792__SOLUTION1_H_ #define CPP_1792__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
1626-Best Team With No Conflicts/cpp_1626/Solution1.h
/** * @author ooooo * @date 2020/11/15 09:03 */ #ifndef CPP_1626__SOLUTION1_H_ #define CPP_1626__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <numeric> using namespace std; /** * dp[i] 表示有第i个球员的最大得分 */ class Sol...
ooooo-youwillsee/leetcode
0117-Populating-Next-Right-Pointers-in-Each-Node-II/cpp_0117/Solution2.h
/** * @author ooooo * @date 2020/9/28 09:27 */ #ifndef CPP_0117__SOLUTION2_H_ #define CPP_0117__SOLUTION2_H_ #include "Node.h" /** * recursion */ class Solution { public: Node *getNextNode(Node *p_node) { while (p_node) { if (p_node->left) return p_node->left; if (p_node->right) return p_node->right...
ooooo-youwillsee/leetcode
1237-Find-Positive-Integer-Solution-for-a-Given-Equation/cpp_1237/Solution1.h
// // Created by ooooo on 2020/1/22. // #ifndef CPP_1237__SOLUTION1_H_ #define CPP_1237__SOLUTION1_H_ #include "CustomFunction.h" /** * 暴力破解 */ class Solution { public: vector<vector<int>> findSolution(CustomFunction &customfunction, int z) { vector<vector<int>> ans; for (int x = 1; x <= 1000; ++x) { ...
ooooo-youwillsee/leetcode
0278-First-Bad-Version/cpp_0278/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0278-First-Bad-Version/cpp_0278/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/17. // #ifndef CPP_0278__SOLUTION1_H_ #define CPP_0278__SOLUTION1_H_ #include <iostream> using namespace std; bool isBadVersion(int version) { return version >= 4; } class Solu...
ooooo-youwillsee/leetcode
0925-Long-Pressed-Name/cpp_0925/Solution1.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0925_SOLUTION1_H #define CPP_0925_SOLUTION1_H #include <iostream> using namespace std; class Solution { public: bool isLongPressedName(string name, string typed) { if (name.empty()) return typed.empty(); if (typed.size() < name.size()) return fal...
ooooo-youwillsee/leetcode
lcof_039/cpp_039/Solution3.h
<filename>lcof_039/cpp_039/Solution3.h<gh_stars>10-100 // // Created by ooooo on 2020/3/24. // #ifndef CPP_039__SOLUTION3_H_ #define CPP_039__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * */ class Solution { public: int majorityElement(vector<int> &nums) { int num = nums[0]; ...
ooooo-youwillsee/leetcode
0637-Average-of-Levels-in-Binary-Tree/cpp_0637/Solution3.h
/** * @author ooooo * @date 2020/9/12 10:44 */ #ifndef CPP_0637__SOLUTION3_H_ #define CPP_0637__SOLUTION3_H_ #include "TreeNode.h" class Solution3 { public: vector<double> averageOfLevels(TreeNode *root) { vector<double> ans; if (!root) return ans; queue<TreeNode *> q; q.push(root); while (!q.empty(...
ooooo-youwillsee/leetcode
0804-Unique-Morse-Code-Words/cpp_0804/Solution1.h
// // Created by ooooo on 2019/12/14. // #ifndef CPP_0804_SOLUTION1_H #define CPP_0804_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int uniqueMorseRepresentations(vector<string> &words) { string arr[] = {".-", "-...", "-.-.", "-...
ooooo-youwillsee/leetcode
0416-Partition-Equal-Subset-Sum/cpp_0416/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/29. // #ifndef CPP_0416__SOLUTION1_H_ #define CPP_0416__SOLUTION1_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; /** * dfs timeout */ class Solution { public: bool dfs(int i, int left_remains, int right_remains) { if (left_...
ooooo-youwillsee/leetcode
0701-Insert-into-a-Binary-Search-Tree/cpp_0701/Solution1.h
/** * @author ooooo * @date 2020/9/30 09:17 */ #ifndef CPP_0701__SOLUTION1_H_ #define CPP_0701__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: TreeNode *insertIntoBST(TreeNode *root, int val) { if (!root) return new TreeNode(val); if (root->val < val) { root->right = insertIntoBST(root->rig...
ooooo-youwillsee/leetcode
0148-Sort-List/cpp_0148/ListNode.h
// // Created by ooooo on 2020/2/18. // #ifndef CPP_0148__LISTNODE_H_ #define CPP_0148__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) : val(nums[0]), next(NULL) { ListNod...
ooooo-youwillsee/leetcode
0234-Palindrome-Linked-List/cpp_0234/ListNode.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/8. // #ifndef CPP_0234_LISTNODE_H #define CPP_0234_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> vec) { if (vec.e...
ooooo-youwillsee/leetcode
0474-Ones and Zeroes/cpp_0474/Solution1.h
/** * @author ooooo * @date 2021/6/6 10:23 */ #ifndef CPP_0474__SOLUTION1_H_ #define CPP_0474__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findMaxForm(vector<string> &strs, int m, int n) { int k = strs.size(); int dp[k + 1][m + 1][n + 1]; memset(d...
ooooo-youwillsee/leetcode
1814-Count Nice Pairs in an Array/cpp_1814/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/4/11 15:48 */ #ifndef CPP_1814__SOLUTION1_H_ #define CPP_1814__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution1 { long long rev(long long num) { long long sum = 0; while (num) { sum = sum * 10 + ...
ooooo-youwillsee/leetcode
0541-Reverse-String-II/cpp_0541/Solution1.h
<filename>0541-Reverse-String-II/cpp_0541/Solution1.h // // Created by ooooo on 2020/1/26. // #ifndef CPP_0541__SOLUTION1_H_ #define CPP_0541__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: string reverseStr(string s, int k) { bool flag = true; int i = 0; // -k => 预留k个空...
ooooo-youwillsee/leetcode
0101-Symmetric-Tree/cpp_0101/Solution1.h
<filename>0101-Symmetric-Tree/cpp_0101/Solution1.h // // Created by ooooo on 2019/12/6. // #ifndef CPP_0101_SOLUTION1_H #define CPP_0101_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { private: bool dfs(TreeNode *node1, TreeNode *node2) { if (!node1 && !node2) return true; i...
ooooo-youwillsee/leetcode
0189-Rotate-Array/cpp_0189/Solution3.h
// // Created by ooooo on 2019/12/5. // #ifndef CPP_0189_SOLUTION3_H #define CPP_0189_SOLUTION3_H #include <iostream> #include <vector> using namespace std; class Solution { private: void reverse(vector<int> &nums, int start, int end) { if (nums.size() == 0) return; while (start < end) { ...
ooooo-youwillsee/leetcode
1046-Last-Stone-Weight/cpp_1046/Solution1.h
// // Created by ooooo on 2019/12/30. // #ifndef CPP_1046_SOLUTION1_H #define CPP_1046_SOLUTION1_H #include <iostream> #include <vector> #include <functional> #include <queue> using namespace std; class Solution { public: int lastStoneWeight(vector<int> &stones) { priority_queue<int> pq(less<int>(), ston...
ooooo-youwillsee/leetcode
0637-Average-of-Levels-in-Binary-Tree/cpp_0637/Solution2.h
<reponame>ooooo-youwillsee/leetcode<filename>0637-Average-of-Levels-in-Binary-Tree/cpp_0637/Solution2.h // // Created by ooooo on 2020/1/3. // #ifndef CPP_0637_SOLUTION2_H #define CPP_0637_SOLUTION2_H #include "TreeNode.h" #include <vector> class Solution { public: /** * * @param node * @param lev...
ooooo-youwillsee/leetcode
0617-Merge-Two-Binary-Trees/cpp_0617/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/9/23 10:12 */ #ifndef CPP_0617__SOLUTION2_H_ #define CPP_0617__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: TreeNode *mergeTrees(TreeNode *t1, TreeNode *t2) { if (!t1) return t2; if (!t2) return t1; t1->val += t2->val; t1->left = mergeTre...
ooooo-youwillsee/leetcode
0102-Binary-Tree-Level-Order-Traversal/cpp_0102/Solution2.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0102_SOLUTION2_H #define CPP_0102_SOLUTION2_H #include <iostream> #include "TreeNode.h" #include <queue> #include <vector> using namespace std; class Solution { private: vector<vector<int>> res; void dfs(int level, TreeNode *node) { if (!node) r...
ooooo-youwillsee/leetcode
0039-Combination-Sum/cpp_0039/Solution2.h
/** * @author ooooo * @date 2020/9/9 22:25 */ #ifndef CPP_0039__SOLUTION2_H_ #define CPP_0039__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> ans; vector<int> candidates; void dfs(vector<int> &vec, int i, int sum) { if (sum < 0) retur...
ooooo-youwillsee/leetcode
1030-Matrix-Cells-in-Distance-Order/cpp_1030/Solution1.h
/** * @author ooooo * @date 2020/11/17 17:35 */ #ifndef CPP_1030__SOLUTION1_H_ #define CPP_1030__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * sort */ class Solution { public: vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) { vector<vector<int>> ans; f...
ooooo-youwillsee/leetcode
0606-Construct-String-from-Binary-Tree/cpp_0606/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/3. // #ifndef CPP_0606_SOLUTION1_H #define CPP_0606_SOLUTION1_H #include "TreeNode.h" /** * 字符串拼接有点低效, 后序遍历 */ class Solution { public: string tree2str(TreeNode *t) { if (!t) return ""; string leftStr = tree2str(t->left); ...
ooooo-youwillsee/leetcode
0893-Groups-of-Special-Equivalent-Strings/cpp_0893/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/1. // #ifndef CPP_0893__SOLUTION1_H_ #define CPP_0893__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * */ class Solution { public: int numSpecialEquivGroups(vector<string> &A) { unorde...
ooooo-youwillsee/leetcode
0941-Valid-Mountain-Array/cpp_0941/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2020/9/30 16:54 */ #ifndef CPP_0941__SOLUTION1_H_ #define CPP_0941__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool validMountainArray(vector<int> &A) { if (A.size() < 3) ret...
ooooo-youwillsee/leetcode
lcof_037/cpp_037/Solution1.h
// // Created by ooooo on 2020/3/23. // #ifndef CPP_037__SOLUTION1_H_ #define CPP_037__SOLUTION1_H_ #include "TreeNode.h" #include <queue> #include <sstream> class Codec { public: // Encodes a tree to a single string. string serialize(TreeNode *root) { if (!root) return "[]"; stringstream ss; ss << ...
ooooo-youwillsee/leetcode
0480-Sliding Window Median/cpp_0480/Solution1.h
<filename>0480-Sliding Window Median/cpp_0480/Solution1.h /** * @author ooooo * @date 2021/3/30 17:06 */ #ifndef CPP_0480__SOLUTION1_H_ #define CPP_0480__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <queue> class Solution { public: class Median { public: int minSize...
ooooo-youwillsee/leetcode
0268-Missing-Number/cpp_0268/Solution2.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0268_SOLUTION2_H #define CPP_0268_SOLUTION2_H #include <iostream> #include <vector> using namespace std; class Solution { public: int missingNumber(vector<int> &nums) { int sum = 0; for (int i = 0, len = nums.size(); i < len; ++i) { s...
ooooo-youwillsee/leetcode
1203-Sort Items by Groups Respecting Dependencies/cpp_1203/Solution1.h
/** * @author ooooo * @date 2021/1/12 13:17 */ #ifndef CPP_1203__SOLUTION1_H_ #define CPP_1203__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: vector<int> sortItems(int n, int m, vector<int> &...
ooooo-youwillsee/leetcode
1579-Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable/cpp_1579/Solution1.h
/** * @author ooooo * @date 2020/9/26 00:41 */ #ifndef CPP_1579__SOLUTION1_H_ #define CPP_1579__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: // union find ( path zipå) int find(vector<int> &p, int i) { int t = i; while (i != p[i]) { i = p[i]; } ...
ooooo-youwillsee/leetcode
lcof_032-2/cpp_032-2/Solution1.h
// // Created by ooooo on 2020/3/20. // #ifndef CPP_032_2__SOLUTION1_H_ #define CPP_032_2__SOLUTION1_H_ #include "TreeNode.h" #include <queue> /** * level order */ class Solution { public: vector<vector<int>> levelOrder(TreeNode *root) { if(!root) return {}; vector<vector<int>> ans; queue<TreeNode *>...
ooooo-youwillsee/leetcode
lcof_019/cpp_019/Solution1.h
<filename>lcof_019/cpp_019/Solution1.h // // Created by ooooo on 2020/3/14. // #ifndef CPP_019__SOLUTION1_H_ #define CPP_019__SOLUTION1_H_ #include <iostream> #include <vector> #include <iostream> #include <vector> using namespace std; class Solution { public: bool dfs(int i, int j) { if (i >= s.size() && ...
ooooo-youwillsee/leetcode
0659-Split Array into Consecutive Subsequences/cpp_0659/Solution1.h
/** * @author ooooo * @date 2020/12/4 20:18 */ #ifndef CPP_0659__SOLUTION1_H_ #define CPP_0659__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool isPossible1(vector<int> &nums) { vector<pair<int, int>> groups; groups.push_back({nums[0], nums[0]}); ve...
ooooo-youwillsee/leetcode
1670-Design Front Middle Back Queue/cpp_1670/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/1/24 17:55 */ #ifndef CPP_1670__SOLUTION1_H_ #define CPP_1670__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack> using namespace std; // https://leetcode-cn.c...
ooooo-youwillsee/leetcode
0435-Non-overlapping Intervals/cpp_0435/Solution1.h
/** * @author ooooo * @date 2020/12/31 15:00 */ #ifndef CPP_0435__SOLUTION1_H_ #define CPP_0435__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int eraseOverlapIntervals(vector<vector<int>> &intervals) { if (intervals.size() <= 1) return 0; sort(intervals....
ooooo-youwillsee/leetcode
0121-Best-Time-to-Buy-and-Sell-Stock/cpp_0121/Solution1.h
// // Created by ooooo on 2020/2/4. // #ifndef CPP_0121__SOLUTION1_H_ #define CPP_0121__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 找到最小值 */ class Solution { public: int maxProfit(vector<int> &prices) { int ans = 0, min_price = INT_MAX; for (auto price: prices) { ...
ooooo-youwillsee/leetcode
0191-Number-of-1-Bits/cpp_0191/Solution1.h
// // Created by ooooo on 2019/12/5. // #ifndef CPP_0191_SOLUTION1_H #define CPP_0191_SOLUTION1_H #include <iostream> using namespace std; class Solution { public: int hammingWeight(uint32_t n) { int res = 0; while (n) { res +=1; n &= n-1; } return res; ...
ooooo-youwillsee/leetcode
0202-Happy-Number/cpp_0202/Solution2.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0202_SOLUTION2_H #define CPP_0202_SOLUTION2_H #include <iostream> #include <cmath> #include <unordered_set> using namespace std; /** * * 哈希表 (先insert,计算之后如果还存在, 就是false) */ class Solution { public: int help(int num) { int sum = 0; while (num...
ooooo-youwillsee/leetcode
lcof_003/cpp_003/Solution3.h
<filename>lcof_003/cpp_003/Solution3.h // // Created by ooooo on 2020/3/5. // #ifndef CPP_003__SOLUTION3_H_ #define CPP_003__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * 抽屉原理 */ class Solution { public: int findRepeatNumber(vector<int> &nums) { for (int i = 0, len = nums.siz...
ooooo-youwillsee/leetcode
0965-Univalued-Binary-Tree/cpp_0965/Solution1.h
<filename>0965-Univalued-Binary-Tree/cpp_0965/Solution1.h // // Created by ooooo on 2019/12/31. // #ifndef CPP_0965_SOLUTION1_H #define CPP_0965_SOLUTION1_H #include "TreeNode.h" class Solution { public: bool dfs(TreeNode *node, int x) { if (!node) return true; return node->val == x && dfs(node-...
ooooo-youwillsee/leetcode
1442-Count Triplets That Can Form Two Arrays of Equal XOR/cpp_1442/Solution3.h
/** * @author ooooo * @date 2021/5/22 11:38 */ #ifndef CPP_1442__SOLUTION3_H_ #define CPP_1442__SOLUTION3_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int countTriplets(vector<int>& arr) { int n = arr.size(); vector<int> pre(n+1); for (i...
ooooo-youwillsee/leetcode
1370-Increasing-Decreasing-String/cpp_1370/Solution1.h
/** * @author ooooo * @date 2020/11/25 09:13 */ #ifndef CPP_1370__SOLUTION1_H_ #define CPP_1370__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string sortString(string s) { vector<int> count(26); for (auto &c :s) { count[c - 'a']++; } int n = s.siz...
ooooo-youwillsee/leetcode
0202-Happy-Number/cpp_0202/Solution3.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0202_SOLUTION3_H #define CPP_0202_SOLUTION3_H #include <iostream> #include <cmath> using namespace std; /** * * 快慢指针 */ class Solution { public: int help(int num) { int sum = 0; while (num) { sum += pow(num % 10, 2); ...
ooooo-youwillsee/leetcode
0144-Binary-Tree-Preorder-Traversal/cpp_0144/TreeNode.h
<filename>0144-Binary-Tree-Preorder-Traversal/cpp_0144/TreeNode.h /** * @author ooooo * @date 2020/9/25 23:15 */ #ifndef CPP_0144__TREENODE_H_ #define CPP_0144__TREENODE_H_ #include <iostream> #include <vector> #include <stack> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; ...
ooooo-youwillsee/leetcode
lcof_030/cpp_030/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/3/19. // #ifndef CPP_030__SOLUTION1_H_ #define CPP_030__SOLUTION1_H_ #include <iostream> using namespace std; class MinStack { private: struct Node { Node *next; int val; Node(Node *next, int val) : next(next), val(val) {} Node(int val) : val(val), ...
ooooo-youwillsee/leetcode
0696-Count-Binary-Substrings/cpp_0696/Solution2.h
// // Created by ooooo on 2020/1/30. // #ifndef CPP_0696__SOLUTION2_H_ #define CPP_0696__SOLUTION2_H_ #include <iostream> using namespace std; /** * 上一个连续字符的个数和当前连续字符的个数 */ class Solution { public: int countBinarySubstrings(string s) { int ans = 0, prev = 0, cur = 1; for (int i = 0; i < s.size(); ++i) ...
ooooo-youwillsee/leetcode
1881-Maximum Value after Insertion/cpp_1881/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/31 10:17 */ #ifndef CPP_1881__SOLUTION1_H_ #define CPP_1881__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string maxValue(string n, int x) { string ans = ""; int i = 0; bool inserted = false; if (n[...
ooooo-youwillsee/leetcode
0080-Remove Duplicates from Sorted Array II/cpp_0080/Solution1.h
/** * @author ooooo * @date 2021/4/6 11:19 */ #ifndef CPP_0080__SOLUTION1_H_ #define CPP_0080__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int removeDuplicates(vector<int> &nums) { int i = 0; for (auto num : nums) { if (i < 2 || num != nums[i - 2]) {...
ooooo-youwillsee/leetcode
0811-Subdomain-Visit-Count/cpp_0811/Solution1.h
// // Created by ooooo on 2020/1/15. // #ifndef CPP_0811__SOLUTION1_H_ #define CPP_0811__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<string> split(string s, string sep) { vector<string> ans; int i = s.find_first_of(" ") ...
ooooo-youwillsee/leetcode
1584-Min Cost to Connect All Points/cpp_1584/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/19 20:12 */ #ifndef CPP_1584__SOLUTION1_H_ #define CPP_1584__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: vector<int> p; struct Edge { int len, u, v; Edge(int len, int u, int...
ooooo-youwillsee/leetcode
0206-Reverse-Linked-List/cpp_0206/ListNode.h
// // Created by ooooo on 2019/10/29. // #ifndef CPP_0206_LISTNODE_H #define CPP_0206_LISTNODE_H struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; #endif //CPP_0206_LISTNODE_H
ooooo-youwillsee/leetcode
0147-Insertion Sort List/cpp_0147/Solution1.h
<filename>0147-Insertion Sort List/cpp_0147/Solution1.h /** * @author ooooo * @date 2020/11/20 09:11 */ #ifndef CPP_0147__SOLUTION1_H_ #define CPP_0147__SOLUTION1_H_ #include "ListNode.h" class Solution { public: void insert(ListNode *dummyHead, ListNode *node) { ListNode *cur = dummyHead; while (cur->nex...
ooooo-youwillsee/leetcode
0744-Find-Smallest-Letter-Greater-Than-Target/cpp_0744/Solution1.h
// // Created by ooooo on 2020/1/20. // #ifndef CPP_0744__SOLUTION1_H_ #define CPP_0744__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * binary search */ class Solution { public: char nextGreatestLetter(vector<char> &letters, char target) { if (target < letters[0] || target >=...
ooooo-youwillsee/leetcode
0018-4Sum/cpp_0018/Solution2.h
// // Created by ooooo on 2019/11/1. // #ifndef CPP_0018_SOLUTION2_H #define CPP_0018_SOLUTION2_H #include <iostream> #include <vector> #include <algorithm> #include <map> #include <set> using namespace std; class Solution { public: vector<vector<int>> fourSum(vector<int> &nums, int target) { vector<vect...
ooooo-youwillsee/leetcode
0088-Merge-Sorted-Array/cpp_0088/Solution2.h
// // Created by ooooo on 2019/12/5. // #ifndef CPP_0088_SOLUTION2_H #define CPP_0088_SOLUTION2_H #include <vector> #include <iostream> using namespace std; class Solution { public: void merge(vector<int> &nums1, int m, vector<int> &nums2, int n) { vector<int> vec(nums1.begin(), nums1.begin() + m); ...
ooooo-youwillsee/leetcode
0132-Palindrome Partitioning II/cpp_0132/Solution1.h
/** * @author ooooo * @date 2021/3/8 12:05 */ #ifndef CPP_0132__SOLUTION1_H_ #define CPP_0132__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int minCut(string s) { int n = s.size(); vector<vector<bool>> dp(n, vector<bool>(n, 0)); for (int i = n - 1; i >...
ooooo-youwillsee/leetcode
0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution1.h
<filename>0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/2/15. // #ifndef CPP_0094__SOLUTION1_H_ #define CPP_0094__SOLUTION1_H_ #include "TreeNode.h" /** * recursion */ class Solution { public: void dfs(TreeNode *node) { if (!node) return; dfs(node...
ooooo-youwillsee/leetcode
0101-Symmetric-Tree/cpp_0101/Solution2.h
<filename>0101-Symmetric-Tree/cpp_0101/Solution2.h<gh_stars>10-100 // // Created by ooooo on 2019/12/6. // #ifndef CPP_0101_SOLUTION2_H #define CPP_0101_SOLUTION2_H #include "TreeNode.h" #include <queue> class Solution { public: bool isSymmetric(TreeNode *root) { queue<TreeNode *> q; q.push(root)...
ooooo-youwillsee/leetcode
1011-Capacity To Ship Packages Within D Days/cpp_1011/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/4/26 19:42 */ #ifndef CPP_1011__SOLUTION1_H_ #define CPP_1011__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool check(vector<int> &weights, int k, int D) { int d = 0; int cur = 0; for (int i = 0; i < w...
ooooo-youwillsee/leetcode
0494-Target-Sum/cpp_0494/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0494-Target-Sum/cpp_0494/Solution1.h // // Created by ooooo on 2020/3/3. // #ifndef CPP_0494__SOLUTION1_H_ #define CPP_0494__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dfs */ class Solution { public: void dfs(int i, int sum) { ...
ooooo-youwillsee/leetcode
0509-Fibonacci-Number/cpp_0509/Solution1.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0509_SOLUTION1_H #define CPP_0509_SOLUTION1_H #include <iostream> using namespace std; /** * recursion */ class Solution { public: int fib(int N) { if (N == 0 || N == 1) return N; return fib(N - 1) + fib(N - 2); } }; #endif //CPP_0509_SOLU...
ooooo-youwillsee/leetcode
0532-K-diff-Pairs-in-an-Array/cpp_0532/Solution3.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0532_SOLUTION3_H #define CPP_0532_SOLUTION3_H #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> using namespace std; /** * */ class Solution { public: int findPairs(vector<int> &nums, int k) { if (nums.empty() || ...
ooooo-youwillsee/leetcode
0268-Missing-Number/cpp_0268/Solution3.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/6. // #ifndef CPP_0268_SOLUTION3_H #define CPP_0268_SOLUTION3_H #include <iostream> #include <vector> using namespace std; /** * a ^ 0 = a * a ^ a = 0 */ class Solution { public: int missingNumber(vector<int> &nums) { int num = nu...
ooooo-youwillsee/leetcode
0037-Sudoku-Solver/cpp_0037/Solution1.h
<filename>0037-Sudoku-Solver/cpp_0037/Solution1.h // // Created by ooooo on 2019/11/21. // #ifndef CPP_0037_SOLUTION1_H #define CPP_0037_SOLUTION1_H #include <iostream> #include <vector> #include <set> using namespace std; class Solution { private: bool check(vector<vector<char>> &board, int i, int j, char k) ...
ooooo-youwillsee/leetcode
1718-Construct the Lexicographically Largest Valid Sequence/cpp_1718/Solution1.h
/** * @author ooooo * @date 2021/1/24 09:28 */ #ifndef CPP_1718__SOLUTION1_H_ #define CPP_1718__SOLUTION1_H_ #include <iostream> #include <vector> #include <set> using namespace std; class Solution { public: vector<int> nums; vector<int> ans; bool dfs(int i) { if (i >= ans.size()) return true; if (ans...
ooooo-youwillsee/leetcode
lcp-028/cpp_028/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>lcp-028/cpp_028/Solution1.h /** * @author ooooo * @date 2021/4/10 20:27 */ #ifndef CPP_028__SOLUTION1_H_ #define CPP_028__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int purchasePlans(vector<int> &nums, int targe...
ooooo-youwillsee/leetcode
0454-4Sum II/cpp_0454/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0454-4Sum II/cpp_0454/Solution1.h /** * @author ooooo * @date 2020/11/27 10:05 */ #ifndef CPP_0454__SOLUTION1_H_ #define CPP_0454__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int fourSumC...
ooooo-youwillsee/leetcode
0680-Valid-Palindrome-II/cpp_0680/Solution3.h
// // Created by ooooo on 2020/1/28. // #ifndef CPP_0680__SOLUTION3_H_ #define CPP_0680__SOLUTION3_H_ #include <iostream> using namespace std; /** * 双指针, 有一个不相等的字符, 就比较 【left-1, right】和 【left, right-1】 */ class Solution { public: bool validPalindrome(string s) { for (int left = 0, right = s.size() - 1; lef...
ooooo-youwillsee/leetcode
0503-Next Greater Element II/cpp_0503/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/6 13:05 */ #ifndef CPP_0503__SOLUTION1_H_ #define CPP_0503__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; // 单调栈 ,底部元素最大 class Solution { public: vector<int> nextGreaterElements(vector<int> &nums)...
ooooo-youwillsee/leetcode
1269-Number of Ways to Stay in the Same Place After Some Steps/cpp_1269/Solution1.h
<filename>1269-Number of Ways to Stay in the Same Place After Some Steps/cpp_1269/Solution1.h /** * @author ooooo * @date 2021/5/13 21:19 */ #ifndef CPP_1269__SOLUTION1_H_ #define CPP_1269__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public:...
ooooo-youwillsee/leetcode
0671-Second-Minimum-Node-In-a-Binary-Tree/cpp_0671/Solution1.h
// // Created by ooooo on 2020/1/4. // #ifndef CPP_0671_SOLUTION1_H #define CPP_0671_SOLUTION1_H #include "TreeNode.h" class Solution { public: int dfs(TreeNode *node, int min) { if (!node) return -1; if (node->val > min) return node->val; int left = dfs(node->left, min); int rig...
ooooo-youwillsee/leetcode
0669-Trim-a-Binary-Search-Tree/cpp_0669/TreeNode.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0669_TREENODE_H #define CPP_0669_TREENODE_H #include <iostream> #include <vector> #include <queue> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} TreeNode(ve...
ooooo-youwillsee/leetcode
1727-Largest Submatrix With Rearrangements/cpp_1727/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/24 17:10 */ #ifndef CPP_1727__SOLUTION1_H_ #define CPP_1727__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <unordered_set> #include <set> using namespace std; class Solution { public: int largestSubma...
ooooo-youwillsee/leetcode
lcof_004/cpp_004/Solution2.h
// // Created by ooooo on 2020/3/6. // #ifndef CPP_004__SOLUTION2_H_ #define CPP_004__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * O(n^2) */ class Solution { public: bool findNumberIn2DArray(vector<vector<int>> &matrix, int target) { for (int i = 0, rows = matrix.size(); i <...
ooooo-youwillsee/leetcode
0621-Task Scheduler/cpp_0621/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/12/5 11:39 */ #ifndef CPP_0621__SOLUTION1_H_ #define CPP_0621__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; /** * timeout */ class Solution { public: int leastInterval(vector<char> &tasks, int n)...
ooooo-youwillsee/leetcode
0106-Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/cpp_0106/TreeNode.h
/** * @author ooooo * @date 2020/9/25 09:24 */ #ifndef CPP_0106__TREENODE_H_ #define CPP_0106__TREENODE_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; ...
ooooo-youwillsee/leetcode
0606-Construct-String-from-Binary-Tree/cpp_0606/Solution2.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0606_SOLUTION2_H #define CPP_0606_SOLUTION2_H #include "TreeNode.h" /** * 无返回值快。 前序遍历 */ class Solution { public: void dfs(TreeNode *node, string &s) { if (!node) return; s += to_string(node->val); if (node->left || node->right) { ...
ooooo-youwillsee/leetcode
0463-Island-Perimeter/cpp_0463/Solution1.h
// // Created by ooooo on 2020/1/11. // #ifndef CPP_0463__SOLUTION1_H_ #define CPP_0463__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 分别计算每一个节点的边数 */ class Solution { public: int help(vector<vector<int>> &grid, int i, int j) { if (grid[i][j] == 0) return 0; int ans = 0;...
ooooo-youwillsee/leetcode
1722-Minimize Hamming Distance After Swap Operations/cpp_1722/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/9 18:18 */ #ifndef CPP_1722__SOLUTION1_H_ #define CPP_1722__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: struct UF { int n; vector<i...
ooooo-youwillsee/leetcode
lcp-030/cpp_030/Solution1.h
/** * @author ooooo * @date 2021/4/10 20:21 */ #ifndef CPP_030__SOLUTION1_H_ #define CPP_030__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int magicTower(vector<int> &nums) { long long sum = 1; int n = nums.size(); for (int i = 0; i < n; i++) { sum ...
ooooo-youwillsee/leetcode
0819-Most-Common-Word/cpp_0819/Solution2.h
<filename>0819-Most-Common-Word/cpp_0819/Solution2.h // // Created by ooooo on 2020/1/30. // #ifndef CPP_0819__SOLUTION2_H_ #define CPP_0819__SOLUTION2_H_ #include <iostream> #include <vector> #include <regex> #include <unordered_map> #include <unordered_set> using namespace std; /** * */ class Solution { public:...
ooooo-youwillsee/leetcode
0212-Word-Search-II/cpp_0212/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2019/12/4. // #ifndef CPP_0212_SOLUTION1_H #define CPP_0212_SOLUTION1_H #include <iostream> #include <vector> #include <set> using namespace std; class Solution { private: struct Node { bool isWord = false; vector<Node *> children = vector<Node *>(26, ...
ooooo-youwillsee/leetcode
0337-House-Robber-III/cpp_0337/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/25. // #ifndef CPP_0337__SOLUTION2_H_ #define CPP_0337__SOLUTION2_H_ #include "TreeNode.h" #include <unordered_map> using namespace std; /** * max money = max(根节点 + 四个孙子 , 两个儿子) * A * / \ * B B * / \ / \ * C C C C * * dfs + memo */ class So...
ooooo-youwillsee/leetcode
0153-Find-Minimum-in-Rotated-Sorted-Array/cpp_0153/Solution2.h
/** * @author ooooo * @date 2020/9/25 23:55 */ #ifndef CPP_0153__SOLUTION2_H_ #define CPP_0153__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findMin(vector<int> &nums, int l, int r) { int mid = l + (r - l) / 2; if (nums[mid] > nums[l] && nums[mid] >...
ooooo-youwillsee/leetcode
lcci_17_13/cpp_17_13/Solution1.h
/** * @author ooooo * @date 2020/10/10 11:58 */ #ifndef CPP_17_13__SOLUTION1_H_ #define CPP_17_13__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int respace(vector<string> &dictionary, string sentence) { int n = sentence.size(); ...
ooooo-youwillsee/leetcode
0459-Repeated-Substring-Pattern/cpp_0459/Solution2.h
// // Created by ooooo on 2020/1/26. // #ifndef CPP_0459__SOLUTION2_H_ #define CPP_0459__SOLUTION2_H_ #include <iostream> using namespace std; class Solution { public: bool repeatedSubstringPattern(string s) { /** * "abab" ==> "abababab" ==> 2 != s.size() * "aba" ==> "abaaba" ==> 3 = s.size() ...
ooooo-youwillsee/leetcode
1646-Get Maximum in Generated Array/cpp_1646/Solution1.h
/** * @author ooooo * @date 2021/2/28 13:02 */ #ifndef CPP_1646__SOLUTION1_H_ #define CPP_1646__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <numeric> using namespace std; class Solution { public: unordered_map...
ooooo-youwillsee/leetcode
0208-Implement-Trie-Prefix-Tree/cpp_0208/Solution1.h
<filename>0208-Implement-Trie-Prefix-Tree/cpp_0208/Solution1.h // // Created by ooooo on 2019/12/2. // #ifndef CPP_0208_SOLUTION1_H #define CPP_0208_SOLUTION1_H #include <string> #include <vector> using namespace std; class Trie { private: struct Node { char c; vector<Node *> children = vector<...
ooooo-youwillsee/leetcode
0207-Course-Schedule/cpp_0207/Solution2.h
// // Created by ooooo on 2020/2/19. // #ifndef CPP_0207__SOLUTION2_H_ #define CPP_0207__SOLUTION2_H_ #include <iostream> #include <vector> #include <queue> using namespace std; /** * 拓扑排序 */ class Solution { public: bool canFinish(int numCourses, vector<vector<int>> &prerequisites) { vector<int> indegrees...
ooooo-youwillsee/leetcode
0300-Longest-Increasing-Subsequence/cpp_0300/Solution2.h
// // Created by ooooo on 2020/2/24. // #ifndef CPP_0300__SOLUTION2_H_ #define CPP_0300__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 二分法 * 每一循环,尽量把小的放进去 */ class Solution { public: int lengthOfLIS(vector<int> &nums) { int n = nums.size(); if (n <= 1) return n; vect...
ooooo-youwillsee/leetcode
lcof_062/cpp_062/Solution2.h
// // Created by ooooo on 2020/4/22. // #ifndef CPP_062__SOLUTION2_H_ #define CPP_062__SOLUTION2_H_ #include <iostream> #include <list> using namespace std; /** * */ class Solution { public: int lastRemaining(int n, int m) { int ans = 0; for (int i = 2; i <= n; ++i) { ans = (ans + m) % i; } ...
ooooo-youwillsee/leetcode
0367-Valid-Perfect-Square/cpp_0367/Solution1.h
// // Created by ooooo on 2020/1/17. // #ifndef CPP_0367__SOLUTION1_H_ #define CPP_0367__SOLUTION1_H_ #include <iostream> using namespace std; /** * int * int 越界 */ class Solution { public: bool isPerfectSquare(int num) { if (num < 2) return true; long left = 2, right = num / 2; while (left <= right...
ooooo-youwillsee/leetcode
0135-Candy/cpp_0135/Solution1.h
/** * @author ooooo * @date 2020/12/24 14:16 */ #ifndef CPP_0135__SOLUTION1_H_ #define CPP_0135__SOLUTION1_H_ #include <iostream> #include <map> #include <vector> #include <numeric> using namespace std; class Solution { public: int candy(vector<int> &ratings) { map<int, vector<int>> rate_map; int n = rati...
ooooo-youwillsee/leetcode
1642-Furthest Building You Can Reach/cpp_1642/Solution2.h
<filename>1642-Furthest Building You Can Reach/cpp_1642/Solution2.h /** * @author ooooo * @date 2021/3/6 16:27 */ #ifndef CPP_1642__SOLUTION2_H_ #define CPP_1642__SOLUTION2_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; /** * 二分法 */ class Solution { public: int furthestBuil...
ooooo-youwillsee/leetcode
0938-Range-Sum-of-BST/cpp_0938/Solution1.h
// // Created by ooooo on 2019/12/31. // #ifndef CPP_0938_SOLUTION1_H #define CPP_0938_SOLUTION1_H #include "TreeNode.h" class Solution { public: int rangeSumBST2(TreeNode *root, int L, int R) { if (!root) return 0; if (root->val < L) return rangeSumBST(root->right, L, R); if (root->val...