repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
lcof_038/cpp_038/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/24. // #ifndef CPP_038__SOLUTION2_H_ #define CPP_038__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: void dfs(int i) { if (i == s.size()) ans.push_back(s); for (int j = i, len = s.size(); j < len; ++j) { if (skip(i, j)) continue; swap(s[j], s[i]); dfs(i + 1); swap(s[j], s[i]); } } vector<string> ans; string s; vector<string> permutation(string s) { this->s = s; dfs(0); return ans; } /** * [i,j-1]区间中有s[j]的重复项则跳过 */ bool skip(int i, int j) { for (; i < j; ++i) { if (s[i] == s[j]) return true; } return false; } }; #endif //CPP_038__SOLUTION2_H_
ooooo-youwillsee/leetcode
0091-Decode Ways/cpp_0091/Solution1.h
<filename>0091-Decode Ways/cpp_0091/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/4/21 14:03 */ #ifndef CPP_0091__SOLUTION1_H_ #define CPP_0091__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int dfs(string &s, int i) { if (memo.find(i) != memo.end()) return memo[i]; if (i == s.size()) return 1; // 如果当前字符为’0‘,不可能构成字母,返回0 if (s[i] == '0') return 0; int ans = 0; // 分情况讨论, 如果后一位为0,那么必须和前面一位构成10,20,其他不可能构成字母,返回0 if (i + 1 < s.size() && s[i+1] == '0') { int num = stoi(s.substr(i, 2)); if (num == 20 || num == 10) { ans += dfs(s, i+2); }else { ans = 0; } }else { // 正常情况判断, ans += dfs(s, i+1); if (i + 1 < s.size()) { if (stoi(s.substr(i, 2)) <= 26) { ans += dfs(s, i+2); } } } return memo[i] = ans; } // 记忆化 unordered_map<int,int> memo; int numDecodings(string s) { return dfs(s, 0); } }; #endif //CPP_0091__SOLUTION1_H_
ooooo-youwillsee/leetcode
1025-Divisor-Game/cpp_1025/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/6. // #ifndef CPP_1025__SOLUTION1_H_ #define CPP_1025__SOLUTION1_H_ #include <iostream> using namespace std; /** * 归纳法 */ class Solution { public: bool divisorGame(int N) { return N % 2 == 0; } }; #endif //CPP_1025__SOLUTION1_H_
ooooo-youwillsee/leetcode
1579-Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable/cpp_1579/Solution2.h
/** * @author ooooo * @date 2021/1/27 14:16 */ #ifndef CPP_1579__SOLUTION2_H_ #define CPP_1579__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: class UF { public: int n; vector<int> p; UF(int n) : n(n), p(n) { for (int i = 0; i < n; ++i) { p[i] = i; } } int find(int i) { if (p[i] == i) return i; return p[i] = find(p[i]); } bool connected(int i, int j) { int pi = find(i), pj = find(j); if (pi == pj) return true; p[pi] = pj; n--; return false; } int getSize() { return n - 1; } }; int maxNumEdgesToRemove(int n, vector<vector<int>> &edges) { vector<vector<int>> aEdges, bEdges; UF a(n + 1), b(n + 1); int needDelete = 0; for (auto &edge: edges) { int t = edge[0], u = edge[1], v = edge[2]; if (t == 3) { a.connected(u, v); if (b.connected(u, v)) needDelete++; } else if (t == 1) { aEdges.push_back({u, v}); } else if (t == 2) { bEdges.push_back({u, v}); } } for (auto &edge: aEdges) { int u = edge[0], v = edge[1]; if (a.connected(u, v)) { needDelete++; } } if (a.getSize() != 1) return -1; for (auto &edge: bEdges) { int u = edge[0], v = edge[1]; if (b.connected(u, v)) { needDelete++; } } if (b.getSize() != 1) return -1; return needDelete; } }; #endif //CPP_1579__SOLUTION2_H_
ooooo-youwillsee/leetcode
1189-Maximum-Number-of-Balloons/cpp_1189/Solution1.h
<filename>1189-Maximum-Number-of-Balloons/cpp_1189/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/15. // #ifndef CPP_1189__SOLUTION1_H_ #define CPP_1189__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: int maxNumberOfBalloons(string text) { unordered_map<char, int> m; for_each(text.begin(), text.end(), [&m](char x) { m[x]++; }); int ans = 0; string target = "balloon"; while (true) { bool notFound = false; for (auto &c: target) { if (!m[c]) { notFound = true; break; } --m[c]; } if (notFound) break; ans++; } return ans; } }; #endif //CPP_1189__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_062/cpp_062/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/22. // #ifndef CPP_062__SOLUTION1_H_ #define CPP_062__SOLUTION1_H_ #include <iostream> #include <list> using namespace std; /** * timeout */ class Solution { public: int lastRemaining(int n, int m) { list<int> l; for (int i = 0; i < n; ++i) { l.push_back(i); } auto begin = l.begin(); while (l.size() > 1) { for (int i = 1; i < m; ++i) { begin++; if (begin == l.end()) begin = l.begin(); } auto cur = begin; //cout << *cur << "->"; begin++; if (begin == l.end()) begin = l.begin(); l.erase(cur); } return *l.begin(); } }; #endif //CPP_062__SOLUTION1_H_
ooooo-youwillsee/leetcode
0052-N-Queens-II/cpp_0052/Solution2.h
// // Created by ooooo on 2019/11/14. // #ifndef CPP_0052_SOLUTION2_H #define CPP_0052_SOLUTION2_H #include <iostream> #include <vector> using namespace std; class Solution { private: void help(int n, int row, int &res, vector<bool> &cols, vector<bool> &pies, vector<bool> &nas) { if (row >= n) { res += 1; return; } for (int col = 0; col < n; ++col) { int ll = col + row; int rr = row - col + n - 1; if (cols[col] && pies[ll] && nas[rr]) { cols[col] = false; pies[ll] = false; nas[rr] = false; help(n, row + 1, res, cols, pies, nas); cols[col] = true; pies[ll] = true; nas[rr] = true; } } } public: int totalNQueens(int n) { int res = 0; vector<bool> cols(n, true); vector<bool> pies(2 * n - 1, true); vector<bool> nas(2 * n - 1, true); help(n, 0, res, cols, pies, nas); return res; } }; #endif //CPP_0052_SOLUTION2_H
ooooo-youwillsee/leetcode
0378-Kth-Smallest-Element-in-a-Sorted-Matrix/cpp_0378/Solution1.h
/** * @author ooooo * @date 2020/9/29 16:44 */ #ifndef CPP_0378__SOLUTION1_H_ #define CPP_0378__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int kthSmallest(vector<vector<int>> &matrix, int k) { int row = matrix.size(), col = matrix[0].size(); vector<int> vec(row * col, 0); for (int i = 0, p = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { vec[p++] = matrix[i][j]; } } sort(vec.begin(), vec.end()); return vec[k - 1]; } }; #endif //CPP_0378__SOLUTION1_H_
ooooo-youwillsee/leetcode
0376-Wiggle Subsequence/cpp_0376/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/12/12 13:53 */ #ifndef CPP_0376__SOLUTION2_H_ #define CPP_0376__SOLUTION2_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<int> up(n, 1), down(n, 1); for (int i = 1; i < n; ++i) { if (nums[i] > nums[i - 1]) { up[i] = max(up[i - 1], down[i - 1] + 1); down[i] = down[i - 1]; } else if (nums[i] < nums[i - 1]) { down[i] = max(down[i - 1], up[i - 1] + 1); up[i] = up[i - 1]; }else { up[i] = up[i - 1]; down[i] = down[i - 1]; } } return max(up[n - 1], down[n - 1]); } }; #endif //CPP_0376__SOLUTION2_H_
ooooo-youwillsee/leetcode
0152-Maximum-Product-Subarray/cpp_0152/Solution1.h
// // Created by ooooo on 2019/12/19. // #ifndef CPP_0152_SOLUTION1_H #define CPP_0152_SOLUTION1_H #include <iostream> #include <climits> #include <vector> using namespace std; class Solution { public: int maxProduct(vector<int> &nums) { int res = INT_MIN; int iMax = 1; int iMin = 1; for (const auto &num : nums) { if (num < 0) { int temp = iMax; iMax = iMin; iMin = temp; } iMax = max(iMax * num, num); iMin = min(iMin * num, num); res = max(res, iMax); } return res; } }; #endif //CPP_0152_SOLUTION1_H
ooooo-youwillsee/leetcode
lcof_056-2/cpp_056-2/Solution1.h
// // Created by ooooo on 2020/4/17. // #ifndef CPP_056_2__SOLUTION1_H_ #define CPP_056_2__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * hash */ class Solution { public: int singleNumber(vector<int> &nums) { unordered_map<int, int> m; for (auto &num: nums) m[num]++; for (auto[k, v] :m) { if (v == 1) return k; } return -1; } }; #endif //CPP_056_2__SOLUTION1_H_
ooooo-youwillsee/leetcode
0153-Find-Minimum-in-Rotated-Sorted-Array/cpp_0153/Solution1.h
<filename>0153-Find-Minimum-in-Rotated-Sorted-Array/cpp_0153/Solution1.h /** * @author ooooo * @date 2020/9/25 23:54 */ #ifndef CPP_0153__SOLUTION1_H_ #define CPP_0153__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findMin(vector<int> &nums) { return *min_element(nums.begin(), nums.end()); } }; #endif //CPP_0153__SOLUTION1_H_
ooooo-youwillsee/leetcode
0682-Baseball-Game/cpp_0682/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0682_SOLUTION1_H #define CPP_0682_SOLUTION1_H #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: int calPoints(vector<string> &ops) { stack<int> s; for (auto item: ops) { if (item == "+") { int first = s.top(); s.pop(); int second = s.top(); s.push(first); s.push(first + second); } else if (item == "D") { s.push(s.top() * 2); } else if (item == "C") { s.pop(); } else { s.push(stoi(item)); } } int sum = 0; while (!s.empty()) { sum += s.top(); s.pop(); } return sum; } }; #endif //CPP_0682_SOLUTION1_H
ooooo-youwillsee/leetcode
lcof_057/cpp_057/Solution1.h
// // Created by ooooo on 2020/4/15. // #ifndef CPP_057__SOLUTION1_H_ #define CPP_057__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> twoSum(vector<int> &nums, int target) { auto it = lower_bound(nums.begin(), nums.end(), target); auto l = nums.begin(), r = nums.end() - 1; if (it != nums.end()) r = it - 1; while (l != r) { int ans = *l + *r; if (ans == target) return {*l, *r}; else if (ans < target) l++; else r--; } return {}; } }; #endif //CPP_057__SOLUTION1_H_
ooooo-youwillsee/leetcode
1189-Maximum-Number-of-Balloons/cpp_1189/Solution2.h
// // Created by ooooo on 2020/1/15. // #ifndef CPP_1189__SOLUTION2_H_ #define CPP_1189__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: // balloon int maxNumberOfBalloons(string text) { unordered_map<char, int> m = { {'b', 0}, {'a', 0}, {'l', 0}, {'o', 0}, {'n', 0} }; for (auto &item: text) { if (m.count(item)) m[item]++; } m['o'] /= 2; m['l'] /= 2; int ans = INT_MAX; vector<int > vec; for_each(m.begin(), m.end(), [&ans](auto entry) { ans = min(ans, entry.second); }); return ans; } }; #endif //CPP_1189__SOLUTION2_H_
ooooo-youwillsee/leetcode
0783-Minimum-Distance-Between-BST-Nodes/cpp_0783/Solution2.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_0783_SOLUTION2_H #define CPP_0783_SOLUTION2_H #include "TreeNode.h" #include <climits> class Solution { public: TreeNode *prev = nullptr; int min = INT_MAX; void dfs(TreeNode *node) { if (!node) return; dfs(node->left); if (prev) { min = std::min(min, node->val - prev->val); } prev = node; dfs(node->right); } int minDiffInBST(TreeNode *root) { if (!root) return 0; dfs(root); return min; } }; #endif //CPP_0783_SOLUTION2_H
ooooo-youwillsee/leetcode
lcof_040/cpp_040/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 // // Created by ooooo on 2020/3/25. // #ifndef CPP_040__SOLUTION1_H_ #define CPP_040__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; /** * 堆 */ class Solution { public: vector<int> getLeastNumbers(vector<int> &arr, int k) { if (k == 0) return {}; priority_queue<int> q; for (auto &num: arr) { if (q.size() < k) { q.push(num); } else { if (q.top() > num) { q.pop(); q.push(num); } } } vector<int> ans; for (int i = 0; i < k; ++i) { ans.push_back(q.top()); q.pop(); } return ans; } }; #endif //CPP_040__SOLUTION1_H_
ooooo-youwillsee/leetcode
0520-Detect-Capital/cpp_0520/Solution1.h
// // Created by ooooo on 2020/1/26. // #ifndef CPP_0520__SOLUTION1_H_ #define CPP_0520__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: bool detectCapitalUse(string word) { int i = 0; bool flag = false; for (int j = 0; j < word.size(); ++j) { if (isupper(word[j])) { i++; if (j == 0) flag = true; } } if (i == word.size() || i == 0) return true; else if (i == 1 && flag) return true; return false; } }; #endif //CPP_0520__SOLUTION1_H_
ooooo-youwillsee/leetcode
0179-Largest Number/cpp_0179/Solution1.h
/** * @author ooooo * @date 2021/4/12 17:16 */ #ifndef CPP_0179__SOLUTION1_H_ #define CPP_0179__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string largestNumber(vector<int> &nums) { int n = nums.size(); vector<string> s; for (int i = 0; i < n; i++) { string numStr = to_string(nums[i]); s.push_back(numStr); } sort(s.begin(), s.end(), [&](const auto &s1, const auto &s2) { return s2 + s1 < s1 + s2; }); string ans = ""; for (int i = 0; i < n; i++) { ans += s[i]; } if (ans[0] == '0') return "0"; return ans; } }; #endif //CPP_0179__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_055-1/cpp_055-1/Solution3.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/11. // #ifndef CPP_055_1__SOLUTION3_H_ #define CPP_055_1__SOLUTION3_H_ #include "TreeNode.h" #include <queue> /** * dfs */ class Solution { public: int maxDepth(TreeNode *root) { if (!root) return 0; return max(maxDepth(root->left), maxDepth(root->right)) + 1; } }; #endif //CPP_055_1__SOLUTION3_H_
ooooo-youwillsee/leetcode
lcof_061/cpp_061/Solution1.h
// // Created by ooooo on 2020/4/21. // #ifndef CPP_061__SOLUTION1_H_ #define CPP_061__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool isStraight(vector<int> &nums) { sort(nums.begin(), nums.end()); int count_0 = 0, len = nums.size(); for (int i = 0; i < len && nums[i] == 0; ++i) { count_0++; } int gap = 0; for (int j = count_0 + 1; j < len; ++j) { if (nums[j] == nums[j - 1]) return false; gap += nums[j] - nums[j - 1] - 1; } return gap <= count_0; } }; #endif //CPP_061__SOLUTION1_H_
ooooo-youwillsee/leetcode
1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp_1290/Solution1.h
// // Created by ooooo on 2020/1/23. // #ifndef CPP_1290__SOLUTION1_H_ #define CPP_1290__SOLUTION1_H_ #include "ListNode.h" class Solution { public: int getDecimalValue(ListNode *head) { string s = ""; while (head) { s += to_string(head->val); head = head->next; } return stoi(s, 0, 2); } }; #endif //CPP_1290__SOLUTION1_H_
ooooo-youwillsee/leetcode
0234-Palindrome-Linked-List/cpp_0234/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/8. // #ifndef CPP_0234_SOLUTION1_H #define CPP_0234_SOLUTION1_H #include "ListNode.h" #include <vector> /** * 循环 */ class Solution { public: bool isPalindrome(ListNode *head) { vector<int> ans; while (head) { ans.push_back(head->val); head = head->next; } for (int i = 0, j = ans.size() - 1; i < j; ++i, --j) { if (ans[i] != ans[j]) return false; } return true; } }; #endif //CPP_0234_SOLUTION1_H
ooooo-youwillsee/leetcode
lcof_053-1/cpp_053-1/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/9. // #ifndef CPP_053_1__SOLUTION2_H_ #define CPP_053_1__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int low_bound(vector<int> &nums, int target) { int l = 0, r = nums.size(); while (l < r) { int mid = l + (r - l + 1) / 2; if (nums[mid] == target) { r = mid; } else if (nums[mid] < target) { l = mid; } } return nums[l] == target ? l : -1; } int up_bound(vector<int> &nums, int target) { int l = 0, r = nums.size(); while (l < r) { int mid = l + (r - l) / 2; if (nums[mid] > target) { r = mid; } else { l = mid + 1; } } return nums[l] == target ? l : -1; } int search(vector<int> &nums, int target) { auto it = lower_bound(nums.begin(), nums.end(), target); if (it == nums.end()) return 0; return upper_bound(nums.begin(), nums.end(), target) - it; } }; #endif //CPP_053_1__SOLUTION2_H_
ooooo-youwillsee/leetcode
0459-Repeated-Substring-Pattern/cpp_0459/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/26. // #ifndef CPP_0459__SOLUTION1_H_ #define CPP_0459__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: bool repeatedSubstringPattern(string s) { //for (int i = 1; i <= s.size() / 2; ++i) { // 从高向低 for (int i = s.size() / 2; i >= 1; --i) { // 子串必须要重复,所以要被整除 if (s.size() % i != 0) continue; bool find = true; string target = s.substr(0, i); for (int j = i; j < s.size(); j += i) { if (target != s.substr(j, i)) { find = false; break; } } if (find) { return true; } } return false; } }; #endif //CPP_0459__SOLUTION1_H_
ooooo-youwillsee/leetcode
0874-Walking-Robot-Simulation/cpp_0874/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/30 14:19 */ #ifndef CPP_0874__SOLUTION1_H_ #define CPP_0874__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: vector<vector<int>> dx_dy = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; struct pair_hash { size_t operator()(pair<int, int> __v) const { return hash<int>{}(__v.first) * 10 + hash<int>{}(__v.second); } }; int robotSim(vector<int> &commands, vector<vector<int>> &obstacles) { int d = 1; int x = 0, y = 0; int ans = 0; unordered_set<pair<int, int>, pair_hash> s; for (auto &item : obstacles) { s.insert(make_pair(item[0], item[1])); } for (auto &c : commands) { if (c == -1) { d = (d + 3) % 4; } else if (c == -2) { d = (d + 1) % 4; } else { for (int i = 1; i <= c; ++i) { int nx = x + dx_dy[d][0], ny = y + dx_dy[d][1]; if (s.find(make_pair(nx, ny)) == s.end()) { x = nx; y = ny; ans = max(ans, x * x + y * y); } else { break; } } } } return ans; } }; #endif //CPP_0874__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_057-2/cpp_057-2/Solution1.h
// // Created by ooooo on 2020/4/16. // #ifndef CPP_057_2__SOLUTION1_H_ #define CPP_057_2__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> group(int l, int r) { vector<int> ans; for (; l < r; l++) { if (l != 0) ans.push_back(l); } return ans; } vector<vector<int>> findContinuousSequence(int target) { int l = 0, r = 0, sum = 0, end = (target + 1) / 2; vector<vector<int>> ans; while (l <= end) { if (sum < target) { sum += r++; } else if (sum == target) { ans.push_back(group(l, r)); sum += r++; } else { sum -= l++; } } return ans; } }; #endif //CPP_057_2__SOLUTION1_H_
ooooo-youwillsee/leetcode
1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp_1577/Solution1.h
<filename>1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp_1577/Solution1.h /** * @author ooooo * @date 2020/9/7 15:09 */ #ifndef CPP_1577__SOLUTION1_H_ #define CPP_1577__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * timeout */ class Solution1 { public: int compute(vector<int> &nums1, vector<int> &nums2) { int count = 0; for (int i = 0; i < nums1.size(); ++i) { long long sum = (long long) nums1[i] * (long long) nums1[i]; for (int j = 0; j < nums2.size(); ++j) { for (int k = j + 1; k < nums2.size(); ++k) { if (sum == (long long) nums2[j] * (long long) nums2[k]) { count++; } } } } return count; } int numTriplets(vector<int> &nums1, vector<int> &nums2) { return compute(nums1, nums2) + compute(nums2, nums1); } }; #endif //CPP_1577__SOLUTION1_H_
ooooo-youwillsee/leetcode
0947-Most Stones Removed with Same Row or Column/cpp_0947/Solution1.h
/** * @author ooooo * @date 2021/1/15 18:53 */ #ifndef CPP_0947__SOLUTION1_H_ #define CPP_0947__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: int ans = 0; void bfs(int rootIndex, unordered_set<int> &visited, unordered_map<int, vector<int>> &x, unordered_map<int, vector<int>> &y, vector<vector<int>> &stones) { queue<int> q; q.push(rootIndex); while (!q.empty()) { auto i = q.front(); q.pop(); if (visited.count(i)) continue; ans++; visited.insert(i); // 取对应的x值 for (auto &j:x[stones[i][0]]) { if (!visited.count(j)) q.push(j); } // 取对应的y值 for (auto &j:y[stones[i][1]]) { if (!visited.count(j)) q.push(j); } } // 除去自己,自己不能删除 ans--; } int removeStones(vector<vector<int>> &stones) { unordered_map<int, vector<int>> x, y; for (int i = 0; i < stones.size(); ++i) { x[stones[i][0]].push_back(i); y[stones[i][1]].push_back(i); } unordered_set<int> visited; for (int i = 0; i < stones.size(); ++i) { if (visited.count(i)) continue; bfs(i, visited, x, y, stones); } return ans; } }; #endif //CPP_0947__SOLUTION1_H_
ooooo-youwillsee/leetcode
0842-Split Array into Fibonacci Sequence/cpp_0842/Solution1.h
/** * @author ooooo * @date 2020/12/8 19:08 */ #ifndef CPP_0842__SOLUTION1_H_ #define CPP_0842__SOLUTION1_H_ #include <iostream> #include <vector> #include <math.h> using namespace std; class Solution { public: long max_v = (int) (pow(2, 31) - 1); bool dfs(vector<int> &nums, string &s, int i) { if (i >= s.size()) return true; long next_num = (long) nums[nums.size() - 1] + (long) nums[nums.size() - 2]; // 结果数超过最大值 返回false if (next_num > max_v) return false; string next = to_string(next_num); if (s.substr(i, next.size()) == next) { nums.push_back(next_num); if (dfs(nums, s, i + next.size())) return true; // 回溯 nums.pop_back(); } return false; } vector<int> splitIntoFibonacci(string s) { vector<int> nums; int max_v_len = to_string(max_v).size(); int n = s.size(); // 取第一个数和第二个数 for (int i = 1; i < n; ++i) { for (int j = 1; j < n; ++j) { // 剪枝操作, n - i - j 表示剩余数的长度至少为 max(i, j) if (i > max_v_len || j > max_v_len || n - i - j < max(i, j)) { continue; } long first = stol(s.substr(0, i)); long second = stol(s.substr(i, j)); // 有一个数超过最大值 continue if (first > max_v || second > max_v || first + second > max_v) continue; nums.push_back(first); nums.push_back(second); // 有成功的组合,就返回 if (dfs(nums, s, i + j)) return nums; // 回溯 nums.pop_back(); nums.pop_back(); } } return nums; } }; #endif //CPP_0842__SOLUTION1_H_
ooooo-youwillsee/leetcode
1784-Check if Binary String Has at Most One Segment of Ones/cpp_1784/Solution1.h
/** * @author ooooo * @date 2021/3/14 15:00 */ #ifndef CPP_1784__SOLUTION1_H_ #define CPP_1784__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: bool checkOnesSegment(string s) { int n = s.size(); int l = 0, r = n - 1; while (l <= r && s[l] == '0') { l++; } while (l <= r && s[r] == '0') { r--; } if (s[r] != '1' || s[l] != '1') { return false; } for (int i = l; i <= r; ++i) { if (s[i] == '0') { return false; } } return true; } }; #endif //CPP_1784__SOLUTION1_H_
ooooo-youwillsee/leetcode
1021-Remove-Outermost-Parentheses/cpp_1021/Solution1.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_1021_SOLUTION1_H #define CPP_1021_SOLUTION1_H #include <iostream> #include <stack> using namespace std; class Solution { public: string removeOuterParentheses(string S) { stack<char> ss; string ans = "", mid = ""; for (auto c: S) { mid += c; if (c == '(') ss.push(c); else { ss.pop(); if (ss.empty()) { ans += mid.substr(1, mid.size() - 2); mid = ""; } } } return ans; } }; #endif //CPP_1021_SOLUTION1_H
ooooo-youwillsee/leetcode
0018-4Sum/cpp_0018/Solution3.h
/** * @author ooooo * @date 2020/10/5 10:30 */ #ifndef CPP_0018__SOLUTION3_H_ #define CPP_0018__SOLUTION3_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: struct vec_hash { size_t operator()(vector<int> __v) const { size_t x = 0; for (int i = 0; i < __v.size(); ++i) { x += hash<int>{}(__v[i]) * (i << i); } return x; } }; vector<vector<int>> fourSum(vector<int> &nums, int target) { unordered_set<vector<int>, vec_hash> ans; sort(nums.begin(), nums.end()); int n = nums.size(); for (int i = 0; i < n - 3; ++i) { for (int j = i + 1; j < n - 2; ++j) { int l = j + 1, r = n - 1; while (l < r) { int sum = nums[i] + nums[j] + nums[l] + nums[r]; if (sum == target) { ans.insert({nums[i], nums[j], nums[l], nums[r]}); l++; r--; } else if (sum < target) { l++; } else { r--; } } } } return vector<vector<int>>(ans.begin(), ans.end()); } }; #endif //CPP_0018__SOLUTION3_H_
ooooo-youwillsee/leetcode
0061-Rotate List/cpp_0061/ListNode.h
/** * @author ooooo * @date 2020/10/5 18:03 */ #ifndef CPP_0061__LISTNODE_H_ #define CPP_0061__LISTNODE_H_ #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; #endif //CPP_0061__LISTNODE_H_
ooooo-youwillsee/leetcode
0563-Binary-Tree-Tilt/cpp_0563/Solution1.h
<filename>0563-Binary-Tree-Tilt/cpp_0563/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2020/1/3. // #ifndef CPP_0563_SOLUTION1_H #define CPP_0563_SOLUTION1_H #include "TreeNode.h" class Solution { public: int res = 0; int dfs(TreeNode *node) { if (!node) return 0; int leftSum = dfs(node->left); int rightSum = dfs(node->right); res += abs(leftSum - rightSum); return leftSum + rightSum + node->val; } int findTilt(TreeNode *root) { dfs(root); return res; } }; #endif //CPP_0563_SOLUTION1_H
ooooo-youwillsee/leetcode
0069-Sqrt-x/cpp_0069/Solution1.h
// // Created by ooooo on 2019/11/21. // #ifndef CPP_0069_SOLUTION1_H #define CPP_0069_SOLUTION1_H class Solution { public: int mySqrt(int x) { if (x == 0 ) return 0; for (int i = 1; i <= x; ++i) { if (i == x / i) return i; if (i > x / i) return i - 1; } return -1; } }; #endif //CPP_0069_SOLUTION1_H
ooooo-youwillsee/leetcode
0142-Linked-List-Cycle-II/cpp_0142/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/17. // #ifndef CPP_0142__SOLUTION1_H_ #define CPP_0142__SOLUTION1_H_ #include "LIstNode.h" #include <unordered_set> /** * 哈希 */ class Solution { public: ListNode *detectCycle(ListNode *head) { unordered_set<ListNode *> set; while(head) { if (set.count(head)) { return head; } set.insert(head); head = head->next; } return nullptr; } }; #endif //CPP_0142__SOLUTION1_H_
ooooo-youwillsee/leetcode
0968-Binary-Tree-Cameras/cpp_0968/Solution1.h
<filename>0968-Binary-Tree-Cameras/cpp_0968/Solution1.h /** * @author ooooo * @date 2020/9/22 11:02 */ #ifndef CPP_0968__SOLUTION1_H_ #define CPP_0968__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: // 0:已被覆盖 // 1:需要安装 // 2: 已安装 int dfs(TreeNode *node) { if (!node) return 0; int l = dfs(node->left); int r = dfs(node->right); if (l == 1 || r == 1) { ans +=1; return 2; } if (l == 2 || r == 2) { return 0; } return 1; } int ans = 0; int minCameraCover(TreeNode *root) { if (!root) return 0; if (dfs(root) == 1) ans ++; return ans; } }; #endif //CPP_0968__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_025/cpp_025/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/16. // #ifndef CPP_025__SOLUTION1_H_ #define CPP_025__SOLUTION1_H_ #include "ListNode.h" class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (!l1) return l2; if (!l2) return l1; ListNode *dummyHead = new ListNode(0), *cur = dummyHead; while (l1 && l2) { if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } if (l1) { cur->next = l1; } if (l2) { cur->next = l2; } return dummyHead->next; } }; #endif //CPP_025__SOLUTION1_H_
ooooo-youwillsee/leetcode
0012-Integer-to-Romanl/cpp_0012/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/25 13:43 */ #ifndef CPP_0012__SOLUTION1_H_ #define CPP_0012__SOLUTION1_H_ #include <iostream> #include <unordered_map> #include <vector> using namespace std; class Solution { public: string intToRoman(int num) { vector<string> vec; int a = num % 10; if (a == 4) vec.emplace_back("IV"); else if (a == 9) vec.emplace_back("IX"); else if (a >= 5) vec.emplace_back("V" + string(a - 5, 'I')); else vec.emplace_back(string(a, 'I')); num -= a; if (num > 0) { int b = num % 100; if (b == 40) vec.emplace_back("XL"); else if (b == 90) vec.emplace_back("XC"); else if (a >= 50) vec.emplace_back("L" + string((b - 50) / 10, 'X')); else vec.emplace_back(string(b, 'X')); num -= b; } if (num > 0) { int c = num % 1000; if (c == 400) vec.emplace_back("CD"); else if (c == 900) vec.emplace_back("CM"); else if (c >= 500) vec.emplace_back("D" + string((c - 500) / 100, 'C')); else vec.emplace_back(string(c, 'C')); num -= c; } vec.emplace_back(string(num / 1000, 'M')); reverse(vec.begin(), vec.end()); string s; for (auto &item : vec) { s += item; } return s; } }; #endif //CPP_0012__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_047/cpp_047/Solution2.h
// // Created by ooooo on 2020/4/3. // #ifndef CPP_047__SOLUTION2_H_ #define CPP_047__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i][j] = max( dp[i][j-1], dp[i-1][j] ) + grid[i][j] */ class Solution { public: int maxValue(vector<vector<int>> &grid) { if (grid.empty()) return 0; int m = grid.size(), n = grid[0].size(); vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) + grid[i-1][j-1]; } } return dp[m][n]; } }; #endif //CPP_047__SOLUTION2_H_
ooooo-youwillsee/leetcode
1716-Calculate Money in Leetcode Bank/cpp_1716/Solution1.h
/** * @author ooooo * @date 2021/1/20 12:14 */ #ifndef CPP_1716__SOLUTION1_H_ #define CPP_1716__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int totalMoney(int n) { int prev = 0, count = 1; int ans = 0; while (n) { ans += count + prev; count++; if (count == 8) { count = 1; prev++; } n--; } return ans; } }; #endif //CPP_1716__SOLUTION1_H_
ooooo-youwillsee/leetcode
1052-Grumpy Bookstore Owner/cpp_1052/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2021/2/23 13:40 */ #ifndef CPP_1052__SOLUTION1_H_ #define CPP_1052__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxSatisfied(vector<int> &customers, vector<int> &grumpy, int x) { int n = customers.size(); // preSum 表示不生气 preGrumpSum表示生气 vector<int> preSum(n + 1), preGrumpSum(n + 1); for (int i = 0; i < n; ++i) { preSum[i + 1] = preSum[i] + customers[i]; preGrumpSum[i + 1] = preGrumpSum[i] + (grumpy[i] == 1 ? 0 : customers[i]); } int ans = 0; for (int i = 0; i + x - 1 < n; ++i) { ans = max(ans, preSum[i + x] - preSum[i] + preGrumpSum[i] + preGrumpSum[n] - preGrumpSum[i + x]); } return ans; } }; #endif //CPP_1052__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcp-028/cpp_028/Solution2.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/4/10 20:28 */ #ifndef CPP_028__SOLUTION2_H_ #define CPP_028__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; int purchasePlans(vector<int> nums, int target) { sort(nums.begin(), nums.end()); int n = nums.size(); int mod = 1e9 + 7; long long ans = 0; for (int i = 0; i < n - 1; ++i) { auto it = upper_bound(nums.begin() + i + 1, nums.end(), target - nums[i]); long cnt = 0; if (it != nums.end() && *it == target - nums[i]) { cnt = it - (nums.begin() + i); } else if (it == nums.end()) { int j = n - 1; for (; j > i; --j) { if (nums[j] + nums[i] <= target) { break; } } cnt = j - i; } else if (it != nums.end()) { cnt = (it - 1) - (nums.begin() + i); } ans = (ans + cnt) % mod; } return ans; } #endif //CPP_028__SOLUTION2_H_
ooooo-youwillsee/leetcode
0144-Binary-Tree-Preorder-Traversal/cpp_0144/Solution1.h
/** * @author ooooo * @date 2020/9/25 23:15 */ #ifndef CPP_0144__SOLUTION1_H_ #define CPP_0144__SOLUTION1_H_ #include "TreeNode.h" /** * color mark */ class Solution { public: vector<int> preorderTraversal(TreeNode *root) { vector<int> ans; if (!root) return ans; stack<pair<TreeNode *, bool>> s; s.push({root, false}); while (!s.empty()) { auto[node, visited] = s.top(); s.pop(); if (visited) { ans.emplace_back(node->val); } else { if (node->right) s.push({node->right, false}); if (node->left) s.push({node->left, false}); s.push({node, true}); } } return ans; } }; #endif //CPP_0144__SOLUTION1_H_
ooooo-youwillsee/leetcode
0589-N-ary-Tree-Preorder-Traversal/cpp_0589/Node.h
// // Created by ooooo on 2020/1/3. // #ifndef CPP_0589_NODE_H #define CPP_0589_NODE_H #include <iostream> #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 = _val; children = _children; } }; #endif //CPP_0589_NODE_H
ooooo-youwillsee/leetcode
1779-Find Nearest Point That Has the Same X or Y Coordinate/cpp_1779/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>1779-Find Nearest Point That Has the Same X or Y Coordinate/cpp_1779/Solution1.h /** * @author ooooo * @date 2021/3/14 14:53 */ #ifndef CPP_1779__SOLUTION1_H_ #define CPP_1779__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: int nearestValidPoint(int x, int y, vector<vector<int>> &points) { unordered_map<int, vector<int>> m; int minDiff = INT_MAX; for (int i = 0; i < points.size(); ++i) { if (points[i][0] == x || points[i][1] == y) { int diff = abs(points[i][0] - x) + abs(points[i][1] - y); minDiff = min(minDiff, diff); m[diff].push_back(i); } } if (minDiff == INT_MAX) { return -1; } return m[minDiff][0]; } }; #endif //CPP_1779__SOLUTION1_H_
ooooo-youwillsee/leetcode
0403-Frog Jump/cpp_0403/Solution2.h
<reponame>ooooo-youwillsee/leetcode<filename>0403-Frog Jump/cpp_0403/Solution2.h /** * @author ooooo * @date 2021/5/1 20:05 */ #ifndef CPP_0403__SOLUTION2_H_ #define CPP_0403__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool canCross(vector<int> &stones) { int n = stones.size(); vector<vector<int>> dp(n, vector<int>(n)); dp[0][0] = true; for (int i = 1; i < n; i++) { for (int j = i - 1; j >= 0; j--) { int step = stones[i] - stones[j]; if (step <= j + 1) { dp[i][step] = dp[j][step] || dp[j][step - 1] || dp[j][step + 1]; } } } for (int i = 0; i < n; i++) { if (dp[n - 1][i]) return true; } return false; } }; #endif //CPP_0403__SOLUTION2_H_
ooooo-youwillsee/leetcode
lcof_056-2/cpp_056-2/Solution2.h
// // Created by ooooo on 2020/4/17. // #ifndef CPP_056_2__SOLUTION2_H_ #define CPP_056_2__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int singleNumber(vector<int> &nums) { vector<int> bits(32, 0); for (auto &num: nums) { unsigned int bit_1 = 1; for (int j = 31; j >= 0; --j) { if (num & bit_1) bits[j]++; bit_1 <<= 1; } } int ans = 0; for (auto &bit :bits) { ans = (ans << 1) + bit % 3; } return ans; } }; #endif //CPP_056_2__SOLUTION2_H_
ooooo-youwillsee/leetcode
1014-Best-Sightseeing-Pair/cpp_1014/Solution1.h
<filename>1014-Best-Sightseeing-Pair/cpp_1014/Solution1.h /** * @author ooooo * @date 2021/2/11 14:39 */ #ifndef CPP_1014__SOLUTION1_H_ #define CPP_1014__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxScoreSightseeingPair(const vector<int> &A) { int n = A.size(); vector<int> maxNums(n, 0); maxNums[n - 1] = A[n - 1] - (n - 1); for (int i = A.size() - 2; i >= 0; --i) { maxNums[i] = max(A[i] - i, maxNums[i + 1]); } int ans = INT_MIN, cur = 0; for (int i = 0; i < A.size() - 1; ++i) { cur = max(cur, A[i] + i); ans = max(ans, cur + maxNums[i + 1]); } return ans; } }; #endif //CPP_1014__SOLUTION1_H_
ooooo-youwillsee/leetcode
1658-Minimum Operations to Reduce X to Zero/cpp_1658/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/1 17:21 */ #ifndef CPP_1658__SOLUTION1_H_ #define CPP_1658__SOLUTION1_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; class Solution { public: int minOperations(vector<int> &nums, int x) { int target = accumulate(nums.begin(), nums.end(), 0) - x; int l = 0, r = 0; int n = nums.size(); int ans = -1, sum = 0; while (r < n) { sum += nums[r]; while (sum > target && l <= r) { sum -= nums[l]; l++; } if (sum == target) { ans = max(ans, r - l + 1); } r++; } return ans == -1 ? -1 : n - ans; } }; #endif //CPP_1658__SOLUTION1_H_
ooooo-youwillsee/leetcode
1546-Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/cpp_1546/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/9 17:55 */ #ifndef CPP_1546__SOLUTION2_H_ #define CPP_1546__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int maxNonOverlapping(vector<int> &nums, int target) { int n = nums.size(); int sum = 0; int ans = 0; // 刚开始前缀和为0 unordered_set<int> set = {0}; for (int i = 0; i < n; ++i) { sum += nums[i]; if (set.count(sum - target)) { ans++; set.clear(); } set.insert(sum); } return ans; } }; #endif //CPP_1546__SOLUTION2_H_
ooooo-youwillsee/leetcode
0669-Trim-a-Binary-Search-Tree/cpp_0669/Solution2.h
<filename>0669-Trim-a-Binary-Search-Tree/cpp_0669/Solution2.h // // Created by ooooo on 2020/1/3. // #ifndef CPP_0669_SOLUTION2_H #define CPP_0669_SOLUTION2_H #include "TreeNode.h" class Solution { public: TreeNode *trimBST(TreeNode *root, int L, int R) { if (!root) return nullptr; if (root->val > R) return trimBST(root->left, L, R); if (root->val < L) return trimBST(root->right, L, R); root->left = trimBST(root->left, L, R); root->right = trimBST(root->right, L, R); return root; } }; #endif //CPP_0669_SOLUTION2_H
ooooo-youwillsee/leetcode
1802-Maximum Value at a Given Index in a Bounded Array/cpp_1802/Solution1.h
/** * @author ooooo * @date 2021/3/28 12:19 */ #ifndef CPP_1802__SOLUTION1_H_ #define CPP_1802__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: int maxValue(int n, int index, int maxSum) { int l = 1, r = maxSum; int ans = 0; while (l <= r) { int mid = l + (r - l) / 2; if (check(mid, index, n, maxSum)) { l = mid + 1; ans = mid; } else { r = mid - 1; } } return ans; } bool check(long long mid, long long index, long long n, long long target) { long long sum = mid; if (index < mid - 1) { sum += (mid - index + mid - 1) * index / 2; } else if (index >= mid - 1) { sum += (index - mid + 1) + (1 + mid - 1) * (mid - 1) / 2; } if (n - index - 1 < mid - 1) { sum += (mid - n + index + 1 + mid - 1) * (n - index - 1) / 2; } else { sum += (n - index - 1 - mid + 1) + (1 + mid - 1) * (mid - 1) / 2; } return sum <= target; } }; #endif //CPP_1802__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_059-1/cpp_059-1/Solution1.h
// // Created by ooooo on 2020/4/19. // #ifndef CPP_059_1__SOLUTION1_H_ #define CPP_059_1__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> maxSlidingWindow(vector<int> &nums, int k) { if (k == 0 && nums.empty()) return {}; vector<int> ans; int max_v = *max_element(nums.begin(), nums.begin() + k); ans.push_back(max_v); for (int i = k, size = nums.size(); i < size; ++i) { if (nums[i] > max_v) { max_v = nums[i]; } else if (nums[i - k] == max_v) { max_v = *max_element(nums.begin() + i - k + 1, nums.begin() + i + 1); } ans.push_back(max_v); } return ans; } }; #endif //CPP_059_1__SOLUTION1_H_
ooooo-youwillsee/leetcode
1024-Video Stitching/cpp_1024/Solution2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2020/10/24 11:55 */ #ifndef CPP_1024__SOLUTION2_H_ #define CPP_1024__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i] 表示0到i区间的最小数目 */ class Solution { public: int videoStitching(vector<vector<int>> &clips, int T) { vector<int> dp(T + 1); for (int i = 1; i <= T; ++i) { dp[i] = clips.size() + 1; for (auto &clip : clips) { if (clip[0] < i && i <= clip[1]) { dp[i] = min(dp[i], dp[clip[0]] + 1); } } //cout << "finally i:" << i << " ->" << dp[i] << " " << endl; } return dp[T] > clips.size() ? -1 : dp[T]; } }; #endif //CPP_1024__SOLUTION2_H_
ooooo-youwillsee/leetcode
1706-Where Will the Ball Fall/cpp_1706/Solution1.h
/** * @author ooooo * @date 2021/1/24 17:15 */ #ifndef CPP_1706__SOLUTION1_H_ #define CPP_1706__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <numeric> using namespace std; class Solution { public: vector<int> findBall(vector<vector<int>> &g) { int m = g.size(), n = g[0].size(); vector<int> ans; for (int i = 0; i < n; ++i) { int row = 0, col = i; while (row < m) { if ((col + 1 < n && g[row][col] == 1 && g[row][col + 1] == 1)) { col++; } else if ((col - 1 >= 0 && g[row][col] == -1 && g[row][col - 1] == -1)) { col--; } else { col = -1; break; } row++; } ans.push_back(col); } return ans; } }; #endif //CPP_1706__SOLUTION1_H_
ooooo-youwillsee/leetcode
1047-Remove-All-Adjacent-Duplicates-In-String/cpp_1047/Solution1.h
// // Created by ooooo on 2020/1/5. // #ifndef CPP_1047_SOLUTION1_H #define CPP_1047_SOLUTION1_H #include <iostream> #include <stack> using namespace std; class Solution { public: string removeDuplicates(string S) { stack<char> ss; for (auto c:S) { if (!ss.empty() && c == ss.top()) { ss.pop(); } else { ss.push(c); } } string ans = ""; while (!ss.empty()) { ans = ss.top() + ans; ss.pop(); } return ans; } }; #endif //CPP_1047_SOLUTION1_H
ooooo-youwillsee/leetcode
0538-Convert-BST-to_Greater-Tree/cpp_0538/Solution3.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/9/21 13:16 */ #ifndef CPP_0538__SOLUTION3_H_ #define CPP_0538__SOLUTION3_H_ #include "TreeNode.h" class Solution { public: TreeNode *prev; TreeNode *convertBST(TreeNode *root) { if (!root) return root; convertBST(root->right); if (!prev) { prev = root; } else { root->val += prev->val; prev = root; } convertBST(root->left); return root; } }; #endif //CPP_0538__SOLUTION3_H_
ooooo-youwillsee/leetcode
0316-Remove Duplicate Letters/cpp_0316/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0316-Remove Duplicate Letters/cpp_0316/Solution1.h /** * @author ooooo * @date 2020/12/20 12:05 */ #ifndef CPP_0316__SOLUTION1_H_ #define CPP_0316__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> #include <unordered_map> #include <unordered_set> using namespace std; /** * 可以用双端队列或者数组加快速度 */ class Solution { public: string removeDuplicateLetters(string s) { stack<char> stack; // 计数 unordered_map<char, int> m; // 用于判断栈中字符是否存在 unordered_set<char> set; for (auto c: s) { m[c]++; } for (auto c: s) { // 栈顶的元素大于当前的元素 && 栈中不包含当前元素 , 特例: "abacb" // m[stack.top()] > 0 表示在后面的字符串中还有此元素 while (!stack.empty() && stack.top() >= c && m[stack.top()] > 0 && !set.count(c)) { char x = stack.top(); stack.pop(); set.erase(x); } if (!set.count(c)) { stack.push(c); set.insert(c); } // 每个元素个数减一 m[c]--; } string ans = ""; while (!stack.empty()) { ans.push_back(stack.top()); stack.pop(); } reverse(ans.begin(), ans.end()); return ans; } }; #endif //CPP_0316__SOLUTION1_H_
ooooo-youwillsee/leetcode
0264-Ugly Number II/cpp_0264/Solution1.h
/** * @author ooooo * @date 2021/4/11 15:12 */ #ifndef CPP_0264__SOLUTION1_H_ #define CPP_0264__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> #include <unordered_set> using namespace std; class Solution { public: vector<int> nums = {2, 3, 5}; using ll = long long; int nthUglyNumber(int n) { priority_queue<ll, vector<ll>, greater<ll>> pq; unordered_set<ll> set; pq.push(1); int i = 0; while (!pq.empty()) { ll x = pq.top(); pq.pop(); if (!set.count(x)) { i++; set.insert(x); for (int k = 0; k < 3; k++) { pq.push(nums[k] * x); } if (i == n) { return x; } } } return 0; } }; #endif //CPP_0264__SOLUTION1_H_
ooooo-youwillsee/leetcode
0083-Remove-Duplicates-from-Sorted-List/cpp_0083/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0083-Remove-Duplicates-from-Sorted-List/cpp_0083/Solution1.h // // Created by ooooo on 2020/1/23. // #ifndef CPP_0083__SOLUTION1_H_ #define CPP_0083__SOLUTION1_H_ #include "ListNode.h" /** * loop */ class Solution { public: ListNode *deleteDuplicates(ListNode *head) { ListNode *cur = head; while (cur && cur->next) { if (cur->val == cur->next->val) { ListNode *delNode = cur->next; cur->next = delNode->next; delNode->next = nullptr; delete delNode; } else { cur = cur->next; } } return head; } }; #endif //CPP_0083__SOLUTION1_H_
ooooo-youwillsee/leetcode
1781-Sum of Beauty of All Substrings/cpp_1781/Solution1.h
/** * @author ooooo * @date 2021/3/14 14:57 */ #ifndef CPP_1781__SOLUTION1_H_ #define CPP_1781__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: int beautySum(string s) { int ans = 0; int n = s.size(); for (int i = 0; i < n; ++i) { int l = 0, r = i; vector<int> m(26); int cnt = 0, maxReq = INT_MIN; while (r < n) { m[s[r] - 'a']++; if (m[s[r] - 'a'] == 1) { cnt++; } maxReq = max(maxReq, m[s[r] - 'a']); int minReq = INT_MAX; for (int j = 0; j < 26; ++j) { if (m[j] > 0) { minReq = min(minReq, m[j]); } } ans += (maxReq - minReq); r++; } } return ans; } }; #endif //CPP_1781__SOLUTION1_H_
ooooo-youwillsee/leetcode
0108-Convert-Sorted-Array-to-Binary-Search-Tree/cpp_0108/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/2. // #ifndef CPP_0108_SOLUTION1_H #define CPP_0108_SOLUTION1_H #include "TreeNode.h" #include <vector> class Solution { public: // 复制vector,时空复杂度变低 TreeNode *sortedArrayToBST2(vector<int> &nums) { if (nums.empty()) return nullptr; // 节点向左偏 int mid = nums.size() / 2; // 节点向右偏 //int mid = (nums.size() - 1) / 2; TreeNode *node = new TreeNode(nums[mid]); vector<int> left(nums.begin(), nums.begin() + mid); vector<int> right(nums.begin() + mid + 1, nums.end()); node->left = sortedArrayToBST(left); node->right = sortedArrayToBST(right); return node; } TreeNode *help(vector<int> &nums, int left, int right) { if (left == right || nums.empty()) return nullptr; int mid = left + (right - left) / 2; TreeNode *node = new TreeNode(nums[mid]); node->left = help(nums, left, mid); node->right = help(nums, mid + 1, right); return node; } TreeNode *sortedArrayToBST(vector<int> &nums) { return help(nums, 0, nums.size()); } }; #endif //CPP_0108_SOLUTION1_H
ooooo-youwillsee/leetcode
0059-Spiral Matrix II/cpp_0059/Solution1.h
/** * @author ooooo * @date 2020/10/5 17:51 */ #ifndef CPP_0059__SOLUTION1_H_ #define CPP_0059__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int>> generateMatrix(int n) { int cur = 1, max = n * n; vector<vector<int>> ans(n, vector<int>(n, 0)); for (int c = 0; c < n / 2 + 1; c++) { for (int i = 0 + c; i < n - 1 - c; i++) { ans[0 + c][i] = cur++; } for (int i = 0 + c; i < n - 1 - c; i++) { ans[i][n - 1 - c] = cur++; } for (int i = 0 + c; i < n - 1 - c; i++) { ans[n - 1 - c][n - 1 - i] = cur++; } for (int i = 0 + c; i < n - 1 - c; i++) { ans[n - 1 - i][0 + c] = cur++; } } if (cur <= max) { ans[n / 2][n / 2] = cur; } return ans; } }; #endif //CPP_0059__SOLUTION1_H_
ooooo-youwillsee/leetcode
0087-Scramble String/cpp_0087/Solution1.h
/** * @author ooooo * @date 2021/5/7 20:08 */ #ifndef CPP_0087__SOLUTION1_H_ #define CPP_0087__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool dfs(string &s1, string &s2, int i, int j, int len) { if (memo[i][j][len] != 0) return memo[i][j][len] == 1; if (s1.substr(i, len) == s2.substr(j, len)) return true; for (int cnt = 1; cnt < len; cnt++) { bool flag = dfs(s1, s2, i, j, cnt) && dfs(s1, s2, i + cnt, j + cnt, len - cnt); flag = flag || dfs(s1, s2, i, j + len - cnt, cnt) && dfs(s1, s2, i + cnt, j, len - cnt); if (flag) { memo[i][j][len] = 1; return true; } } memo[i][j][len] = -1; return false; } int memo[35][35][35]; bool isScramble(string s1, string s2) { memset(memo, 0, sizeof(memo)); // 0 表示没有计算过, 1 表示true, -1 表示false return dfs(s1, s2, 0, 0, s1.size()); } }; #endif //CPP_0087__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_010-1/cpp_010-1/Solution2.h
<filename>lcof_010-1/cpp_010-1/Solution2.h // // Created by ooooo on 2020/3/8. // #ifndef CPP_010_1__SOLUTION2_H_ #define CPP_010_1__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * F(N) = F(N - 1) + F(N - 2) * * recursion + memo * */ class Solution { public: unsigned long dfs(int n) { if (memo.count(n)) return memo[n]; if (n == 0 || n == 1) return n; return memo[n] = (dfs(n - 1) + dfs(n - 2)) % 1000000007; } unordered_map<int, unsigned long> memo; int fib(int n) { return dfs(n); } }; #endif //CPP_010_1__SOLUTION2_H_
ooooo-youwillsee/leetcode
0404-Sum-of-Left-Leaves/cpp_0404/Solution1.h
// // Created by ooooo on 2020/1/2. // #ifndef CPP_0404_SOLUTION1_H #define CPP_0404_SOLUTION1_H #include "TreeNode.h" class Solution { public: int help(TreeNode *node, bool isLeft) { if (!node) return 0; int x = isLeft && !node->left && !node->right ? node->val : 0; return help(node->left, true) + help(node->right, false) + x; } int sumOfLeftLeaves(TreeNode *root) { return help(root, false); } }; #endif //CPP_0404_SOLUTION1_H
ooooo-youwillsee/leetcode
0897-Increasing-Order-Search-Tree/cpp_0897/Solution1.h
<filename>0897-Increasing-Order-Search-Tree/cpp_0897/Solution1.h // // Created by ooooo on 2020/1/5. // #ifndef CPP_0897_SOLUTION1_H #define CPP_0897_SOLUTION1_H #include "TreeNode.h" class Solution { public: TreeNode *prev = nullptr; void inOrder(TreeNode *node) { if (!node) return; inOrder(node->left); prev->right = node; node->left = nullptr; prev = node; inOrder(node->right); } TreeNode *increasingBST(TreeNode *root) { TreeNode *ans = new TreeNode(0); prev = ans; inOrder(root); return ans->right; } }; #endif //CPP_0897_SOLUTION1_H
ooooo-youwillsee/leetcode
0225-Implement-Stack-using-Queues/cpp_0225/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2019/10/30. // #ifndef CPP_0225_SOLUTION1_H #define CPP_0225_SOLUTION1_H #include <iostream> #include <queue> using namespace std; class MyStack { private: queue<int> q; public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { q.push(x); } /** Removes the element on top of the stack and returns that element. */ int pop() { for (int i = 0, len = q.size() - 1; i < len; ++i) { q.push(q.front()); q.pop(); } int res = q.front(); q.pop(); return res; } /** Get the top element. */ int top() { for (int i = 0, len = q.size() - 1; i < len; ++i) { q.push(q.front()); q.pop(); } int res = q.front(); q.push(res); q.pop(); return res; } /** Returns whether the stack is empty. */ bool empty() { return q.empty(); } }; /** * Your MyStack object will be instantiated and called as such: * MyStack* obj = new MyStack(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->top(); * bool param_4 = obj->empty(); */ #endif //CPP_0225_SOLUTION1_H
ooooo-youwillsee/leetcode
1668-Maximum Repeating Substring/cpp_1668/Solution1.h
<filename>1668-Maximum Repeating Substring/cpp_1668/Solution1.h<gh_stars>10-100 /** * @author ooooo * @date 2021/1/24 17:52 */ #ifndef CPP_1668__SOLUTION1_H_ #define CPP_1668__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack> using namespace std; class Solution { public: int maxRepeating(string sequence, string word) { int ans = 0; int max_count = sequence.size() / word.size(); vector<string> str_vec(max_count + 1); str_vec[0] = word; for (int i = 1; i < str_vec.size(); ++i) { if (sequence.find(str_vec[i - 1]) != -1) { ans = max(ans, i); } str_vec[i] = str_vec[i - 1] + word; } return ans; } }; #endif //CPP_1668__SOLUTION1_H_
ooooo-youwillsee/leetcode
0139-Word-Break/cpp_0139/Solution3.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/17. // #ifndef CPP_0139__SOLUTION3_H_ #define CPP_0139__SOLUTION3_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * 记忆化 记录索引 i 的位置 */ class Solution { public: bool wordBreak(string s, vector<string> &wordDict) { unordered_set<string> word_set(wordDict.begin(), wordDict.end()); vector<bool> dp(s.length() + 1, false); // 0 表示空字符串 dp[0] = true; for (int i = 1; i <= s.length(); ++i) { for (int j = 0; j < i; ++j) { if (dp[j] && word_set.count(s.substr(j, i - j))) { dp[i] = true; break; } } } return dp[s.length()]; } }; #endif //CPP_0139__SOLUTION3_H_
ooooo-youwillsee/leetcode
0038-Count-and-Say/cpp_0038/Solution1.h
// // Created by ooooo on 2020/1/24. // #ifndef CPP_0038__SOLUTION1_H_ #define CPP_0038__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: string help(string s) { int count = 1; string ans = ""; for (int i = 0; i < s.size(); ++i) { if (i == s.size() - 1 || s[i] != s[i + 1]) { ans += to_string(count) + string(1, s[i]); count = 1; } else { count++; } } return ans; } string countAndSay(int n) { string ans = "1"; for (int i = 1; i < n; ++i) { ans = help(ans); } return ans; } }; #endif //CPP_0038__SOLUTION1_H_
ooooo-youwillsee/leetcode
0532-K-diff-Pairs-in-an-Array/cpp_0532/Solution4.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0532_SOLUTION4_H #define CPP_0532_SOLUTION4_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() || k < 0) return 0; int ans = 0; unordered_set<int> set; unordered_set<int> s; for (auto &num: nums) { // 可以存入最小值,!!! if (s.count(num + k)) set.insert(num); if (s.count(num - k)) set.insert(num - k); s.insert(num); } return set.size(); } }; #endif //CPP_0532_SOLUTION4_H
ooooo-youwillsee/leetcode
0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution3.h
// // Created by ooooo on 2020/2/15. // #ifndef CPP_0094__SOLUTION3_H_ #define CPP_0094__SOLUTION3_H_ #include "TreeNode.h" #include <stack> /** * iteration */ class Solution { public: vector<int> inorderTraversal(TreeNode *root) { vector<int> ans; stack<TreeNode *> stack; TreeNode *curNode = root; while (curNode != nullptr || !stack.empty()) { while (curNode) { stack.push(curNode); curNode = curNode->left; } curNode = stack.top(); stack.pop(); ans.push_back(curNode->val); curNode = curNode->right; } return ans; } }; #endif //CPP_0094__SOLUTION3_H_
ooooo-youwillsee/leetcode
0803-Bricks Falling When Hit/cpp_0803/Solution1.h
<filename>0803-Bricks Falling When Hit/cpp_0803/Solution1.h /** * @author ooooo * @date 2021/1/16 18:28 */ #ifndef CPP_0803__SOLUTION1_H_ #define CPP_0803__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <unordered_set> using namespace std; class Solution { public: int m, n; vector<int> p, pSize; vector<vector<int>> directions = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; vector<int> hitBricks(vector<vector<int>> &grid, vector<vector<int>> &hits) { m = grid.size(), n = grid[0].size(); vector<vector<int>> copy = grid; for (int i = 0; i < hits.size(); ++i) { int x = hits[i][0], y = hits[i][1]; // 初始化状态 copy[x][y] = 0; } // 初始化并查集, 屋顶为 size int size = m * n; p.assign(size + 1, 0); pSize.assign(size + 1, 1); for (int i = 0; i < p.size(); ++i) { p[i] = i; } // 处理第0行屋顶 for (int j = 0; j < n; ++j) { if (copy[0][j] == 1) { connect(tranIndex(0, j), size); } } // 从第二行到尾行 for (int i = 1; i < m; ++i) { for (int j = 0; j < n; ++j) { if (copy[i][j] == 1) { if (i - 1 >= 0 && copy[i - 1][j] == 1) { connect(tranIndex(i - 1, j), tranIndex(i, j)); } if (j - 1 >= 0 && copy[i][j - 1] == 1) { connect(tranIndex(i, j - 1), tranIndex(i, j)); } } } } vector<int> ans(hits.size()); // 补砖块 for (int i = hits.size() - 1; i >= 0; --i) { int x = hits[i][0], y = hits[i][1]; // 原先就是为0,所以没有任何砖块掉落 if (grid[x][y] == 0) { continue; } int prevSize = pSize[find(size)]; // 如果敲碎是屋顶的砖块 if (x == 0) { connect(tranIndex(x, y), size); } // 连接 for (auto &direction: directions) { int nx = x + direction[0]; int ny = y + direction[1]; if (inArea(nx, ny) && copy[nx][ny] == 1) { connect(tranIndex(x, y), tranIndex(nx, ny)); } } int curSize = pSize[find(size)]; ans[i] = max(0, curSize - prevSize - 1); // 最后添加这个砖块 copy[x][y] = 1; } return ans; } bool inArea(int i, int j) { return i >= 0 && i < m && j >= 0 && j < n; } int tranIndex(int i, int j) { return n * i + j; } int find(int i) { if (p[i] == i) return i; return p[i] = find(p[i]); } bool connect(int i, int j) { int pi = find(i), pj = find(j); if (pi == pj) return true; p[pi] = pj; pSize[pj] += pSize[pi]; return false; } }; #endif //CPP_0803__SOLUTION1_H_
ooooo-youwillsee/leetcode
1143-Longest Common Subsequence/cpp_1143/Solution1.h
/** * @author ooooo * @date 2021/4/3 12:12 */ #ifndef CPP_1143__SOLUTION1_H_ #define CPP_1143__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int longestCommonSubsequence(string s1, string s2) { int m = s1.size(), n = s2.size(); vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (s1[i] == s2[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } } return dp[m][n]; } }; #endif //CPP_1143__SOLUTION1_H_
ooooo-youwillsee/leetcode
0518-Coin Change 2/cpp_0518/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2021/2/17 19:21 */ #ifndef CPP_0518__SOLUTION1_H_ #define CPP_0518__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int change(int amount, vector<int> &coins) { int n = coins.size(); vector<vector<int>> dp(amount + 1, vector<int>(n + 1, 0)); for (int i = 1; i <= n; ++i) { dp[0][i] = 1; } dp[0][0] = 1; for (int i = 1; i <= amount; ++i) { for (int j = 0; j < n; ++j) { int coin = coins[j], cnt = 0; while (i - cnt * coin >= 0) { dp[i][j + 1] += dp[i - cnt * coin][j]; cnt++; } } } return dp[amount][n]; } }; #endif //CPP_0518__SOLUTION1_H_
ooooo-youwillsee/leetcode
0287-Find-the-Duplicate-Number/cpp_0287/Solution3.h
// // Created by ooooo on 2020/2/23. // #ifndef CPP_0287__SOLUTION3_H_ #define CPP_0287__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * binary search * 对区间[l, r]中查找 l <= X <= mid 的个数count, * 如果 count > mid - l + 1; 就说明重复的数字在 [l, mid] * 反之,重复的数字在 [mid+1, r] */ class Solution { public: int findDuplicate(vector<int> &nums) { int l = 1, r = nums.size() - 1, mid = 0, count = 0; while (l < r) { mid = l + (r - l) / 2; count = 0; // find l <= X <=mid 的个数 for (int i = 0; i < nums.size(); ++i) { if (nums[i] <= mid && nums[i] >= l) count++; } if (count > mid - l + 1) { r = mid; } else { l = mid + 1; } } return l; } }; #endif //CPP_0287__SOLUTION3_H_
ooooo-youwillsee/leetcode
0521-Longest-Uncommon-Subsequence-I/cpp_0521/Solution1.h
// // Created by ooooo on 2020/1/26. // #ifndef CPP_0521__SOLUTION1_H_ #define CPP_0521__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: int findLUSlength(string a, string b) { if (a.size() > b.size()) return a.size(); else if (a.size() < b.size()) return b.size(); else if (a == b) return -1; return a.size(); } }; #endif //CPP_0521__SOLUTION1_H_
ooooo-youwillsee/leetcode
0547-Number of Provinces/cpp_0547/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/7 17:08 */ #ifndef CPP_0547__SOLUTION1_H_ #define CPP_0547__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int findCircleNum(vector<vector<int>> &isConnected) { int n = isConnected.size(); vector<int> p(n); for (int i = 0; i < n; ++i) { p[i] = i; } int ans = n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (isConnected[i][j]) { ans -= connect(p, i, j); } } } return ans; } int find(vector<int> &p, int i) { if (i == p[i]) return i; int pi = find(p, p[i]); p[i] = pi; return pi; } int connect(vector<int> &p, int i, int j) { int pi = find(p, i), pj = find(p, j); if (pi == pj) return 0; p[pi] = pj; return 1; } }; #endif //CPP_0547__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_036/cpp_036/Node.h
<filename>lcof_036/cpp_036/Node.h // // Created by ooooo on 2020/3/23. // #ifndef CPP_036__NODE_H_ #define CPP_036__NODE_H_ #include <iostream> using namespace std; class Node { public: int val; Node *left; Node *right; Node() {} Node(int _val) { val = _val; left = NULL; right = NULL; } Node(int _val, Node *_left, Node *_right) { val = _val; left = _left; right = _right; } }; #endif //CPP_036__NODE_H_
ooooo-youwillsee/leetcode
0684-Redundant Connection/cpp_0684/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/1/13 20:10 */ #ifndef CPP_0684__SOLUTION1_H_ #define CPP_0684__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> findRedundantConnection(vector<vector<int>> &edges) { int n = edges.size(); vector<int> p(n + 1), rank(n + 1, 1); for (int i = 0; i < n + 1; ++i) { p[i] = i; } vector<int> ans; for (int i = 0; i < edges.size(); ++i) { int u = edges[i][0], v = edges[i][1]; if (connected(p, rank, u, v)) { ans = edges[i]; } } return ans; } int find(vector<int> &p, int i) { if (p[i] == i) return i; return p[i] = find(p, p[i]); } bool connected(vector<int> &p, vector<int> &rank, int i, int j) { int pi = find(p, i), pj = find(p, j); if (pi == pj) return true; if (rank[pi] == rank[pj]) { p[pi] = pj; rank[pj]++; } else if (rank[pi] < rank[pj]) { p[pi] = pj; } else { p[pj] = pi; } return false; } }; #endif //CPP_0684__SOLUTION1_H_
ooooo-youwillsee/leetcode
1074-Number of Submatrices That Sum to Target/cpp_1074/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>1074-Number of Submatrices That Sum to Target/cpp_1074/Solution1.h // // Created by ooooo on 5/31/2021. // #ifndef CPP_1074_SOLUTION1_H #define CPP_1074_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) { int m = matrix.size(), n = matrix[0].size(); int dp[m+1][n+1]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { dp[i+1][j+1] = dp[i+1][j] + dp[i][j+1] - dp[i][j] + matrix[i][j]; } } int ans = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { for (int p = i+1; p <= m; p++) { for (int q = j+1; q <= n; q++) { if (dp[p][q] - dp[p][j] - dp[i][q] + dp[i][j] == target) { ans ++; } } } } } return ans; } }; #endif //CPP_1074_SOLUTION1_H
ooooo-youwillsee/leetcode
0645-Set-Mismatch/cpp_0645/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/13. // #ifndef CPP_0645__SOLUTION2_H_ #define CPP_0645__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> findErrorNums(vector<int> &nums) { unordered_map<int, int> m; for (auto &num: nums) { m[num]++; } int i = -1, j = -1; for (int k = 1; k <= nums.size(); ++k) { // k = 0 为缺失数字 if (m[k] == 0) j = k; // k = 2 为重复数字 else if (m[k] == 2) i = k; } return {i, j}; } }; #endif //CPP_0645__SOLUTION2_H_
ooooo-youwillsee/leetcode
0027-Remove-Element/cpp_0027/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0027_SOLUTION1_H #define CPP_0027_SOLUTION1_H #include <iostream> #include <vector> using namespace std; class Solution { public: int removeElement(vector<int> &nums, int val) { int i = 0; for (int j = 0; j < nums.size(); ++j) { if (nums[j] != val) { // 不相等就直接赋值 nums[i] = nums[j]; i++; } } return i; } }; #endif //CPP_0027_SOLUTION1_H
ooooo-youwillsee/leetcode
0437-Path-Sum-III/cpp_0437/Solution2.h
<filename>0437-Path-Sum-III/cpp_0437/Solution2.h // // Created by ooooo on 2020/1/2. // #ifndef CPP_0437_SOLUTION2_H #define CPP_0437_SOLUTION2_H #include "TreeNode.h" #include <vector> class Solution { public: // prevSums变为数组可以加速,用一个p标记最后的元素位置。 int dfs(TreeNode *node, vector<int> prevSums, int sum) { if (!node) return 0; int count = node->val == sum ? 1 : 0; for (int i = 0; i < prevSums.size(); ++i) { prevSums[i] += node->val; if (prevSums[i] == sum) count++; } prevSums.push_back(node->val); return dfs(node->left, prevSums, sum) + dfs(node->right, prevSums, sum) + count; } int pathSum(TreeNode *root, int sum) { return dfs(root, {}, sum); } }; #endif //CPP_0437_SOLUTION2_H
ooooo-youwillsee/leetcode
0970-Powerful-Integers/cpp_0970/Solution2.h
// // Created by ooooo on 2020/1/17. // #ifndef CPP_0970__SOLUTION2_H_ #define CPP_0970__SOLUTION2_H_ #include <iostream> #include <unordered_set> #include <unordered_map> #include <vector> using namespace std; /** * map缓存,2^20 > 1000000 */ class Solution { public: unordered_map<int, vector<int>> m; int calcMaxBound(int x, int bound) { if (x == 1) { m[x] = vector<int>(bound, 1); return bound; } int ans = 0, sum = 1; while (sum <= bound) { m[x].push_back(sum); ans++; sum *= x; } return ans; } vector<int> powerfulIntegers(int x, int y, int bound) { unordered_set<int> ans; m[x] = m[y] = {}; int xMax = calcMaxBound(x, bound), yMax = calcMaxBound(y, bound); // 2^20 > 1000000 for (int i = 0; i < xMax && i < 20; ++i) { for (int j = 0; j < yMax && j < 20; ++j) { int sum = m[x][i] + m[y][j]; if (sum <= bound) { ans.insert(sum); } } } return vector<int>(ans.begin(), ans.end()); } }; #endif //CPP_0970__SOLUTION2_H_
ooooo-youwillsee/leetcode
0680-Valid-Palindrome-II/cpp_0680/Solution1.h
// // Created by ooooo on 2020/1/28. // #ifndef CPP_0680__SOLUTION1_H_ #define CPP_0680__SOLUTION1_H_ #include <iostream> using namespace std; /** * 超出内存限制 */ class Solution { public: bool isValid(string s, int pos) { for (int left = 0, right = s.size() - 1; left < right; ++left, --right) { if (left == pos) left++; else if (right == pos) right--; if (s[left] != s[right]) return false; } return true; } bool validPalindrome(string s) { // s.size 是unsigned 无法和-1正常比较 for (int i = s.size(); i >= 0; --i) { if (isValid(s, i)) return true; } return false; } }; #endif //CPP_0680__SOLUTION1_H_
ooooo-youwillsee/leetcode
0056-Merge-Intervals/cpp_0056/Solution1.h
// // Created by ooooo on 2020/2/12. // #ifndef CPP_0056__SOLUTION1_H_ #define CPP_0056__SOLUTION1_H_ #include <iostream> #include <vector> #include <set> using namespace std; /** * 合并 => [1, n-1] */ class Solution { public: struct Range { int start, end; Range(int start, int an_end) : start(start), end(an_end) {} Range() {} bool operator<(const Range &other) const { return this->start < other.start; } }; vector<vector<int>> merge(vector<vector<int>> &intervals) { if (intervals.size() <= 1) return intervals; vector<Range> vec; for (int i = 0; i < intervals.size(); ++i) { auto item = intervals[i]; vec.push_back(Range(item[0], item[1])); } sort(vec.begin(), vec.end()); vector<vector<int>> ans; Range item = vec[0]; for (int i = 1; i < vec.size(); i++) { if (item.end >= vec[i].start) { item = Range(item.start, max(item.end, vec[i].end)); } else { ans.push_back({item.start, item.end}); item = vec[i]; } if (i == vec.size() - 1) { ans.push_back({item.start, item.end}); } } return ans; } }; #endif //CPP_0056__SOLUTION1_H_
ooooo-youwillsee/leetcode
0102-Binary-Tree-Level-Order-Traversal/cpp_0102/Solution1.h
// // Created by ooooo on 2019/11/4. // #ifndef CPP_0102_SOLUTION1_H #define CPP_0102_SOLUTION1_H #include <iostream> #include "TreeNode.h" #include <queue> #include <vector> using namespace std; class Solution { public: vector<vector<int>> levelOrder(TreeNode *root) { vector<vector<int>> res; if (!root) { return res; } queue<TreeNode *> q; q.push(root); while (!q.empty()) { int size = q.size(); vector<int> vec; for (int i = 0; i < size; ++i) { TreeNode *node = q.front(); vec.push_back(node->val); if (node->left) q.push(node->left); if (node->right) q.push(node->right); q.pop(); } res.push_back(vec); } return res; } }; #endif //CPP_0102_SOLUTION1_H
ooooo-youwillsee/leetcode
lcof_022/cpp_022/Solution1.h
// // Created by ooooo on 2020/3/15. // #ifndef CPP_022__SOLUTION1_H_ #define CPP_022__SOLUTION1_H_ #include "ListNode.h" #include <vector> class Solution { public: ListNode *getKthFromEnd(ListNode *head, int k) { if (!head) return nullptr; vector<ListNode *> vec; while (head) { vec.push_back(head); head = head->next; } if (k > vec.size()) return nullptr; return vec[vec.size() - k]; } }; #endif //CPP_022__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_058-2/cpp_058-2/Solution2.h
// // Created by ooooo on 2020/4/19. // #ifndef CPP_058_2__SOLUTION2_H_ #define CPP_058_2__SOLUTION2_H_ #include <iostream> using namespace std; class Solution { public: string reverseLeftWords(string s, int n) { string ans; reverse_copy(s.begin(), s.end(), back_inserter(ans)); reverse(ans.begin(), ans.begin() + ans.size() - n); reverse(ans.begin() + ans.size() - n, ans.end()); return ans; } }; #endif //CPP_058_2__SOLUTION2_H_
ooooo-youwillsee/leetcode
1880-Check if Word Equals Summation of Two Words/cpp_1880/Solution1.h
<filename>1880-Check if Word Equals Summation of Two Words/cpp_1880/Solution1.h /** * @author ooooo * @date 2021/5/31 10:15 */ #ifndef CPP_1880__SOLUTION1_H_ #define CPP_1880__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: long long help(string &s) { long long ans = 0; for (auto c : s) { ans = ans * 10 + (c - 'a'); } return ans; } bool isSumEqual(string a, string b, string c) { return help(a) == help(c) - help(b); } }; #endif //CPP_1880__SOLUTION1_H_
ooooo-youwillsee/leetcode
1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/cpp_1365/Solution1.h
/** * @author ooooo * @date 2020/10/26 08:59 */ #ifndef CPP_1365__SOLUTION1_H_ #define CPP_1365__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int> &nums) { vector<int> copy = nums; sort(copy.begin(), copy.end()); vector<int> ans; for (int i = 0; i < nums.size(); ++i) { auto it = lower_bound(copy.begin(), copy.end(), nums[i]); int count = it - copy.begin(); ans.push_back(count); } return ans; } }; #endif //CPP_1365__SOLUTION1_H_
ooooo-youwillsee/leetcode
1679-Max Number of K-Sum Pairs/cpp_1679/Solution1.h
/** * @author ooooo * @date 2021/1/29 20:45 */ #ifndef CPP_1679__SOLUTION1_H_ #define CPP_1679__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxOperations(vector<int> &nums, int k) { sort(nums.begin(), nums.end()); int l = 0, r = nums.size() - 1; int ans = 0; while (l < r) { int sum = nums[l] + nums[r]; if (sum == k) { ans++; l++; r--; } else if (sum < k) { l++; } else { r--; } } return ans; } }; #endif //CPP_1679__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcci_17_13/cpp_17_13/Solution2.h
/** * @author ooooo * @date 2020/10/10 19:30 */ #ifndef CPP_17_13__SOLUTION2_H_ #define CPP_17_13__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * trie */ class Solution { public: struct Node { vector<Node *> children; bool isWord; Node() { this->children.assign(26, nullptr); this->isWord = false; } }; Node *root = new Node(); void buildTree(vector<string> &dict) { for (auto &word: dict) { Node *cur = root; for (int i = word.size() - 1; i >= 0; --i) { int k = word[i] - 'a'; if (!cur->children[k]) { cur->children[k] = new Node(); } cur = cur->children[k]; } cur->isWord = true; } } int respace(vector<string> &dictionary, string sentence) { int n = sentence.size(); if (dictionary.empty()) return n; buildTree(dictionary); vector<int> dp(n + 1, 0); for (int i = 0; i < n; i++) { dp[i + 1] = dp[i] + 1; Node *cur = root; int j = i; for (; j >= 0; --j) { int k = sentence[j] - 'a'; if (!cur->children[k]) break; cur = cur->children[k]; if (cur->isWord) { dp[i + 1] = min(dp[i + 1], dp[j]); } } //cout << "i+1: " << i + 1 << " " << dp[i + 1] << endl; } return dp[n]; } }; #endif //CPP_17_13__SOLUTION2_H_
ooooo-youwillsee/leetcode
0973-K Closest Points to Origin/cpp_0973/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/11/9 08:58 */ #ifndef CPP_0973__SOLUTION1_H_ #define CPP_0973__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; /** * heap */ class Solution { public: struct Item { vector<int> point; Item(vector<int> &point) { this->point = point; } vector<int> toPoint() const { return this->point; } long distance() const { int x = this->point[0], y = this->point[1]; return x * x + y * y; } bool operator<(const Item &__y) const { return this->distance() > __y.distance(); } }; vector<vector<int>> kClosest(vector<vector<int>> &points, int K) { priority_queue<Item> q; vector<vector<int>> ans; for (auto &p: points) { q.push(Item{p}); } while (K) { ans.emplace_back(q.top().toPoint()); q.pop(); K--; } return ans; } }; #endif //CPP_0973__SOLUTION1_H_
ooooo-youwillsee/leetcode
1663-Smallest String With A Given Numeric Value/cpp_1663/Solution1.h
/** * @author ooooo * @date 2020/12/11 10:29 */ #ifndef CPP_1663__SOLUTION1_H_ #define CPP_1663__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: string getSmallestString(int n, int k) { string ans = ""; for (int i = 0; i < n; ++i) { ans += 'a'; } k -= n; int cnt = k / 25; int j = n - 1; for (int i = 0; i < cnt; ++i) { ans[j] = 'z'; j--; } if (k % 25) { ans[j] = (k % 25) + 'a'; } return ans; } }; #endif //CPP_1663__SOLUTION1_H_
ooooo-youwillsee/leetcode
0055-Jump-Game/cpp_0055/Solution2.h
<filename>0055-Jump-Game/cpp_0055/Solution2.h // // Created by ooooo on 2020/2/12. // #ifndef CPP_0055__SOLUTION2_H_ #define CPP_0055__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 贪心 */ class Solution { public: bool canJump(vector<int> &nums) { int lastPos = nums.size() - 1; for (int i = nums.size() - 1; i >= 0; --i) { // 当前的位置 i ,加上步长 nums[i] if (i + nums[i] >= lastPos) { lastPos = i; } } return lastPos == 0; } }; #endif //CPP_0055__SOLUTION2_H_