repo_name
stringlengths
5
122
path
stringlengths
3
232
text
stringlengths
6
1.05M
ooooo-youwillsee/leetcode
1678-Goal Parser Interpretation/cpp_1678/Solution1.h
/** * @author ooooo * @date 2021/1/29 20:47 */ #ifndef CPP_1678__SOLUTION1_H_ #define CPP_1678__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <stack> #include <numeric> #include <queue> using namespace std; class Solution { public: string interpret(string command) { string ans = ""; for (int i = 0; i < command.size(); ++i) { char c = command[i]; if (c == 'G') { ans += c; } else if (c == ')') { if (command[i - 1] == '(') { ans += 'o'; } else { ans += "al"; } } } return ans; } }; #endif //CPP_1678__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_022/cpp_022/Solution2.h
// // Created by ooooo on 2020/3/15. // #ifndef CPP_022__SOLUTION2_H_ #define CPP_022__SOLUTION2_H_ #include "ListNode.h" class Solution { public: ListNode *getKthFromEnd(ListNode *head, int k) { ListNode *dummyHead = new ListNode(0), *cur = dummyHead; dummyHead->next = head; // 先前走 k-1 步 for (int i = 0; i < k - 1; ++i) { if (cur) cur = cur->next; else return nullptr; } while (cur->next) { cur = cur->next; dummyHead = dummyHead->next; } return dummyHead; } }; #endif //CPP_022__SOLUTION2_H_
ooooo-youwillsee/leetcode
0844-Backspace-String-Compare/cpp_0844/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/5. // #ifndef CPP_0844_SOLUTION1_H #define CPP_0844_SOLUTION1_H #include <iostream> #include <stack> using namespace std; class Solution { public: stack<char> help(string s) { stack<char> ss; for (auto c: s) { if (c == '#') { if (!ss.empty()) { ss.pop(); } continue; } ss.push(c); } return ss; } bool backspaceCompare(string S, string T) { return help(S) == help(T); } }; #endif //CPP_0844_SOLUTION1_H
ooooo-youwillsee/leetcode
1630-Arithmetic Subarrays/cpp_1630/Solution1.h
/** * @author ooooo * @date 2020/11/15 09:40 */ #ifndef CPP_1630__SOLUTION1_H_ #define CPP_1630__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: bool isArithmeticArray(vector<int> &nums) { if (nums.size() < 2) return false; sort(nums.begin(), nums.end()); int diff = nums[1] - nums[0]; for (int i = 2; i < nums.size(); ++i) { if (nums[i] - nums[i - 1] != diff) return false; } return true; } vector<bool> checkArithmeticSubarrays(vector<int> &nums, vector<int> &l, vector<int> &r) { vector<bool> ans; for (int i = 0; i < l.size(); ++i) { int start = l[i], end = r[i]; vector<int> arr; for (int j = 0; start <= end; ++j, ++start) { arr.push_back(nums[start]); } ans.push_back(isArithmeticArray(arr)); } return ans; } } #endif //CPP_1630__SOLUTION1_H_
ooooo-youwillsee/leetcode
0575-Distribute-Candies/cpp_0575/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/12. // #ifndef CPP_0575__SOLUTION2_H_ #define CPP_0575__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int distributeCandies(vector<int> &candies) { unordered_set<int> ans(candies.begin(), candies.end()); return min(ans.size(), candies.size() / 2); } }; #endif //CPP_0575__SOLUTION2_H_
ooooo-youwillsee/leetcode
lcof_055-1/cpp_055-1/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/11. // #ifndef CPP_055_1__SOLUTION2_H_ #define CPP_055_1__SOLUTION2_H_ #include "TreeNode.h" #include <queue> /** * dfs */ class Solution { public: void dfs(TreeNode *root, int level) { if (!root) { maxLevel = max(maxLevel, level); return; } dfs(root->left, level + 1); dfs(root->right, level + 1); } int maxLevel = 0; int maxDepth(TreeNode *root) { dfs(root, 0); return maxLevel; } }; #endif //CPP_055_1__SOLUTION2_H_
ooooo-youwillsee/leetcode
1738-Find Kth Largest XOR Coordinate Value/cpp_1738/Solution2.h
/** * @author ooooo * @date 2021/5/19 21:11 */ #ifndef CPP_1738__SOLUTION2_H_ #define CPP_1738__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int kthLargestValue(vector<vector<int>> &matrix, int k) { int m = matrix.size(), n = matrix[0].size(); vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); vector<int> ans; 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]; ans.push_back(dp[i + 1][j + 1]); } } sort(ans.begin(), ans.end()); return ans[ans.size() - k]; } }; #endif //CPP_1738__SOLUTION2_H_
ooooo-youwillsee/leetcode
0496-Next-Greater-Element-I/cpp_0496/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/5. // #ifndef CPP_0496_SOLUTION2_H #define CPP_0496_SOLUTION2_H #include <iostream> #include <vector> #include <stack> #include <unordered_map> using namespace std; class Solution { public: vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2) { stack<int> s; unordered_map<int, int> m; for (auto num: nums2) { while (!s.empty() && s.top() < num) { m[s.top()] = num; s.pop(); } s.push(num); } vector<int> res; for (auto num: nums1) { if (m.count(num)) { res.push_back(m[num]); } else { res.push_back(-1); } } return res; } }; #endif //CPP_0496_SOLUTION2_H
ooooo-youwillsee/leetcode
0299-Bulls-and-Cows/cpp_0299/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/10. // #ifndef CPP_0299__SOLUTION2_H_ #define CPP_0299__SOLUTION2_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: string getHint(string secret, string guess) { int A = 0, B = 0; unordered_map<char, int> m; for (int i = 0; i < secret.size(); ++i) { if (secret[i] == guess[i]) A++; m[secret[i]]++; } for (int i = 0; i < guess.size(); ++i) { if (m[guess[i]]) { B++; m[guess[i]]--; } } B = A > 0 ? B - A : B; return to_string(A) + "A" + to_string(B) + "B"; } }; #endif //CPP_0299__SOLUTION2_H_
ooooo-youwillsee/leetcode
0581-Shortest-Unsorted-Continuous-Subarray/cpp_0581/Solution2.h
// // Created by ooooo on 2020/2/7. // #ifndef CPP_0581__SOLUTION2_H_ #define CPP_0581__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * */ class Solution { public: int findUnsortedSubarray(vector<int> &nums) { int min_value = INT_MAX, max_value = INT_MIN; for (int i = 1; i < nums.size(); ++i) { if (nums[i - 1] > nums[i]) { min_value = min(min_value, nums[i]); } } for (int i = nums.size() - 2; i >= 0; --i) { if (nums[i] > nums[i + 1]) { max_value = max(max_value, nums[i]); } } int l = 0, r = 0; for (l = 0; l < nums.size(); ++l) { if (nums[l] > min_value) { break; } } for (r = nums.size() - 1; r >= 0; --r) { if (nums[r] < max_value) { break; } } return r - l < 0 ? 0 : r - l + 1; } }; #endif //CPP_0581__SOLUTION2_H_
ooooo-youwillsee/leetcode
1071-Greatest-Common-Divisor-of-Strings/cpp_1071/Solution2.h
// // Created by ooooo on 2020/2/2. // #ifndef CPP_1071__SOLUTION2_H_ #define CPP_1071__SOLUTION2_H_ #include <iostream> #include <math.h> #include <numeric> using namespace std; /** * gcd */ class Solution { public: string gcdOfStrings(string str1, string str2) { if (str1 + str2 != str2 + str1) return ""; return str1.substr(0, gcd(str1.size(), str2.size())); } }; #endif //CPP_1071__SOLUTION2_H_
ooooo-youwillsee/leetcode
0221-Maximal-Square/cpp_0221/Solution1.h
<filename>0221-Maximal-Square/cpp_0221/Solution1.h // // Created by ooooo on 2020/2/20. // #ifndef CPP_0221__SOLUTION1_H_ #define CPP_0221__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp[i][j] = min(dp[i][j-1],dp[i-1][j-1],dp[i-1][j]) + 1 */ class Solution { public: int maximalSquare(vector<vector<char>> &matrix) { if (matrix.empty()) return 0; int m = matrix.size(), n = matrix[0].size(), ans = 0; 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) { if (matrix[i - 1][j - 1] == '1') { dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1; ans = max(ans, dp[i][j]); } } } return ans * ans; } }; #endif //CPP_0221__SOLUTION1_H_
ooooo-youwillsee/leetcode
1232-Check If It Is a Straight Line/cpp_1232/Solution1.h
<filename>1232-Check If It Is a Straight Line/cpp_1232/Solution1.h /** * @author ooooo * @date 2021/1/17 12:19 */ #ifndef CPP_1232__SOLUTION1_H_ #define CPP_1232__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool checkStraightLine(vector<vector<int>> &coordinates) { sort(coordinates.begin(), coordinates.end(), [](const vector<int> &x, const vector<int> &y) { return x[0] < y[0]; }); for (int i = 2; i < coordinates.size(); ++i) { int x1 = coordinates[i - 2][0], y1 = coordinates[i - 2][1]; int x2 = coordinates[i - 1][0], y2 = coordinates[i - 1][1]; int x3 = coordinates[i][0], y3 = coordinates[i][1]; if ((y2 - y1) * (x3 - x2) != (x2 - x1) * (y3 - y2)) { return false; } } return true; } }; #endif //CPP_1232__SOLUTION1_H_
ooooo-youwillsee/leetcode
1576-Replace-All-XLetter-to-Avoid-Consecutive-Repeating-Characters/cpp_1576/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/7 13:14 */ #ifndef CPP_1576__SOLUTION1_H_ #define CPP_1576__SOLUTION1_H_ #include <iostream> #include <string> using namespace std; class Solution { public: bool dfs(string &s, int i) { if (i == s.length()) return true; if (s[i] != '?' && dfs(s, i + 1)) return true; for (int j = 0; j < 26; ++j) { char c = j + 'a'; if (s[i - 1] != c && s[i + 1] != c) { s[i] = c; if (dfs(s, i + 1)) return true; s[i] = '?'; } } return false; } string modifyString(string s) { s = '$' + s + '$'; dfs(s, 1); s = s.substr(1, s.size() - 2); return s; } }; #endif //CPP_1576__SOLUTION1_H_
ooooo-youwillsee/leetcode
0234-Palindrome-Linked-List/cpp_0234/Solution2.h
// // Created by ooooo on 2020/1/8. // #ifndef CPP_0234_SOLUTION2_H #define CPP_0234_SOLUTION2_H #include "ListNode.h" class Solution { public: bool isPalindrome(ListNode *head) { if (!head || !head->next) return true; ListNode *low = head, *fast = head, *prev = nullptr, *temp = nullptr; while (fast->next && fast->next->next) { fast = fast->next->next; temp = low->next; low->next = prev; prev = low; low = temp; } ListNode *left = nullptr; ListNode *right = low->next; if (!fast->next) { left = prev; } else { low->next = prev; left = low; } bool match = true; while (match && left && right) { if (left->val != right->val) { match = false; break; } left = left->next; right = right->next; } return match; } }; #endif //CPP_0234_SOLUTION2_H
ooooo-youwillsee/leetcode
0127-Word-Ladder/cpp_0127/Solution2.h
<filename>0127-Word-Ladder/cpp_0127/Solution2.h // // Created by ooooo on 2019/12/31. // #ifndef CPP_0127_SOLUTION2_H #define CPP_0127_SOLUTION2_H #include <iostream> #include <string> #include <climits> #include <vector> #include <unordered_set> #include <unordered_map> using namespace std; class Solution { public: unordered_set<string> findBestMatch(string beginWord, unordered_set<string> wordSet, unordered_map<string, int> &visited, int depth) { unordered_set<string> res; for (int i = 0; i < beginWord.size(); ++i) { string s = beginWord; for (int j = 'a'; j < 'z'; ++j) { s[i] = j; if (!wordSet.count(s) || visited.count(s)) continue; visited[s] = depth; res.insert(s); } } return res; } int ladderLength(string beginWord, string endWord, vector<string> &wordList) { unordered_set<string> wordSet(wordList.begin(), wordList.end()); unordered_map<string, int> visited; int depth = 1; unordered_set<string> words = findBestMatch(beginWord, wordSet, visited, depth); while (!words.empty()) { depth += 1; unordered_set<string> container; for (auto item: words) { unordered_set<string> matches = findBestMatch(item, wordSet, visited, depth); if (visited.count(endWord)) return visited[endWord] + 1; container.insert(matches.begin(), matches.end()); } words = container; } //for (auto entry: visited) { // cout << entry.first << "->" << entry.second << "; "; //} //cout << endl; return 0; } }; #endif //CPP_0127_SOLUTION2_H
ooooo-youwillsee/leetcode
0824-Goat-Latin/cpp_0824/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/31. // #ifndef CPP_0824__SOLUTION1_H_ #define CPP_0824__SOLUTION1_H_ #include <iostream> using namespace std; /** * 拼接字符串效率低 */ class Solution { public: string help(string word, int i, bool space) { string newWord = ""; char firstChar = tolower(word[0]); if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || firstChar == 'o' || firstChar == 'u') { newWord = word.append("ma").append(string(i, 'a')); } else { newWord = word.substr(1, word.size() - 1) + word.substr(0, 1); newWord.append("ma").append(string(i, 'a')); } if (space) { newWord.append(" "); } return newWord; } string toGoatLatin(string S) { int i = 0, j = S.find(" ", i), count = 1; string ans = ""; while (j != -1) { string word = S.substr(i, j - i); ans += help(word, count++, true); i = j + 1; j = S.find(" ", i); } ans += help(S.substr(i, S.size() - i), count++, false); return ans; } }; #endif //CPP_0824__SOLUTION1_H_
ooooo-youwillsee/leetcode
0105-Construct-Binary-Tree-from-Preorder-and-Inorder Traversal/cpp_0105/Solution1.h
// // Created by ooooo on 2020/2/16. // #ifndef CPP_0105__SOLUTION1_H_ #define CPP_0105__SOLUTION1_H_ #include "TreeNode.h" #include <vector> #include <unordered_map> class Solution { public: TreeNode *help(int in_l, int in_r) { if (in_l > in_r) return nullptr; int root_value = preorder[preIndex++]; TreeNode *root = new TreeNode(root_value); int index = inorder_map[root_value]; root->left = help(in_l, index - 1); root->right = help(index + 1, in_r); return root; } int preIndex = 0; vector<int> preorder; unordered_map<int, int> inorder_map; TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { if (preorder.empty()) return nullptr; this->preorder = preorder; int n = inorder.size(); for (int i = 0; i < n; ++i) { inorder_map[inorder[i]] = i; } return help(0, n - 1); } }; #endif //CPP_0105__SOLUTION1_H_
ooooo-youwillsee/leetcode
0575-Distribute-Candies/cpp_0575/Solution1.h
// // Created by ooooo on 2020/1/12. // #ifndef CPP_0575__SOLUTION1_H_ #define CPP_0575__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: int distributeCandies(vector<int> &candies) { unordered_map<int, int> m, ans; int target = candies.size() / 2; for (const auto &candy: candies) { m[candy]++; } for (auto &entry: m) { if (entry.second == 1) { ans[entry.first] = entry.second; entry.second = 0; } } // 单个糖果的数量已经超过一半 if (ans.size() >= target) { return target; } int c = ans.size(); while (c != target) { for (auto &entry: m) { if (c == target) break; if (entry.second) { ans[entry.first]++; entry.second--; c++; } } } return ans.size(); } }; #endif //CPP_0575__SOLUTION1_H_
ooooo-youwillsee/leetcode
1190-Reverse Substrings Between Each Pair of Parentheses/cpp_1190/Solution1.h
/** * @author ooooo * @date 2021/5/26 13:25 */ #ifndef CPP_1190__SOLUTION1_H_ #define CPP_1190__SOLUTION1_H_ #include <iostream> #include <vector> #include <stack> using namespace std; class Solution { public: string reverseParentheses(string s) { stack<char> sk; for (int i = 0; i < s.size(); i++) { if (s[i] == ')') { string tmp = ""; while (sk.top() != '(') { tmp += sk.top(); sk.pop(); } sk.pop(); for (auto c :tmp) { sk.push(c); } } else { sk.push(s[i]); } } string ans = ""; while (!sk.empty()) { ans += sk.top(); sk.pop(); } reverse(ans.begin(), ans.end()); return ans; } }; #endif //CPP_1190__SOLUTION1_H_
ooooo-youwillsee/leetcode
0961-N-Repeated-Element-in-Size-2N-Array/cpp_0961/Solution1.h
// // Created by ooooo on 2020/1/17. // #ifndef CPP_0961__SOLUTION1_H_ #define CPP_0961__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: int repeatedNTimes(vector<int> &A) { unordered_set<int> s; for (auto item: A) { if (s.count(item)) return item; s.insert(item); } return -1; } }; #endif //CPP_0961__SOLUTION1_H_
ooooo-youwillsee/leetcode
0114-Flatten-Binary-Tree-to-Linked-List/cpp_0114/Solution3.h
// // Created by ooooo on 2020/2/16. // #ifndef CPP_0114__SOLUTION3_H_ #define CPP_0114__SOLUTION3_H_ #include "TreeNode.h" /** * in-place */ class Solution { public: void flatten(TreeNode *root) { while (root) { if (!root->left) { root = root->right; } else { TreeNode *prev = root->left; while (prev->right) prev = prev->right; prev->right = root->right; root->right = root->left; root->left = nullptr; root = root->right; } } } }; #endif //CPP_0114__SOLUTION3_H_
ooooo-youwillsee/leetcode
0937-Reorder-Log-File/cpp_0937/Solution1.h
// // Created by ooooo on 2020/2/1. // #ifndef CPP_0937__SOLUTION1_H_ #define CPP_0937__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { private: struct Word { // 标识符 string prefix; // 标识符后面的字符 string rest; // 完整的字符串 string completeStr; Word(const string &prefix, const string &rest, const string &complete_str) : prefix(prefix), rest(rest), completeStr(complete_str) {} Word() {} bool operator()(const Word &word1, const Word &word2) const { if (word1.rest == word2.rest) { return word1.prefix < word2.prefix; } return word1.rest < word2.rest; } }; public: vector<string> reorderLogFiles(vector<string> &logs) { // log1存放数字log vector<string> log1; vector<Word> log2; for (auto log: logs) { if (isdigit(log[log.size() - 1])) { log1.push_back(log); } else { int first_space_i = log.find(" "); Word word(log.substr(0, first_space_i), log.substr(first_space_i + 1, log.size() - first_space_i - 1), log); log2.push_back(word); } } sort(log2.begin(), log2.end(), Word()); vector<string> ans; for (auto &word: log2) { ans.push_back(word.completeStr); } ans.insert(ans.end(), log1.begin(), log1.end()); return ans; } }; #endif //CPP_0937__SOLUTION1_H_
ooooo-youwillsee/leetcode
0200-Number-of-Islands/cpp_0200/Solution2.h
// // Created by ooooo on 2020/2/19. // #ifndef CPP_0200__SOLUTION2_H_ #define CPP_0200__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * 并查集 */ class Solution { private: struct UnionFind { vector<int> parent; vector<int> rank; int count = 0; UnionFind(vector<vector<char>> &grid) { int m = grid.size(), n = grid[0].size(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '1') { parent.push_back(i * n + j); count += 1; } else { parent.push_back(-1); } rank.push_back(1); } } } // path compression int find(int p) { if (p != parent[p]) { parent[p] = find(parent[p]); } return parent[p]; } void unionElements(int p, int q) { int pRoot = find(p); int qRoot = find(q); if (pRoot == qRoot) return; if (rank[pRoot] < rank[qRoot]) { parent[pRoot] = qRoot; } else if (rank[pRoot] > rank[qRoot]) { parent[qRoot] = pRoot; } else { parent[pRoot] = qRoot; rank[qRoot] += 1; } count--; } }; public: int numIslands(vector<vector<char>> &grid) { if (grid.empty()) return 0; int m = grid.size(), n = grid[0].size(); UnionFind uf(grid); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '1') { grid[i][j] = '0'; if (j < n - 1 && grid[i][j + 1] == '1') { uf.unionElements(i * n + j, i * n + j + 1); } if (j > 0 && grid[i][j - 1] == '1') { uf.unionElements(i * n + j, i * n + j - 1); } if (i < m - 1 && grid[i + 1][j] == '1') { uf.unionElements(i * n + j, (i + 1) * n + j); } if (i > 0 && grid[i - 1][j] == '1') { uf.unionElements(i * n + j, (i - 1) * n + j); } } } } return uf.count; } }; #endif //CPP_0200__SOLUTION2_H_
ooooo-youwillsee/leetcode
0594-Longest-Harmonious-Subsequence/cpp_0594/Solution2.h
// // Created by ooooo on 2020/1/12. // #ifndef CPP_0594__SOLUTION2_H_ #define CPP_0594__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * 哈希表 + 单次扫描 */ class Solution { public: int findLHS(vector<int> &nums) { unordered_map<int, int> m; int ans = 0; for (const auto &num: nums) { m[num]++; if (m.count(num - 1)) { ans = max(ans, m[num] + m[num - 1]); } if (m.count(num + 1)) { ans = max(ans, m[num] + m[num + 1]); } } return ans; } }; #endif //CPP_0594__SOLUTION2_H_
ooooo-youwillsee/leetcode
0914-X-of-a-Kind-in-a-Deck-of-Cards/cpp_0914/Solution1.h
/** * @author ooooo * @date 2020/9/30 16:20 */ #ifndef CPP_0914__SOLUTION1_H_ #define CPP_0914__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: /** * 求最大公约数 */ int gcd(int x, int y) { if (x < y) return gcd(y, x); if (x % y == 0) return y; return gcd(y, x % y); } bool hasGroupsSizeX(vector<int> &deck) { unordered_map<int, int> m; for (auto &v : deck) { m[v]++; } int c = -1; for (auto &[k, v] : m) { if (c == -1) { c = v; } else { c = gcd(c, v); } if (c < 2) return false; } return true; } }; #endif //CPP_0914__SOLUTION1_H_
ooooo-youwillsee/leetcode
1207-Unique-Number-of-Occurrences/cpp_1207/Solution1.h
// // Created by ooooo on 2020/1/15. // #ifndef CPP_1207__SOLUTION1_H_ #define CPP_1207__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: bool uniqueOccurrences(vector<int> &arr) { unordered_map<int, int> m; unordered_set<int> s; for_each(arr.begin(), arr.end(), [&m](int x) { m[x]++; }); for (auto &entry: m) { if (s.count(entry.second)) return false; s.insert(entry.second); } return true; } }; #endif //CPP_1207__SOLUTION1_H_
ooooo-youwillsee/leetcode
0839-Similar String Groups/cpp_0839/Solution1.h
<filename>0839-Similar String Groups/cpp_0839/Solution1.h /** * @author ooooo * @date 2021/2/26 16:34 */ #ifndef CPP_0839__SOLUTION1_H_ #define CPP_0839__SOLUTION1_H_ #include <iostream> #include <vector> #include <set> using namespace std; class Solution { public: struct UF { vector<int> p; int n; UF(int n) : p(n), n(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 connect(int i, int j) { int pi = find(i), pj = find(j); if (pi == pj) { return true; } p[pi] = pj; n--; return false; } }; int numSimilarGroups(vector<string> &strs) { int n = strs.size(); UF uf(n); for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { int pi = uf.find(i), pj = uf.find(j); if (pi == pj) continue; if(check(strs[i], strs[j])) { uf.connect(i, j); } } } return uf.n; } bool check(string &s1, string &s2) { int cnt = 0; for (int i = 0; i < s1.size(); ++i) { if (s1[i] != s2[i]) { cnt++; } } return cnt <= 2; } }; #endif //CPP_0839__SOLUTION1_H_
ooooo-youwillsee/leetcode
0322-Coin-Change/cpp_0322/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/26. // #ifndef CPP_0322__SOLUTION2_H_ #define CPP_0322__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; /** * dfs timeout */ class Solution { public: vector<int> coins; unordered_map<int, int> memo; int ans = INT_MAX; void dfs(int amount, int count) { if (amount == 0) { ans = min(ans, count); return; } for (int i = 0; i < coins.size() && amount >= coins[i]; ++i) { dfs(amount - coins[i], count + 1); } } int coinChange(vector<int> &coins, int amount) { sort(coins.begin(), coins.end()); this->coins = coins; dfs(amount, 0); return ans == INT_MAX ? -1 : ans; } }; #endif //CPP_0322__SOLUTION2_H_
ooooo-youwillsee/leetcode
lcof_034/cpp_034/Solution1.h
// // Created by ooooo on 2020/3/22. // #ifndef CPP_034__SOLUTION1_H_ #define CPP_034__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: void dfs(TreeNode *node, int sum, vector<int> &nums) { if (!node) return; sum -= node->val; nums.push_back(node->val); if (sum == 0 && !node->left && !node->right) ans.push_back(nums); dfs(node->left, sum, nums); dfs(node->right, sum, nums); nums.pop_back(); } vector<vector<int>> ans; vector<vector<int>> pathSum(TreeNode *root, int sum) { vector<int> nums; dfs(root, sum, nums); return ans; } }; #endif //CPP_034__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_034/cpp_034/TreeNode.h
<filename>lcof_034/cpp_034/TreeNode.h // // Created by ooooo on 2020/3/22. // #ifndef CPP_034__TREENODE_H_ #define CPP_034__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(vector<int> nums) { if (nums.empty()) return; this->val = nums[0]; this->left = this->right = nullptr; queue<TreeNode *> q; q.push(this); for (int i = 1, len = nums.size(); i < len; i += 2) { auto node = q.front(); q.pop(); if (nums[i] != INT_MAX) node->left = new TreeNode(nums[i]); if (i + 1 >= len) return; if (nums[i + 1] != INT_MAX) node->right = new TreeNode(nums[i + 1]); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } } }; #endif //CPP_034__TREENODE_H_
ooooo-youwillsee/leetcode
0448-Find-All-Numbers-Disappeared-in-an-Array/cpp_0448/Solution2.h
// // Created by ooooo on 2020/2/6. // #ifndef CPP_0448__SOLUTION2_H_ #define CPP_0448__SOLUTION2_H_ #include <iostream> #include <vector> #include <cmath> using namespace std; class Solution { public: vector<int> findDisappearedNumbers(vector<int> &nums) { for (int i = 0; i < nums.size(); ++i) { int new_index = abs(nums[i]) - 1; if (nums[new_index] > 0) { nums[new_index] *= -1; } } vector<int> ans; for (int i = 1; i <= nums.size(); ++i) { if (nums[i - 1] > 0) { ans.push_back(i); } } return ans; } }; #endif //CPP_0448__SOLUTION2_H_
ooooo-youwillsee/leetcode
1664-Ways to Make a Fair Array/cpp_1664/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/12/11 10:31 */ #ifndef CPP_1664__SOLUTION1_H_ #define CPP_1664__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int waysToMakeFair(vector<int> &nums) { int n = nums.size(); if (n == 1) return 1; vector<int> sum(n); sum[0] = nums[0]; sum[1] = nums[1]; for (int i = 2; i < n; ++i) { sum[i] = sum[i - 2] + nums[i]; } int totalOdd, totalEven; if ((n - 1) % 2 == 0) { totalEven = sum[n - 1]; totalOdd = sum[n - 2]; } else { totalOdd = sum[n - 1]; totalEven = sum[n - 2]; } int ans = 0; for (int i = 0; i < n; ++i) { int prevOdd, prevEven, remainOdd, remainEven; if (i % 2 == 0) { prevOdd = i >= 1 ? sum[i - 1] : 0, prevEven = i >= 2 ? sum[i - 2] : 0; remainOdd = totalEven - nums[i] - prevEven, remainEven = totalOdd - prevOdd; } else { prevOdd = i >= 2 ? sum[i - 2] : 0, prevEven = i >= 1 ? sum[i - 1] : 0; remainEven = totalOdd - nums[i] - prevOdd, remainOdd = totalEven - prevEven; } if (prevOdd + remainOdd == prevEven + remainEven) { ans += 1; } } return ans; } }; #endif //CPP_1664__SOLUTION1_H_
ooooo-youwillsee/leetcode
0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/cpp_0235/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/9/27 11:04 */ #ifndef CPP_0235__SOLUTION2_H_ #define CPP_0235__SOLUTION2_H_ #include "TreeNode.h" class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { if (root->val > p->val && root->val > q->val) { return lowestCommonAncestor(root->left, p, q); } if (root->val < p->val && root->val < q->val) { return lowestCommonAncestor(root->right, p, q); } return root; } }; #endif //CPP_0235__SOLUTION2_H_
ooooo-youwillsee/leetcode
0530-Minimum-Absolute-Difference-in-BST/cpp_0530/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/2. // #ifndef CPP_0530_SOLUTION2_H #define CPP_0530_SOLUTION2_H #include "TreeNode.h" #include <climits> /** * 中序遍历, prev 存储前一个节点 */ class Solution { public: int min; TreeNode *prev; void inOrder(TreeNode *node) { if (!node) return; inOrder(node->left); if (prev != nullptr) { min = std::min(min, node->val - prev->val); } prev = node; inOrder(node->right); } int getMinimumDifference(TreeNode *root) { if (!root) return 0; min = INT_MAX; prev = nullptr; inOrder(root); return min; } }; #endif //CPP_0530_SOLUTION2_H
ooooo-youwillsee/leetcode
0448-Find-All-Numbers-Disappeared-in-an-Array/cpp_0448/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/6. // #ifndef CPP_0448__SOLUTION1_H_ #define CPP_0448__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: vector<int> findDisappearedNumbers(vector<int> &nums) { unordered_set<int> set(nums.begin(), nums.end()); vector<int> ans; for (int i = 1; i <= nums.size(); ++i) { if (!set.count(i)) { ans.push_back(i); } } return ans; } }; #endif //CPP_0448__SOLUTION1_H_
ooooo-youwillsee/leetcode
0141-Linked-List-Cycle/cpp_0141/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2019/10/30. // #ifndef CPP_0141_SOLUTION1_H #define CPP_0141_SOLUTION1_H #include <iostream> #include "ListNode.h" using namespace std; class Solution1 { public: bool hasCycle(ListNode *head) { if (!head) { return false; } ListNode *low = head; ListNode *fast = head->next; while (low && fast && fast->next) { low = low->next; fast = fast->next->next; if (low == fast) { return true; } } return false; } }; #endif //CPP_0141_SOLUTION1_H
ooooo-youwillsee/leetcode
0131-Palindrome Partitioning/cpp_0131/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/7 08:31 */ #ifndef CPP_0131__SOLUTION2_H_ #define CPP_0131__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: void dfs(string &s, int i, vector<string> &row) { if (i == s.size()) { ans.push_back(row); return; } for (int len = 1; len <= s.size() - i; ++len) { if (dp[i][i + len - 1]) { row.push_back(s.substr(i, len)); dfs(s, i + len, row); row.pop_back(); } } } vector<vector<bool>> dp; vector<vector<string>> ans; vector<vector<string>> partition(string s) { // 动态规划判断回文串 int n = s.size(); dp.assign(n, vector<bool>(n)); for (int i = 0; i < n; ++i) { dp[i][i] = true; } for (int i = n - 2; i >= 0; --i) { for (int j = i + 1; j < n; ++j) { if (j - i > 1) { dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j]; } else { dp[i][j] = s[i] == s[j]; } } } vector<string> row; dfs(s, 0, row); return ans; } }; #endif //CPP_0131__SOLUTION2_H_
ooooo-youwillsee/leetcode
0704-Binary-Search/cpp_0704/Solution1.h
// // Created by ooooo on 2020/1/19. // #ifndef CPP_0704__SOLUTION1_H_ #define CPP_0704__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int search(vector<int> &nums, int target) { if (nums.empty()) return -1; int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) { return mid; } else if (nums[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } }; #endif //CPP_0704__SOLUTION1_H_
ooooo-youwillsee/leetcode
0226-Invert-Binary-Tree/cpp_0226/Solution2.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/2. // #ifndef CPP_0226_SOLUTION2_H #define CPP_0226_SOLUTION2_H #include <queue> #include "TreeNode.h" class Solution { public: TreeNode *invertTree(TreeNode *root) { if (!root) return nullptr; queue<TreeNode *> q; q.push(root); while (!q.empty()) { TreeNode *node = q.front(); q.pop(); TreeNode *rightNode = node->right; node->right = node->left; node->left = rightNode; if (node->left) q.push(node->left); if (node->right) q.push(node->right); } return root; } }; #endif //CPP_0226_SOLUTION2_H
ooooo-youwillsee/leetcode
0098-Validate-Binary-Search-Tree/cpp_0098/Solution3.h
// // Created by ooooo on 2019/11/1. // #ifndef CPP_0098_SOLUTION3_H #define CPP_0098_SOLUTION3_H #include "TreeNode.h" #include <optional> using namespace std; class Solution { private: bool help(TreeNode *node, int *min, int *max) { if (node == NULL) { return true; } if (min && *min > node->val) return false; if (max && *max < node->val) return false; return help(node->left, min, &(node->val)) && help(node->right, &(node->val), max); } public: bool isValidBST(TreeNode *root) { return help(root, NULL, NULL); } }; #endif //CPP_0098_SOLUTION3_H
ooooo-youwillsee/leetcode
lcof_053-2/cpp_053-2/Solution3.h
// // Created by ooooo on 2020/4/10. // #ifndef CPP_053_2__SOLUTION3_H_ #define CPP_053_2__SOLUTION3_H_ #include <iostream> #include <vector> using namespace std; /** * binary */ class Solution { public: int missingNumber(vector<int> &nums) { int l = 0, r = nums.size() - 1; while (l <= r) { int mid = l + (r - l) / 2; if (nums[mid] == mid) { l = mid + 1; } else { if (mid == 0 || nums[mid - 1] == mid - 1) { return mid; } r = mid - 1; } } return nums.size(); } }; #endif //CPP_053_2__SOLUTION3_H_
ooooo-youwillsee/leetcode
0845-Longest Mountain in Array/cpp_0845/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2020/10/25 10:28 */ #ifndef CPP_0845__SOLUTION1_H_ #define CPP_0845__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dp */ class Solution { public: int longestMountain(vector<int> &A) { if (A.size() < 3)return 0; int m = A.size(); vector<int> dp(m, 0), top(m, 1); int ans = 0; for (int i = 1; i < m; ++i) { for (int j = i; j >= 0; --j) { if (A[j] < A[i]) { top[i] = max(top[i], top[j] + 1); } else if (A[j] > A[i] && top[j] >= 2) { dp[i] = max(dp[i], dp[j] + 1); dp[i] = max(dp[i], top[j] + 1); ans = max(dp[i], ans); if (dp[i] > i) break; } } //cout << i << " " << dp[i] << endl; } return ans; } }; #endif //CPP_0845__SOLUTION1_H_
ooooo-youwillsee/leetcode
0740-Delete and Earn/cpp_0740/Solution1.h
/** * @author ooooo * @date 2021/5/5 09:19 */ #ifndef CPP_0740__SOLUTION1_H_ #define CPP_0740__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int deleteAndEarn(vector<int> &nums) { unordered_map<int, int> m; int maxV = *max_element(nums.begin(), nums.end()); for (int i = 0; i < nums.size(); i++) { m[nums[i]]++; } int first = 0 * m[0], second = 1 * m[1]; int ans = max(first, second); for (int i = 2; i <= maxV; i++) { int tmp = second; second = max(second, first + i * m[i]); first = tmp; ans = max(first, second); } return ans; } }; #endif //CPP_0740__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_006/cpp_006/Solution2.h
// // Created by ooooo on 2020/3/7. // #ifndef CPP_006__SOLUTION2_H_ #define CPP_006__SOLUTION2_H_ #include "ListNode.h" /** * O(n) */ class Solution { public: vector<int> reversePrint(ListNode *head) { ListNode *cur = head; vector<int> ans; while (cur) { ans.emplace_back(cur->val); cur = cur->next; } reverse(ans.begin(), ans.end()); return ans; } }; #endif //CPP_006__SOLUTION2_H_
ooooo-youwillsee/leetcode
0879-Profitable Schemes/cpp_0879/Solution1.h
// // Created by ooooo on 6/14/2021. // #ifndef CPP_0879_SOLUTION1_H #define CPP_0879_SOLUTION1_H #include <iostream> #include <vector> #include <cstring> #include <numeric> using namespace std; class Solution { public: int profitableSchemes(int n, int minProfit, vector<int> &group, vector<int> &profit) { int m = profit.size(); int mod = 1e9 + 7; int dp[m + 1][n + 1][minProfit + 1]; memset(dp, 0, sizeof(dp)); for (int i = 0; i <= n; i++) { dp[0][i][0] = 1; } for (int i = 0; i < m; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= minProfit; k++) { dp[i + 1][j][k] = dp[i][j][k]; if (j >= group[i]) { dp[i + 1][j][k] = (dp[i + 1][j][k] + dp[i][j - group[i]][max(0, k - profit[i])]) % mod; } } } } return dp[m][n][minProfit]; } }; #endif //CPP_0879_SOLUTION1_H
ooooo-youwillsee/leetcode
1662-Check If Two String Arrays are Equivalent/cpp_1662/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2020/12/11 10:25 */ #ifndef CPP_1662__SOLUTION1_H_ #define CPP_1662__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool arrayStringsAreEqual(vector<string> &word1, vector<string> &word2) { string word_seq1 = ""; for (auto &item1 : word1) { word_seq1 += item1; } string word_seq2 = ""; for (auto &item2 : word2) { word_seq2 += item2; } return word_seq1 == word_seq2; } }; #endif //CPP_1662__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_049/cpp_049/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/6. // #ifndef CPP_049__SOLUTION1_H_ #define CPP_049__SOLUTION1_H_ #include <iostream> #include <unordered_set> using namespace std; /** * timeout */ class Solution { public: int nthUglyNumber(int n) { unordered_set<int> s{1, 2, 3, 5}; for (int i = 1; i < INT_MAX; ++i) { if (i == 1) n--; else { bool has = false; if (i % 2 == 0 && s.count(i / 2)) has = true; else if (i % 3 == 0 && s.count(i / 3)) has = true; else if (i % 5 == 0 && s.count(i / 5)) has = true; if (has) { s.insert(i); n--; } } if (n == 0) return i; } return -1; } }; #endif //CPP_049__SOLUTION1_H_
ooooo-youwillsee/leetcode
0341-Flatten Nested List Iterator/cpp_0341/Solution1.h
/** * @author ooooo * @date 2021/3/23 15:55 */ #ifndef CPP_0341__SOLUTION1_H_ #define CPP_0341__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class NestedInteger { public: // Return true if this NestedInteger holds a single integer, rather than a nested list. bool isInteger() const; // Return the single integer that this NestedInteger holds, if it holds a single integer // The result is undefined if this NestedInteger holds a nested list int getInteger() const; // Return the nested list that this NestedInteger holds, if it holds a nested list // The result is undefined if this NestedInteger holds a single integer const vector<NestedInteger> &getList() const; }; class NestedIterator { public: vector<int> ans; void dfs(vector<NestedInteger> &nestedList, vector<int> &vec) { for (int i = 0; i < nestedList.size(); ++i) { if (nestedList[i].isInteger()) { vec.push_back(nestedList[i].getInteger()); } else { dfs(nestedList[i].getList(), vec); } } } int i = 0; NestedIterator(vector<NestedInteger> &nestedList) { i = 0; ans.clear(); dfs(nestedList, ans); } int next() { return ans[i++]; } bool hasNext() { return i < ans.size(); } }; #endif //CPP_0341__SOLUTION1_H_
ooooo-youwillsee/leetcode
1648-Sell Diminishing-Valued Colored Balls/cpp_1648/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/3/5 11:11 */ #ifndef CPP_1648__SOLUTION1_H_ #define CPP_1648__SOLUTION1_H_ #include <iostream> #include <vector> #include <numeric> using namespace std; class Solution { public: static constexpr int MOD = 1000000007; int maxProfit(vector<int> &inventory, int orders) { long long l = 1, r = *max_element(inventory.begin(), inventory.end()); long long pos = 0; while (l <= r) { long long mid = l + (r - l) / 2; if (check(mid, inventory, orders)) { pos = mid; r = mid - 1; } else { l = mid + 1; } } long long total = 0; for (int i = 0; i < inventory.size(); ++i) { long long v = inventory[i]; if (v > pos) { total += (pos + 1 + v) * (inventory[i] - pos) / 2; orders -= (inventory[i] - pos); } } if (orders == 0) { return total % MOD; } return (total + orders * pos) % MOD; } bool check(long long mid, vector<int> &inventory, int orders) { long long sum = 0; for (int i = 0; i < inventory.size(); ++i) { long long v = inventory[i]; if (v > mid) { sum += (v - mid); } } return sum <= orders; } }; #endif //CPP_1648__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_006/cpp_006/Solution1.h
// // Created by ooooo on 2020/3/7. // #ifndef CPP_006__SOLUTION1_H_ #define CPP_006__SOLUTION1_H_ #include "ListNode.h" /** * O(n^2) */ class Solution { public: vector<int> reversePrint(ListNode *head) { ListNode *cur = head; vector<int> ans; while (cur) { ans.insert(ans.begin(), cur->val); cur = cur->next; } return ans; } }; #endif //CPP_006__SOLUTION1_H_
ooooo-youwillsee/leetcode
0617-Merge-Two-Binary-Trees/cpp_0617/Solution1.h
<filename>0617-Merge-Two-Binary-Trees/cpp_0617/Solution1.h<gh_stars>10-100 // // Created by ooooo on 2019/12/31. // #ifndef CPP_0617_SOLUTION1_H #define CPP_0617_SOLUTION1_H #include "TreeNode.h" class Solution { public: TreeNode *mergeTrees(TreeNode *t1, TreeNode *t2) { if (!t1 && !t2) return nullptr; if (!t1) return t2; if (!t2) return t1; // 重用旧节点 t1->val = t1->val + t2->val; t1->left = mergeTrees(t1->left, t2->left); t1->right = mergeTrees(t1->right, t2->right); return t1; } }; #endif //CPP_0617_SOLUTION1_H
ooooo-youwillsee/leetcode
0217-Contains-Duplicate/cpp_0217/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/1/6. // #ifndef CPP_0217_SOLUTION1_H #define CPP_0217_SOLUTION1_H #include <iostream> #include <vector> #include <unordered_set> using namespace std; class Solution { public: bool containsDuplicate(vector<int> &nums) { unordered_set<int> s; for (int i = 0; i < nums.size(); ++i) { if (s.count(nums[i])) { return true; } s.insert(nums[i]); } return false; } }; #endif //CPP_0217_SOLUTION1_H
ooooo-youwillsee/leetcode
0328-Odd Even Linked List/cpp_0328/ListNode.h
/** * @author ooooo * @date 2020/11/13 21:07 */ #ifndef CPP_0328__LISTNODE_H_ #define CPP_0328__LISTNODE_H_ #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {} }; #endif //CPP_0328__LISTNODE_H_
ooooo-youwillsee/leetcode
0965-Univalued-Binary-Tree/cpp_0965/TreeNode.h
<filename>0965-Univalued-Binary-Tree/cpp_0965/TreeNode.h // // Created by ooooo on 2019/12/31. // #ifndef CPP_0965_TREENODE_H #define CPP_0965_TREENODE_H #include <iostream> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; #endif //CPP_0965_TREENODE_H
ooooo-youwillsee/leetcode
0896-Monotonic-Array/cpp_0896/Solution1.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2020/9/30 15:57 */ #ifndef CPP_0896__SOLUTION1_H_ #define CPP_0896__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: bool isMonotonic(vector<int> &A) { if (A.size() <= 2) return true; bool is_incr = A[0] <= A[A.size() - 1]; for (int i = 1; i < A.size(); ++i) { if (is_incr) { if (A[i] < A[i - 1]) return false; } else { if (A[i] > A[i - 1]) return false; } } return true; } }; #endif //CPP_0896__SOLUTION1_H_
ooooo-youwillsee/leetcode
0703-Kth-Largest-Element-in-a-Stream/cpp_0703/Solution1.h
// // Created by ooooo on 2019/10/30. // #ifndef CPP_0703_SOLUTION1_H #define CPP_0703_SOLUTION1_H #include <iostream> #include <queue> using namespace std; class KthLargest { private: priority_queue<int, vector<int>, greater<int>> q; int k; public: KthLargest(int k, vector<int> &nums) { this->k = k; for (auto item : nums) { add(item); } } int add(int val) { if (q.size() < k) { q.push(val); } else if (q.top() < val) { q.pop(); q.push(val); } return q.top(); } }; #endif //CPP_0703_SOLUTION1_H
ooooo-youwillsee/leetcode
1202-Smallest String With Swaps/cpp_1202/Solution2.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/2/13 09:17 */ #ifndef CPP_1202__SOLUTION2_H_ #define CPP_1202__SOLUTION2_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: class UF { public: vector<int> p; int n; 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]); } int connect(int i, int j) { int pi = find(i), pj = find(j); if (pi == pj) return pi; p[pi] = pj; return pj; } }; string smallestStringWithSwaps(string s, vector<vector<int>> &pairs) { int sz = s.size(); UF uf(sz); unordered_map<int, priority_queue<char, vector<char>, greater<int>>> m; for (auto &pair : pairs) { uf.connect(pair[0], pair[1]); } for (int j = 0; j < s.size(); ++j) { int pRoot = uf.find(j); m[pRoot].push(s[j]); } string ans = ""; for (int i = 0; i < s.size(); ++i) { auto &letters = m[uf.find(i)]; ans += letters.top(); letters.pop(); } return ans; } }; #endif //CPP_1202__SOLUTION2_H_
ooooo-youwillsee/leetcode
0287-Find-the-Duplicate-Number/cpp_0287/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/23. // #ifndef CPP_0287__SOLUTION2_H_ #define CPP_0287__SOLUTION2_H_ #include <iostream> #include <vector> using namespace std; /** * sort */ class Solution { public: int findDuplicate(vector<int> &nums) { sort(nums.begin(), nums.end()); for (int i = 0; i < nums.size() - 1; ++i) { if (nums[i] == nums[i + 1]) return nums[i]; } return -1; } }; #endif //CPP_0287__SOLUTION2_H_
ooooo-youwillsee/leetcode
lcof_032-2/cpp_032-2/Solution2.h
// // Created by ooooo on 2020/3/20. // #ifndef CPP_032_2__SOLUTION2_H_ #define CPP_032_2__SOLUTION2_H_ #include "TreeNode.h" #include <queue> /** * dfs */ class Solution { public: void dfs(TreeNode *node, int level) { if (!node) return; if (ans.size() <= level) { ans.push_back({}); } ans[level].emplace_back(node->val); dfs(node->left, level + 1); dfs(node->right, level + 1); } vector<vector<int>> ans; vector<vector<int>> levelOrder(TreeNode *root) { dfs(root, 0); return ans; } }; #endif //CPP_032_2__SOLUTION2_H_
ooooo-youwillsee/leetcode
1562-Find Latest Group of Size M/cpp_1562/Solution1.h
/** * @author ooooo * @date 2020/12/12 18:33 */ #ifndef CPP_1562__SOLUTION1_H_ #define CPP_1562__SOLUTION1_H_ #include <iostream> #include <vector> #include <set> using namespace std; class Solution { public: int findLatestStep(vector<int> &arr, int m) { int n = arr.size(); if (m == n) return n; set<int> insert = {0, n + 1}; for (int i = n - 1; i >= 0; --i) { int k = arr[i]; auto it = insert.lower_bound(k); if (k - *prev(it) - 1 == m || *it - k - 1 == m) return i; insert.insert(k); } return -1; } }; #endif //CPP_1562__SOLUTION1_H_
ooooo-youwillsee/leetcode
0287-Find-the-Duplicate-Number/cpp_0287/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/2/23. // #ifndef CPP_0287__SOLUTION1_H_ #define CPP_0287__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> using namespace std; /** * hash */ class Solution { public: int findDuplicate(vector<int> &nums) { unordered_set<int> set; for (auto &num : nums) { if (set.count(num)) { return num; } set.insert(num); } return -1; } }; #endif //CPP_0287__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_048/cpp_048/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/4/5. // #ifndef CPP_048__SOLUTION1_H_ #define CPP_048__SOLUTION1_H_ #include <iostream> using namespace std; /** * 滑动窗口 */ class Solution { public: int lengthOfLongestSubstring(string s) { int i = 0, j = 0, ans = 0; bool arr[256] = {false}; while (j < s.size()) { if (arr[s[j]]) { arr[s[i]] = false; i++; } else { arr[s[j]] = true; j++; } ans = max(ans, j - i); } return ans; } }; #endif //CPP_048__SOLUTION1_H_
ooooo-youwillsee/leetcode
0501-Find-Mode-in-Binary-Search-Tree/cpp_0501/TreeNode.h
<filename>0501-Find-Mode-in-Binary-Search-Tree/cpp_0501/TreeNode.h<gh_stars>10-100 // // Created by ooooo on 2020/1/2. // #ifndef CPP_0501_TREENODE_H #define CPP_0501_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) {} }; #endif //CPP_0501_TREENODE_H
ooooo-youwillsee/leetcode
0724-Find-Pivot-Index/cpp_0724/Solution1.h
<filename>0724-Find-Pivot-Index/cpp_0724/Solution1.h /** * @author ooooo * @date 2021/1/28 20:34 */ #ifndef CPP_0724__SOLUTION1_H_ #define CPP_0724__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int pivotIndex(vector<int> &nums) { int n = nums.size(); vector<int> sum(n + 1); for (int i = 0; i < n; ++i) { sum[i + 1] = sum[i] + nums[i]; } for (int i = 0; i < n; ++i) { if (sum[i] == sum[n] - sum[i + 1]) { return i; } } return -1; } }; #endif //CPP_0724__SOLUTION1_H_
ooooo-youwillsee/leetcode
0721-Accounts Merge/cpp_0721/Solution1.h
<filename>0721-Accounts Merge/cpp_0721/Solution1.h /** * @author ooooo * @date 2021/1/18 17:13 */ #ifndef CPP_0721__SOLUTION1_H_ #define CPP_0721__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; class Solution { public: unordered_map<string, int> email_index; vector<int> p; vector<vector<string>> accountsMerge(vector<vector<string>> &accounts) { int count = 0; for (int i = 0; i < accounts.size(); ++i) { for (int j = 1; j < accounts[i].size(); ++j) { email_index[accounts[i][j]] = count; count++; } } p.assign(count, 0); for (int i = 0; i < p.size(); ++i) { p[i] = i; } for (int i = 0; i < accounts.size(); ++i) { string first = accounts[i][1]; for (int j = 2; j < accounts[i].size(); ++j) { connected(email_index[first], email_index[accounts[i][j]]); } } unordered_map<int, pair<string, unordered_set<string>>> groups; for (int i = 0; i < accounts.size(); ++i) { for (int j = 1; j < accounts[i].size(); ++j) { int groupId = find(email_index[accounts[i][j]]); groups[groupId].first = accounts[i][0]; // name groups[groupId].second.insert(accounts[i][j]); // email } } vector<vector<string>> ans; for (auto &entry: groups) { string name = entry.second.first; ans.push_back({name}); for (auto &email: entry.second.second) { ans.back().push_back(email); } sort(ans.back().begin(), ans.back().end()); } return ans; } 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(p, i), pj = find(p, j); if (pi == pj) { return true; } p[pi] = pj; return false; } }; #endif //CPP_0721__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_025/cpp_025/Solution2.h
// // Created by ooooo on 2020/3/16. // #ifndef CPP_025__SOLUTION2_H_ #define CPP_025__SOLUTION2_H_ #include "ListNode.h" class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (!l1) return l2; if (!l2) return l1; ListNode * ans = nullptr; if (l1->val <= l2->val) { ans = l1; ans->next = mergeTwoLists(l1->next, l2); }else { ans = l2; ans->next = mergeTwoLists(l1, l2->next); } return ans; } }; #endif //CPP_025__SOLUTION2_H_
ooooo-youwillsee/leetcode
lcof_042/cpp_042/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/27. // #ifndef CPP_042__SOLUTION1_H_ #define CPP_042__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxSubArray(vector<int> &nums) { int n = nums.size(), ans = nums[0]; vector<int> dp(n, 0); dp[0] = nums[0]; for (int i = 1; i < n; ++i) { if (dp[i - 1] < 0) dp[i] = nums[i]; else dp[i] = dp[i - 1] + nums[i]; ans = std::max(ans, dp[i]); } return ans; } }; #endif //CPP_042__SOLUTION1_H_
ooooo-youwillsee/leetcode
0959-Regions Cut By Slashes/cpp_0959/Solution1.h
/** * @author ooooo * @date 2021/1/25 19:35 */ #ifndef CPP_0959__SOLUTION1_H_ #define CPP_0959__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> using namespace std; class Solution { public: struct UF { int n; vector<int> p; UF(int n) : n(n), p(n) { for (int i = 0; i < n; ++i) { p[i] = i; } } 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; n--; return false; } }; /** * 0 * 3 1 * 2 * @param grid * @return */ int regionsBySlashes(vector<string> &grid) { int n = grid.size(); UF uf(4 * n * n); // 左上合并 for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int index = 4 * (j + i * n); if (grid[i][j] == '/') { uf.connect(index + 1, index + 2); uf.connect(index + 0, index + 3); } else if (grid[i][j] == '\\') { uf.connect(index + 0, index + 1); uf.connect(index + 3, index + 2); } else { uf.connect(index + 0, index + 1); uf.connect(index + 0, index + 2); uf.connect(index + 0, index + 3); } // 向上一层合并 if (i >= 1) { uf.connect(index + 0, 4 * ((i - 1) * n + j) + 2); } if (j >= 1) { uf.connect(index + 3, 4 * (i * n + j - 1) + 1); } } } return uf.n; } }; #endif //CPP_0959__SOLUTION1_H_
ooooo-youwillsee/leetcode
0337-House-Robber-III/cpp_0337/Solution1.h
// // Created by ooooo on 2020/2/25. // #ifndef CPP_0337__SOLUTION1_H_ #define CPP_0337__SOLUTION1_H_ #include "TreeNode.h" using namespace std; class Solution { public: int dfs(TreeNode *node, bool rob) { if (!node) return 0; // 添加下面这一行,减少递归次数,程序通过!!! if (!node->left && !node->right) return rob ? node->val : 0; int ans = 0; if (rob) { ans = node->val + dfs(node->left, false) + dfs(node->right, false); } return max(ans, dfs(node->left, true) + dfs(node->right, true)); } int rob(TreeNode *root) { return dfs(root, true); } }; #endif //CPP_0337__SOLUTION1_H_
ooooo-youwillsee/leetcode
0096-Unique-Binary-Search-Trees/cpp_0096/Solution1.h
// // Created by ooooo on 2020/2/15. // #ifndef CPP_0096__SOLUTION1_H_ #define CPP_0096__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * f(i,n) 表示以 i 为根,长度为 n 的个数 ( 1 <= i <= n) * G(n) 表示长度为 n 的总个数 * 即: G(n) = f(1, n) + f(2, n) +...+ f(n, n) * 而 f(i, n) = G(i-1) * G(n-i) * * ==> G(n) = G(0) * G(n-1) + G(1) * G(n-2) +...+ G(n-1) * G(0) */ class Solution { public: int numTrees(int n) { vector<int> g(n + 1); g[0] = g[1] = 1; for (int k = 2; k <= n; ++k) { for (int i = 1; i <= k; ++i) { g[k] += g[i - 1] * g[k - i]; } } return g[n]; } }; #endif //CPP_0096__SOLUTION1_H_
ooooo-youwillsee/leetcode
0020-Valid-Parentheses/cpp_0020/Solution1.h
// // Created by ooooo on 2019/10/30. // #ifndef CPP_0020_SOLUTION1_H #define CPP_0020_SOLUTION1_H #include <iostream> #include <stack> #include <map> using namespace std; class Solution1 { public: bool isValid(string s) { stack<char> stack; map<char, char> map; map['}'] = '{'; map[')'] = '('; map[']'] = '['; for (auto i : s) { if (map.find(i) == map.end()) { stack.push(i); } else { if (stack.empty() || stack.top() != map[i]) { return false; } else { stack.pop(); } } } return stack.empty(); } }; #endif //CPP_0020_SOLUTION1_H
ooooo-youwillsee/leetcode
0500-Keyboard-Row/cpp_0500/Solution1.h
<reponame>ooooo-youwillsee/leetcode<filename>0500-Keyboard-Row/cpp_0500/Solution1.h // // Created by ooooo on 2020/1/11. // #ifndef CPP_0500__SOLUTION1_H_ #define CPP_0500__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<string> findWords(vector<string> &words) { if (words.empty()) return {}; unordered_map<char, int> m = { {'q', 1}, {'w', 1}, {'e', 1}, {'r', 1}, {'t', 1}, {'y', 1}, {'u', 1}, {'i', 1}, {'o', 1}, {'p', 1}, {'a', 2}, {'s', 2}, {'d', 2}, {'f', 2}, {'g', 2}, {'h', 2}, {'j', 2}, {'k', 2}, {'l', 2}, {'z', 3}, {'x', 3}, {'c', 3}, {'v', 3}, {'b', 3}, {'n', 3}, {'m', 3} }; vector<string> ans; for (const auto &word: words) { unordered_map<int, int> count; for (const auto &c: word) { // 属于同一组的+1 count[m[tolower(c)]]++; } if (count.size() == 1) { ans.push_back(word); } } return ans; } }; #endif //CPP_0500__SOLUTION1_H_
ooooo-youwillsee/leetcode
0205-Isomorphic-Strings/cpp_0205/Solution1.h
// // Created by ooooo on 2020/1/9. // #ifndef CPP_0205_SOLUTION1_H #define CPP_0205_SOLUTION1_H #include <iostream> #include <unordered_map> using namespace std; class Solution { public: bool isIsomorphic(string s, string t) { unordered_map<char, char> m1; unordered_map<char, char> m2; for (int i = 0; i < s.size(); ++i) { if (m1.count(s[i])) { if (m1[s[i]] != t[i]) return false; } else { if (m2.count(t[i]) && m2[t[i]] != s[i]) return false; // 相互映射 m1[s[i]] = t[i]; m2[t[i]] = s[i]; } } return true; } }; #endif //CPP_0205_SOLUTION1_H
ooooo-youwillsee/leetcode
0834-Sum of Distances in Tree/cpp_0834/Solution1.h
/** * @author ooooo * @date 2020/10/6 11:59 */ #ifndef CPP_0834__SOLUTION1_H_ #define CPP_0834__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> #include <numeric> using namespace std; /** * bfs timeout */ class Solution { public: int bfs(vector<vector<int>> &g, int i) { vector<int> d(g.size(), 0); vector<int> visited(g.size(), false); queue<int> q; q.push(i); int ans = 0; while (!q.empty()) { int u = q.front(); q.pop(); visited[u] = true; for (auto &v: g[u]) { if (visited[v]) continue; d[v] = d[u] + 1; q.push(v); ans += d[v]; } } return ans; } vector<int> sumOfDistancesInTree(int N, vector<vector<int>> &edges) { vector<vector<int>> g(N); for (auto &e: edges) { int u = e[0], v = e[1]; g[u].push_back(v); g[v].push_back(u); } vector<int> ans; for (int i = 0; i < N; ++i) { ans.push_back(bfs(g, i)); } return ans; } }; #endif //CPP_0834__SOLUTION1_H_
ooooo-youwillsee/leetcode
1785-Minimum Elements to Add to Form a Given Sum/cpp_1785/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2021/3/14 15:01 */ #ifndef CPP_1785__SOLUTION1_H_ #define CPP_1785__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 minElements(vector<int> &nums, int limit, int goal) { long long sum = accumulate(nums.begin(), nums.end(), (long long) 0); long long target = abs(goal - sum); if (target == 0) { return 0; } return target % limit == 0 ? target / limit : target / limit + 1; } }; #endif //CPP_1785__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_012/cpp_012/Solution1.h
// // Created by ooooo on 2020/3/10. // #ifndef CPP_012__SOLUTION1_H_ #define CPP_012__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * dfs( rows * cols * len(word)) */ class Solution { public: bool is_valid(int i, int j, int word_index) { return i >= 0 && i < board.size() && j >= 0 && j < board[0].size() && board[i][j] == word[word_index] && !marked[i][j]; } bool dfs(int i, int j, int word_index) { if (word_index == word.size()) return true; if (!is_valid(i, j, word_index)) return false; marked[i][j] = true; for (int k = 0; k < 4; ++k) { int dx = i + dx_dy[k][0], dy = j + dx_dy[k][1]; if (dfs(dx, dy, word_index + 1)) { return true; } } marked[i][j] = false; return false; } vector<vector<char>> board; vector<vector<bool>> marked; vector<vector<int>> dx_dy = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; string word; bool exist(vector<vector<char>> &board, string word) { int rows = board.size(), cols = board[0].size(); this->board = board; this->word = word; this->marked.assign(rows, vector<bool>(cols, false)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if (dfs(i, j, 0)) return true; } } return false; } }; #endif //CPP_012__SOLUTION1_H_
ooooo-youwillsee/leetcode
1824-Minimum Sideway Jumps/cpp_1824/Solution1.h
/** * @author ooooo * @date 2021/4/19 09:37 */ #ifndef CPP_1824__SOLUTION1_H_ #define CPP_1824__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution1 { public: int minSideJumps(vector<int> nums) { int n = nums.size(); vector<vector<int>> dp(n, vector<int>(4, n * 3 + 1)); dp[0][1] = dp[0][3] = 1; dp[0][2] = 0; for (int i = 1; i < n; i++) { if (nums[i] == 2) { dp[i][1] = min(dp[i - 1][1], 1 + dp[i - 1][3]); dp[i][3] = min(dp[i - 1][1] + 1, dp[i - 1][3]); } else if (nums[i] == 1) { dp[i][2] = min(dp[i - 1][2], 1 + dp[i - 1][3]); dp[i][3] = min(dp[i - 1][2] + 1, dp[i - 1][3]); } else if (nums[i] == 3) { dp[i][1] = min(dp[i - 1][1], 1 + dp[i - 1][2]); dp[i][2] = min(dp[i - 1][1] + 1, dp[i - 1][2]); } else if (nums[i] == 0) { dp[i][1] = min(min(dp[i - 1][1], dp[i - 1][2] + 1), dp[i - 1][3] + 1); dp[i][2] = min(min(dp[i - 1][2], dp[i - 1][1] + 1), dp[i - 1][3] + 1); dp[i][3] = min(min(dp[i - 1][3], dp[i - 1][1] + 1), dp[i - 1][2] + 1); } dp[i][nums[i]] = n * 3 + 1; } return min(min(dp[n - 1][1], dp[n - 1][2]), dp[n - 1][3]); } }; #endif //CPP_1824__SOLUTION1_H_
ooooo-youwillsee/leetcode
0389-Find-the-Difference/cpp_0389/Solution2.h
// // Created by ooooo on 2020/1/11. // #ifndef CPP_0389__SOLUTION2_H_ #define CPP_0389__SOLUTION2_H_ #include <iostream> #include <unordered_map> using namespace std; /** * map */ class Solution { public: char findTheDifference(string s, string t) { unordered_map<char, int> m; for (auto &c:s) { m[c]++; } for (int i = 0; i < t.size(); ++i) { if (m[t[i]]) { m[t[i]]--; } else return t[i]; } return '\0'; } }; #endif //CPP_0389__SOLUTION2_H_
ooooo-youwillsee/leetcode
0347-Top-K-Frequent-Elements/cpp_0347/Solution1.h
// // Created by ooooo on 2020/1/24. // #ifndef CPP_0347__SOLUTION1_H_ #define CPP_0347__SOLUTION1_H_ #include <iostream> #include <vector> #include <queue> #include <unordered_map> using namespace std; class Solution { public: struct Freq { int val; int times; Freq(int val, int times) : val(val), times(times) {} bool operator<(const Freq &x) const { return this->times > x.times; } }; vector<int> topKFrequent(vector<int> &nums, int k) { unordered_map<int, int> m; for (auto num: nums) m[num]++; priority_queue<Freq> pq; for (auto entry: m) { Freq item(entry.first, entry.second); if (pq.size() < k) { pq.push(item); } else { Freq top = pq.top(); if (item.times > top.times) { pq.pop(); pq.push(item); } } } vector<int> ans; while (!pq.empty()) { ans.push_back(pq.top().val); pq.pop(); } return ans; } }; #endif //CPP_0347__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcp-18/cpp_018/Solution1.h
/** * @author ooooo * @date 2020/9/26 17:23 */ #ifndef CPP_018__SOLUTION1_H_ #define CPP_018__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int breakfastNumber(vector<int> &staple, vector<int> &drinks, int x) { sort(staple.begin(), staple.end()); sort(drinks.begin(), drinks.end()); int ans = 0; for (auto &item: staple) { auto it = upper_bound(drinks.begin(), drinks.end(), x - item); ans = (it - drinks.begin() + ans) % 1000000007; } return ans; } }; #endif //CPP_018__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_045/cpp_045/Solution2.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/4/1. // #ifndef CPP_045__SOLUTION2_H_ #define CPP_045__SOLUTION2_H_ #include <iostream> #include <vector> #include <sstream> using namespace std; /** * ab < ba */ class Solution { public: string minNumber(vector<int> &nums) { vector<string> vec; for (auto &num: nums) vec.push_back(to_string(num)); sort(vec.begin(), vec.end(), [](string &s1, string s2) { return s1 + s2 < s2 + s1; }); stringstream ss; for (auto &str: vec) { ss << str; } return ss.str(); } }; #endif //CPP_045__SOLUTION2_H_
ooooo-youwillsee/leetcode
0106-Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/cpp_0106/Solution1.h
<filename>0106-Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/cpp_0106/Solution1.h /** * @author ooooo * @date 2020/9/25 09:24 */ #ifndef CPP_0106__SOLUTION1_H_ #define CPP_0106__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: vector<int> postorder; int k; // 后序位置,用于判断根节点 TreeNode *dfs(int i, int j) { if (i > j) return NULL; int val = postorder[k--]; TreeNode *node = new TreeNode(val); int cur_index = index_map[val]; node->right = dfs(cur_index + 1, j); node->left = dfs(i, cur_index - 1); return node; } unordered_map<int, int> index_map; TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { this->postorder = postorder; for (int i = 0; i < inorder.size(); ++i) { index_map[inorder[i]] = i; } k = postorder.size() - 1; return dfs(0, inorder.size() - 1); } }; #endif //CPP_0106__SOLUTION1_H_
ooooo-youwillsee/leetcode
1035-Uncrossed Lines/cpp_1035/Solution1.h
/** * @author ooooo * @date 2021/5/21 07:35 */ #ifndef CPP_1035__SOLUTION1_H_ #define CPP_1035__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int maxUncrossedLines(vector<int> &nums1, vector<int> &nums2) { int m = nums1.size(), n = nums2.size(); vector<vector<int>> dp(m + 1, vector<int>(n + 1)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (nums1[i] == nums2[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(max(dp[i][j], dp[i + 1][j]), dp[i][j + 1]); } } } return dp[m][n]; } }; #endif //CPP_1035__SOLUTION1_H_
ooooo-youwillsee/leetcode
0763-Partition Labels/cpp_0763/Solution2.h
<reponame>ooooo-youwillsee/leetcode<gh_stars>10-100 /** * @author ooooo * @date 2020/10/22 15:49 */ #ifndef CPP_0763__SOLUTION2_H_ #define CPP_0763__SOLUTION2_H_ #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<int> partitionLabels(string s) { vector<int> ans; int l = 0, r = 0; unordered_map<char, int> char_indexes; for (int i = 0; i < s.size(); ++i) { char_indexes[s[i]] = i; } while (l < s.size()) { int prev = l; while (l <= r) { r = max(r, char_indexes[s[l]]); l++; } ans.push_back(l - prev); r = l; } return ans; } }; #endif //CPP_0763__SOLUTION2_H_
ooooo-youwillsee/leetcode
0993-Cousins-in-Binary-Tree/cpp_0993/Solution3.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/5/17 21:58 */ #ifndef CPP_0993__SOLUTION3_H_ #define CPP_0993__SOLUTION3_H_ #include "TreeNode.h" #include <iostream> #include <queue> using namespace std; class Solution { public: bool isCousins(TreeNode *root, int x, int y) { if (!root) return false; queue<TreeNode *> q; q.push(root); while (!q.empty()) { bool isX = false, isY = false; for (int i = 0, sz = q.size(); i < sz; i++) { TreeNode *node = q.front(); q.pop(); if (node->val == x) isX = true; if (node->val == y) isY = true; if (node->left) { q.push(node->left); } if (node->right) { q.push(node->right); } if (node->left && node->right) { if (node->left->val == x && node->right->val == y) return false; if (node->right->val == x && node->left->val == y) return false; } } if (isX && isY) return true; if ((isX && !isY) || (!isX && isY)) return false; } return false; } }; #endif //CPP_0993__SOLUTION3_H_
ooooo-youwillsee/leetcode
lcp-19/cpp_019/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/10/1 18:31 */ #ifndef CPP_019__SOLUTION1_H_ #define CPP_019__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: // 状态0 表示 最左边的为红色 // 状态1 表示 中间的为黄色 // 状态2 表示 最右边的为红色 int minimumOperations(string leaves) { vector<vector<int>> dp(leaves.size(), vector<int>(3, 0)); dp[0][0] = leaves[0] == 'y'; dp[0][1] = dp[0][2] = dp[1][2] = leaves.size(); // 不可能存在 for (int i = 1; i < leaves.size(); ++i) { int toRed = leaves[i] == 'y'; int toYellow = leaves[i] == 'r'; dp[i][0] = dp[i - 1][0] + toRed; dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + toYellow; if (i >= 2) dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + toRed; } return dp[leaves.size() - 1][2]; } }; #endif //CPP_019__SOLUTION1_H_
ooooo-youwillsee/leetcode
0023-Merge-k-Sorted-Lists/cpp_0023/ListNode.h
/** * @author ooooo * @date 2020/9/25 22:36 */ #ifndef CPP_0023__LISTNODE_H_ #define CPP_0023__LISTNODE_H_ #include <iostream> #include <vector> #include <queue> using namespace std; struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {} }; #endif //CPP_0023__LISTNODE_H_
ooooo-youwillsee/leetcode
1442-Count Triplets That Can Form Two Arrays of Equal XOR/cpp_1442/Solution1.h
<filename>1442-Count Triplets That Can Form Two Arrays of Equal XOR/cpp_1442/Solution1.h /** * @author ooooo * @date 2021/5/22 11:37 */ #ifndef CPP_1442__SOLUTION1_H_ #define CPP_1442__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; class Solution { public: int countTriplets(vector<int> &arr) { int n = arr.size(); vector<int> pre(n + 1); for (int i = 0; i < n; i++) { pre[i + 1] = pre[i] ^ arr[i]; } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j; k < n; k++) { int a = pre[j] ^pre[i]; int b = pre[k + 1] ^pre[j]; if (a == b) ans++; } } } return ans; } }; #endif //CPP_1442__SOLUTION1_H_
ooooo-youwillsee/leetcode
0222-Count Complete Tree Nodes/cpp_0222/Solution1.h
<reponame>ooooo-youwillsee/leetcode /** * @author ooooo * @date 2020/11/24 09:07 */ #ifndef CPP_0222__SOLUTION1_H_ #define CPP_0222__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: int countNodes(TreeNode *root) { if (root == nullptr) return 0; return countNodes(root->left) + countNodes(root->right) + 1; } }; #endif //CPP_0222__SOLUTION1_H_
ooooo-youwillsee/leetcode
0387-First-Unique-Character-in-a-String/cpp_0387/Solution1.h
<gh_stars>10-100 // // Created by ooooo on 2020/1/11. // #ifndef CPP_0387__SOLUTION1_H_ #define CPP_0387__SOLUTION1_H_ #include <iostream> #include <unordered_map> using namespace std; class Solution { public: int firstUniqChar(string s) { unordered_map<char, int> m; for (auto &c: s) { m[c]++; } for (int i = 0; i < s.size(); ++i) { // s[i]的数量为1,表示只有一个,也就是不重复的字符 if (m[s[i]] == 1) return i; } return -1; } }; #endif //CPP_0387__SOLUTION1_H_
ooooo-youwillsee/leetcode
1680-Concatenation of Consecutive Binary Numbers/cpp_1680/Solution1.h
<gh_stars>10-100 /** * @author ooooo * @date 2021/2/14 09:55 */ #ifndef CPP_1680__SOLUTION1_H_ #define CPP_1680__SOLUTION1_H_ #include <iostream> using namespace std; class Solution { public: static const long MOD = 1000000007; int concatenatedBinary(int n) { long ans = 0; for (int i = 1; i <= n; ++i) { int x = i, y = 0, count = 0; while (x) { y += (x % 2) << count; x /= 2; count++; } ans = (((ans << count) % MOD) + y) % MOD; } return ans; } }; #endif //CPP_1680__SOLUTION1_H_
ooooo-youwillsee/leetcode
0509-Fibonacci-Number/cpp_0509/Solution2.h
// // Created by ooooo on 2020/1/6. // #ifndef CPP_0509_SOLUTION2_H #define CPP_0509_SOLUTION2_H #include <iostream> #include <vector> using namespace std; /** * loop */ class Solution { public: int fib2(int N) { if (N == 0 || N == 1) return N; vector<int> ans = {0, 1}; for (int i = 2; i <= N; ++i) { ans.push_back(ans[i - 1] + ans[i - 2]); } return ans[N]; } int fib(int N) { if (N == 0 || N == 1) return N; int a = 0, b = 1, ans = 0; for (int i = 2; i <= N; ++i) { ans = a + b; a = b; b = ans; } return ans; } }; #endif //CPP_0509_SOLUTION2_H
ooooo-youwillsee/leetcode
0705-Design HashSet/cpp_0705/Solution1.h
<filename>0705-Design HashSet/cpp_0705/Solution1.h // // Created by ooooo on 2020/1/13. // #ifndef CPP_0705__SOLUTION1_H_ #define CPP_0705__SOLUTION1_H_ #include <iostream> using namespace std; class MyHashSet { private: struct Node { int val; Node *next; Node(int val) : val(val), next(nullptr) {} Node(int val, Node *next) : val(val), next(next) {} Node() { this->next = nullptr; this->val = -1; } }; public: Node *dummyHead; int size; /** Initialize your data structure here. */ MyHashSet() { dummyHead = new Node(); size = 0; } void add(int key) { if (!contains(key)) { dummyHead->next = new Node(key, dummyHead->next); size += 1; } } void remove(int key) { if (!contains(key)) return; Node *prev = dummyHead; while (prev->next) { if (prev->next->val == key) { break; } prev = prev->next; } if (prev) { Node *delNode = prev->next; prev->next = delNode->next; size--; delNode->next = nullptr; } } /** Returns true if this set contains the specified element */ bool contains(int key) { Node *curNode = dummyHead->next; while (curNode) { if (curNode->val == key) { return true; } curNode = curNode->next; } return false; } }; /** * Your MyHashSet object will be instantiated and called as such: * MyHashSet* obj = new MyHashSet(); * obj->add(key); * obj->remove(key); * bool param_3 = obj->contains(key); */ #endif //CPP_0705__SOLUTION1_H_
ooooo-youwillsee/leetcode
lcof_021/cpp_021/Solution1.h
<reponame>ooooo-youwillsee/leetcode // // Created by ooooo on 2020/3/15. // #ifndef CPP_021__SOLUTION1_H_ #define CPP_021__SOLUTION1_H_ #include <iostream> #include <vector> using namespace std; /** * 双指针 */ class Solution { public: vector<int> exchange(vector<int> &nums) { int l = 0, r = nums.size() - 1; while (true) { while (l < r && (nums[l] & 1) == 1) l++; while (l < r && (nums[r] & 1) == 0) r--; if (l >= r) break; swap(nums[l], nums[r]); l++; r--; } return nums; } }; #endif //CPP_021__SOLUTION1_H_
ooooo-youwillsee/leetcode
0094-Binary-Tree-Inorder-Traversal/cpp_0094/TreeNode.h
<gh_stars>10-100 // // Created by ooooo on 2020/2/15. // #ifndef CPP_0094__TREENODE_H_ #define CPP_0094__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(vector<int> nums) { if (nums.empty()) return; this->val = nums[0]; this->left = this->right = nullptr; queue<TreeNode *> q; q.push(this); for (int i = 1; i < nums.size() && !q.empty();) { TreeNode *node = q.front(); q.pop(); if (nums[i] == INT_MAX) { node->left = nullptr; } else { node->left = new TreeNode(nums[i]); q.push(node->left); } if (i + 1 >= nums.size()) return; if (nums[i + 1] == INT_MAX) { node->right = nullptr; } else { node->right = new TreeNode(nums[i + 1]); q.push(node->right); } i += 2; } } }; #endif //CPP_0094__TREENODE_H_
ooooo-youwillsee/leetcode
0113-Path-Sum-II/cpp_0113/Solution1.h
<filename>0113-Path-Sum-II/cpp_0113/Solution1.h /** * @author ooooo * @date 2020/9/26 00:13 */ #ifndef CPP_0113__SOLUTION1_H_ #define CPP_0113__SOLUTION1_H_ #include "TreeNode.h" class Solution { public: void dfs(TreeNode *node, int sum, vector<int> &temp) { if (!node) return; temp.emplace_back(node->val); if (sum == node->val && !node->left && !node->right) { ans.push_back(temp); } sum -= node->val; dfs(node->left, sum, temp); dfs(node->right, sum, temp); temp.pop_back(); } vector<vector<int>> ans; vector<vector<int>> pathSum(TreeNode *root, int sum) { vector<int> temp; dfs(root, sum, temp); return ans; } }; #endif //CPP_0113__SOLUTION1_H_
ooooo-youwillsee/leetcode
0234-Palindrome-Linked-List/cpp_0234/Solution3.h
<filename>0234-Palindrome-Linked-List/cpp_0234/Solution3.h // // Created by ooooo on 2020/1/8. // #ifndef CPP_0234_SOLUTION3_H #define CPP_0234_SOLUTION3_H #include "ListNode.h" class Solution { public: bool isPalindrome(ListNode *head) { if (!head || !head->next) return true; ListNode *low = head, *fast = head, *prev = nullptr, *temp = nullptr; while (fast && fast->next) { temp = low; low = low->next; fast = fast->next->next; temp->next = prev; prev = temp; } if (fast && !fast->next) { low = low->next; } while (prev && low) { if (prev->val != low->val) return false; prev = prev->next; low = low->next; } return true; } }; #endif //CPP_0234_SOLUTION3_H
ooooo-youwillsee/leetcode
1704-Determine if String Halves Are Alike/cpp_1704/Solution1.h
<filename>1704-Determine if String Halves Are Alike/cpp_1704/Solution1.h /** * @author ooooo * @date 2021/1/24 17:14 */ #ifndef CPP_1704__SOLUTION1_H_ #define CPP_1704__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <numeric> using namespace std; class Solution { public: bool halvesAreAlike(string s) { int n = s.size(); unordered_set<char> set = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; int cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; ++i) { if (i < n / 2) { if (set.count(s[i])) { cnt1++; } } else { if (set.count(s[i])) { cnt2++; } } } return cnt1 == cnt2; } }; #endif //CPP_1704__SOLUTION1_H_
ooooo-youwillsee/leetcode
0692-Top K Frequent Words/cpp_0692/Solution1.h
/** * @author ooooo * @date 2021/5/20 07:31 */ #ifndef CPP_0692__SOLUTION1_H_ #define CPP_0692__SOLUTION1_H_ #include <iostream> #include <vector> #include <unordered_map> #include <queue> using namespace std; class Solution { public: vector<string> topKFrequent(vector<string> &words, int k) { unordered_map<string, int> m; for (auto &word: words) { m[word]++; } auto fn = [&](const string &s1, const string &s2) constexpr { if (m[s1] == m[s2]) { return s1 < s2; } else { return m[s1] < m[s2]; } }; priority_queue<string, vector<string>, decltype(fn)> pq(fn); for (auto &[kk, v]: m) { pq.push(kk); } vector<string> ans; while (k) { auto x = pq.top(); pq.pop(); ans.push_back(x); } return ans; } }; #endif //CPP_0692__SOLUTION1_H_