![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode刷题
WMXNLFD
努力成瘾的人
个人博客:https://www.cnblogs.com/wmxnlfd/
展开
-
LeetCode经典基础算法题1
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int>hash; for(int i = 0; i < nums.size(); i++) { int t = target - nums[i]; if(hash.count(.原创 2020-09-16 16:16:46 · 187 阅读 · 0 评论 -
Leetcode总结 栈专题
class Solution {public: bool isValid(string s) { stack<char> stk; for(auto c : s) { if(c == ')') { if(stk.empty() || stk.top...原创 2020-09-03 14:32:21 · 131 阅读 · 0 评论 -
【QZMS】4 BFS DFS Depth 树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *//** * Definition for singly-linked list. * struct ListNod.原创 2020-07-29 23:40:45 · 157 阅读 · 0 评论 -
【QZMS】3 BST LCA depth树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int>>.原创 2020-07-28 23:39:51 · 117 阅读 · 0 评论 -
【QZMS】2 LCA BiNode 树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* lowestCommonAncestor.原创 2020-07-27 23:33:44 · 146 阅读 · 0 评论 -
【QZMS】LC第31场双周赛
class Solution {public: int count(int x) { if(x < 0) { return 0; } return (x + 1) / 2; } int countOdds(int low, int high) { return count(high) - count(low - 1); }};class Solutio.原创 2020-07-26 23:45:20 · 141 阅读 · 0 评论 -
LeetCode Weekly contest 190
字符串class Solution {public: int isPrefixOfWord(string sentence, string searchWord) { sentence += " "; string cur = "";// 分割字符串 int idx = 1, m = searchWord.size(); for(auto c : sentence){ if(c == ' '){ .原创 2020-05-24 14:33:53 · 151 阅读 · 0 评论 -
Leetcode总结 链表专题
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* a...原创 2020-04-05 21:32:42 · 116 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第52期 576-600
class Solution {public: vector<vector<vector<int>>>f; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; int mod = 1e9 + 7; int findPaths(int m, int n, int N, int i...原创 2020-03-21 21:52:04 · 91 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第51期 557-575
class Solution {public: string reverseWords(string s) { int front = 0, back = 0; for(int i = 0; i < s.size(); i ++) { if(s[i] != ' ') back ++; ...原创 2020-03-19 15:33:48 · 105 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第50期 538-556
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...原创 2020-03-14 21:57:28 · 95 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第49期 521-537
class Solution {public: int findLUSlength(string a, string b) { if(a == b) return -1; return max(a.size(), b.size()); }};class Solution {public: int fin...原创 2020-03-10 21:57:43 · 105 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第48期 516-520
class Solution {public: int longestPalindromeSubseq(string s) { int n = s.size(); vector<vector<int>>dp(n, vector<int>(n, 0)); for(int i = 0; i < n; ...原创 2020-03-08 23:39:34 · 99 阅读 · 0 评论 -
【ML&DL学习】8 tf keras regression customized loss layers
自定义损失函数之前使用的损失函数def customized_mse(y_true, y_pred): return tf.reduce_mean(tf.square(y_pred - y_true))model = keras.models.Sequential([ keras.layers.Dense(30, activation='relu', ...原创 2020-03-07 10:20:19 · 233 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第47期 501-515
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...原创 2020-03-02 23:12:37 · 78 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第46期 486-500
class Solution {public: bool PredictTheWinner(vector<int>& nums) { int n = nums.size(); vector<vector<int>> dp(n, vector<int>(n, 0)); for(int ...原创 2020-02-27 20:08:49 · 126 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第45期 473-485
class Solution {public: int side; int sum = 0; vector<bool>st; bool makesquare(vector<int>& nums) { if(!nums.size()) return false; for(auto x : nums) ...原创 2020-02-22 23:21:21 · 137 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第44期 466-472
class Solution {public: int getMaxRepetitions(string s1, int n1, string s2, int n2) { int idx2 = 0, len2 = s2.size(), cnt = 0; int i = 1; vector<int>v; v.pu...原创 2020-02-20 00:36:27 · 87 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第43期 451-464
class Solution {public: string frequencySort(string s) { unordered_map<char, int>map; for(auto &c : s) map[c] ++; vector<pair<char, int>>...原创 2020-02-17 19:40:36 · 195 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第42期 441-450
class Solution {public: int arrangeCoins(int n) { int res = 0; while(n > res) { res ++; n = n - res; } return res; }};c...原创 2020-02-15 20:36:05 · 149 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第41期 432-440
class Solution {public: int minMutation(string start, string end, vector<string>& bank) { if(start.empty() || end.empty() || bank.empty()) return -1; if(find(bank.beg...原创 2020-02-13 14:00:24 · 127 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第40期 414-430
class Solution {public: int thirdMax(vector<int>& nums) { set<int> hash;//set里面会自动排序(从小到大) for(auto x : nums) { hash.insert(x); if...原创 2020-02-12 11:46:25 · 96 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第39期 401-413
class Solution {public: vector<string> readBinaryWatch(int num) { vector<string>res; for(int i = 0; i < 12; i ++) for(int j = 0; j < 60; j ++) ...原创 2020-02-09 22:01:10 · 82 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第38期 391-400
class Solution {public: bool isRectangleCover(vector<vector<int>>& rectangles) { int x1 = INT_MAX; int y1 = INT_MAX; int x2 = INT_MIN; int y2 = IN...原创 2020-02-07 21:37:38 · 104 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第37期 381-390
class RandomizedCollection {public: /** Initialize your data structure here. */ unordered_map<int, unordered_set<int>>hash; vector<int>nums; RandomizedCollection() ...原创 2020-02-06 21:50:35 · 109 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第36期 371-380
class Solution {public: int getSum(int a, int b) { if(!b) return a; int sum = a ^ b, carry = uint(a & b) << 1; return getSum(sum, carry); }};class Solut...原创 2020-02-05 21:08:59 · 91 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第35期 351-370
class SummaryRanges {public: /** Initialize your data structure here. */ map<int, int>m; SummaryRanges() { } void addNum(int val) { if(m.count(val)) r...原创 2020-02-04 21:14:40 · 128 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第34期 341-350
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Return ...原创 2020-02-03 21:23:44 · 79 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第33期 331-340
class Solution {public: bool res = true; bool isValidSerialization(string preorder) { preorder += ','; int u = 0; dfs(preorder, u); return res && u ==...原创 2020-02-02 20:16:39 · 86 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第32期 321-330
class Solution {public: vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) { int n = nums1.size(), m = nums2.size(); vector<int>...原创 2020-02-01 21:07:10 · 85 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第31期 311-320
class Solution {public: vector<vector<int>>f; vector<int>nums; int maxCoins(vector<int>& _nums) { int n = _nums.size(); f = vector<vector&l...原创 2020-01-31 23:25:48 · 74 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第30期 301-310
class Solution {public: set<string> res; vector<string> removeInvalidParentheses(string s) { int left = 0, right = 0; for(auto c : s) if(c == '(') ...原创 2020-01-30 21:00:02 · 89 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第29期 291-300
class Solution {public: bool canWinNim(int n) { return n & 3; //n % 4 等价于 n & 3 }};class MedianFinder {public: priority_queue<int> smaller;//小根堆存储大于等于当前中位数 ...原创 2020-01-29 19:20:58 · 90 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第28期 281-290
class Solution {public: vector<string> res; vector<string> addOperators(string num, int target) { dfs(num, "", 0, 0, 0, target); return res; } void dfs(s...原创 2020-01-28 19:35:33 · 156 阅读 · 1 评论 -
从零开始的刷LeetCode生活 第27期 271-280
class Solution {public: int hundred = 100, thousand = 1000, million = 1000000, billion = 1000000000; unordered_map<int, string>numbers; string numberToWords(int num) { char...原创 2020-01-27 20:09:14 · 91 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第26期 261-270
class Solution {public: bool isUgly(int num) { if(num <= 0) return false; while(num % 5 == 0) num /= 5; while(num % 3 == 0) num /= 3; w...原创 2020-01-26 19:56:55 · 137 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第25期 257-260
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...原创 2020-01-25 14:37:32 · 92 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第24期 241-250
class Solution {public: vector<int> diffWaysToCompute(string input) { vector<int>res; for(int i = 0; i < input.size(); i ++) { char c = input[i...原创 2020-01-24 13:48:12 · 108 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第23期 231-240
class Solution {public: bool isPowerOfTwo(int n) { return n > 0 && (n & -n) == n; }};class MyQueue {public: stack<int> stk, tmp; /** Initialize your...原创 2020-01-23 19:30:15 · 144 阅读 · 0 评论 -
从零开始的刷LeetCode生活 第22期 221-230
class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { if(matrix.empty()) return 0; int n = matrix.size(), m = matrix[0].size(); vector...原创 2020-01-22 22:06:50 · 80 阅读 · 0 评论