![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
everlasting_lxy
这个作者很懒,什么都没留下…
展开
-
leetcode131. 分割回文串
1、题目https://leetcode-cn.com/problems/palindrome-partitioning/submissions/2、题意题解1:爆搜+特判class Solution {public: vector<vector<string>> res; vector<string> t; void dfs(string &s,int cnt) { if(cnt==s.size())原创 2020-11-30 21:19:09 · 189 阅读 · 0 评论 -
leetcode283移动零
1、题目https://leetcode-cn.com/problems/peeking-iterator/2、题意题解1:将非0的数放在数组前面 后面的数全部置于0即可class Solution {public: void moveZeroes(vector<int>& nums) { int j = 0; for(auto &x:nums) if(x) nums[j+原创 2020-11-19 23:18:10 · 166 阅读 · 0 评论 -
leetcode108. 将有序数组转换为二叉搜索树
1、题目https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/submissions/2、题意题解1:dfs/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x)原创 2020-09-24 18:52:42 · 55 阅读 · 0 评论 -
leetcode684. 冗余连接
1、题目https://leetcode-cn.com/problems/redundant-connection/submissions/2、题意题解1:并查集当前两个节点为一个集合时说明有环 返回这两个结果,否则合并p[a] = b;class Solution {public: vector<int> p; int find(int x) { return x==p[x]?x:p[x] = find(p[x]); } ve原创 2020-09-23 20:32:55 · 57 阅读 · 0 评论 -
leetcode990. 等式方程的可满足性
1、题目https://leetcode-cn.com/problems/satisfiability-of-equality-equations/2、题意题解1:并查集将a-z当做0-25表示;将相等的合并为一个集合;将不相等的判断时否为一个集合 即find(a)==find(b);相等则等式不成立class Solution {public: vector<int> p; int find(int x) { return x==p[x]原创 2020-09-23 20:01:58 · 79 阅读 · 0 评论 -
leetcode547. 朋友圈
1、题目https://leetcode-cn.com/problems/friend-circles/2、题意题解1:dfs当前这个同学不在一个朋友圈内的话,将这一行与他为朋友的和与他朋友的朋友标记为一个朋友圈;res++;返回res即可。class Solution {public: vector<bool> st; int findCircleNum(vector<vector<int>>& M) { int m原创 2020-09-23 19:42:38 · 302 阅读 · 0 评论 -
leetcode974. 和可被 K 整除的子数组
1、题目https://leetcode-cn.com/problems/subarray-sums-divisible-by-k/2、题意题解1:前缀和+哈希表和leetcode560类似class Solution {public: int subarraysDivByK(vector<int>& A, int k) { unordered_map<int, int> m; m[0] = 1; long原创 2020-09-22 18:54:38 · 66 阅读 · 0 评论 -
leetcode560. 和为K的子数组
1、题目https://leetcode-cn.com/problems/subarray-sum-equals-k/solution/dai-ni-da-tong-qian-zhui-he-cong-zui-ben-fang-fa-y/2、题意题解1:前缀和+哈希表class Solution {public: int subarraySum(vector<int>& nums, int k) { unordered_map<int,int>原创 2020-09-22 18:27:15 · 81 阅读 · 0 评论 -
leetcode637. 二叉树的层平均值
1、题目https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/submissions/2、题意题解1:bfs/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x),原创 2020-09-12 20:19:04 · 75 阅读 · 0 评论 -
leetcode136. 只出现一次的数字
1、题目https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/2、题意题解1:异或所有数字最后剩下的就是结果class Solution {public: int singleNumber(vector<int>& nums) { int res=0; for(auto x : nums) res^=x; return res; }};...原创 2020-09-11 00:28:44 · 68 阅读 · 0 评论 -
leetcode216. 组合总和 III
1、题目https://leetcode-cn.com/problems/combination-sum-iii/2、题意题解1:递归回溯和组合总和12类似class Solution {public: vector<vector<int>> res; vector<int> path; void dfs(int start,int k,int tar) { if(k==0&&tar==0)原创 2020-09-11 00:20:56 · 86 阅读 · 0 评论 -
leetcode129. 求根到叶子节点数字之和
1、题目https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/2、题意题解1:递归每次到底层更新结果/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL),原创 2020-09-10 15:38:34 · 80 阅读 · 0 评论 -
leetcode128. 最长连续序列
1、题目https://leetcode-cn.com/problems/longest-consecutive-sequence/2、题意题解1:hash表当当前该数前面没有元素时 判断从这个数开始往后连续的最大个数 每一次删除在set中的这一段连续的数class Solution {public: int longestConsecutive(vector<int>& nums) { int res = 0; unordered_s原创 2020-09-10 15:25:46 · 90 阅读 · 0 评论 -
leetcode124. 二叉树中的最大路径和
1、题目https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/submissions/2、题意题解1:递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(原创 2020-09-10 15:04:51 · 53 阅读 · 0 评论 -
leetcode123. 买卖股票的最佳时机 III
1、题目https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/2、题意题解1:线性扫描(On)维护f[i]数组表示购买一次所能组成的最大值;res初始化为f[n-1](表示只买一次的最大值);从后往前遍历 维护一个从后往前的最大值maxv 当当前数值<maxv更新res = res = max(res,maxv-prices[i]+f[i-1]);(表示只买卖一次且是第天买入的最大值);/* */cla原创 2020-09-09 00:13:35 · 86 阅读 · 0 评论 -
leetcode257. 二叉树的所有路径
1、题目https://leetcode-cn.com/problems/binary-tree-paths/2、题意题解1:dfs/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}原创 2020-09-04 10:26:40 · 56 阅读 · 0 评论 -
leetcode841. 钥匙和房间
1、题目https://leetcode-cn.com/problems/keys-and-rooms/2、题意题解1:dfsclass Solution {public: void dfs(vector<vector<int>>& rooms,int house,vector<bool>&st) { st[house] = true; for(int j=0;j<rooms[house原创 2020-08-31 07:32:14 · 158 阅读 · 0 评论 -
leetcode459. 重复的子字符串
1、题目https://leetcode-cn.com/problems/repeated-substring-pattern/2、题意题解1:枚举class Solution {public: bool repeatedSubstringPattern(string s) { int n = s.size(),j; s = " "+s; for(int i=1;i<=n/2;i++) { if(原创 2020-08-24 05:58:19 · 175 阅读 · 1 评论 -
leetcode122. 买卖股票的最佳时机 II
1、题目https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/2、题意题解1:贪心class Solution {public: int maxProfit(vector<int>& prices) { int res = 0; for(int i=1;i<prices.size();i++) res+=max(0,prices原创 2020-08-23 06:53:23 · 79 阅读 · 0 评论 -
leetcode201. 数字范围按位与
1、题目https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/2、题意题解1:位运算二进制从最高位开始 相等并且为1 将当前这位加到结果,不相等说明从这一位开始往后无法增加结果 break;class Solution {public: int rangeBitwiseAnd(int m, int n) { int res = 0; for (int i = 30; i >= 0;原创 2020-08-23 06:18:25 · 68 阅读 · 0 评论 -
leetcode679. 24 点游戏
1、题目https://leetcode-cn.com/problems/24-game/submissions/2、题意题解1:dfs+回溯除法有可能为小数每次选两个数nums[i]和nums[j],枚举所有情况(除法的时候 要保证被除数不为0),遍历所有情况(删除nums[i]和nums[j]并加上一个这两个数能构成的一种情况);当nums.size()=1时判断nums[0]-24的绝对值是否<1e-5 是的话则有解;class Solution {public: boo原创 2020-08-22 17:34:37 · 104 阅读 · 0 评论 -
leetcode529. 扫雷游戏
1、题目https://leetcode-cn.com/problems/minesweeper/2、题意题解1:dfs当前这个格子’E’若为’B’说明周围8个格子没有雷,继续搜索它其它八个格子为’E’的格子 (若为数字则说明有雷 则不用扩散)class Solution {public: vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<原创 2020-08-20 02:40:32 · 126 阅读 · 0 评论 -
leetcode121. 买卖股票的最佳时机
1、题目https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/2、题意题解1:维护最小买入的价格 更新结果的最大值class Solution {public: int maxProfit(vector<int>& prices) { int res = 0,start = INT_MAX; for(int i =0;i<prices.size();i++)原创 2020-08-17 17:35:31 · 59 阅读 · 0 评论 -
leetcode120. 三角形最小路径和
1、题目https://leetcode-cn.com/problems/triangle/2、题意题解1:class Solution {public: int minimumTotal(vector<vector<int>>& f) { for (int i = f.size() - 2; i >= 0; i -- ) for (int j = 0; j <= i; j ++ )原创 2020-08-17 17:26:13 · 47 阅读 · 0 评论 -
leetcode119. 杨辉三角 II
1、题目https://leetcode-cn.com/problems/pascals-triangle-ii/submissions/2、题意题解1:class Solution {public: vector<int> getRow(int rowIndex) { vector<vector<int>> f; for (int i = 0; i <=rowIndex; i ++ ) {原创 2020-08-17 17:23:47 · 66 阅读 · 0 评论 -
leetcode118. 杨辉三角
1、题目https://leetcode-cn.com/problems/pascals-triangle/2、题意题解1:class Solution {public: vector<vector<int>> generate(int n) { vector<vector<int>> f; for (int i = 0; i < n; i ++ ) { vector<int原创 2020-08-17 17:19:03 · 59 阅读 · 0 评论 -
leetcode98. 验证二叉搜索树
1、题目https://leetcode-cn.com/problems/validate-binary-search-tree/submissions/2、题意题解1:中序遍历为升序若当前节点为>min并且<max则符合情况若递归左子树说明左边树要val则将max更新为root->val;右子树>root->val min更新为root->val/** * Definition for a binary tree node. * struct Tree原创 2020-08-17 17:08:28 · 63 阅读 · 0 评论 -
leetcode96. 不同的二叉搜索树
1、题目https://leetcode-cn.com/problems/unique-binary-search-trees/2、题意题解1:f[n]表示以n为跟节点二叉搜索树的个数左子树有0-(n-1)个节点 右子树有(n-1)-0个节点f[n]为所有的总合class Solution {public: int numTrees(int n) { vector<int> f(n + 1); f[0] = 1; for原创 2020-08-17 16:49:49 · 55 阅读 · 0 评论 -
leetcode97. 交错字符串
1、题目https://leetcode-cn.com/problems/interleaving-string/2、题意题解1:dp做法和10正则表达式匹配 72. 编辑距离类似f[i][j]表示s1前i个字符和s2前j个字符是否可以交错形成s3前i+j个字符1:s1[i] == s3[i + j] f[i][j] = f[i - 1][j];2:s2[j] == s3[i + j] f[i][j] = f[i][j-1];两种情况一种为true则f[i][j] = true;c原创 2020-08-13 15:54:16 · 74 阅读 · 0 评论 -
leetcode101. 对称二叉树
1、题目https://leetcode-cn.com/problems/symmetric-tree/submissions/2、题意题解1:递归1:判断第一颗子树的左子树和第二颗子树的右子树是否相等;2:判断第一颗子树的右子树和第二颗子树的左子数是否相等;/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *r原创 2020-08-12 18:13:13 · 77 阅读 · 0 评论 -
leetcode100. 相同的树
1、题目https://leetcode-cn.com/problems/same-tree/2、题意题解1:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * Tre原创 2020-08-12 18:01:47 · 130 阅读 · 0 评论 -
leetcode107. 二叉树的层次遍历 II
1、题目https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/submissions/2、题意题解1:bfs和leetcode102一样 最后将res翻转/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *原创 2020-08-12 17:59:54 · 64 阅读 · 0 评论 -
leetcode103. 二叉树的锯齿形层次遍历
1、题目https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/submissions/2、题意题解1:bfs和leetcode102一样 只需要多加一个cnt判断当前是否在偶数层即可,在将当前着一层的数组翻转/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left原创 2020-08-12 17:56:02 · 129 阅读 · 1 评论 -
leetcode102. 二叉树的层序遍历
1、题目https://leetcode-cn.com/problems/binary-tree-level-order-traversal/2、题意题解1:bfsclass Solution {public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; queue<TreeNode*> q;原创 2020-08-12 17:46:51 · 65 阅读 · 0 评论 -
leetcode106. 从中序与后序遍历序列构造二叉树
1、题目https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/2、题意题解1:做法和leetcode105类似 开个哈希表记录中序便利值的索引后序的最后一个节点为根 找到在中序中的位置…/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod原创 2020-08-12 17:43:02 · 79 阅读 · 0 评论 -
leetcode105. 从前序与中序遍历序列构造二叉树
1、题目https://editor.csdn.net/md/?not_checkout=12、题意题解1:哈希表记录中序值的索引每次先序的第一个点为根 根据中序遍历找到跟节点的位置k左子树在中序遍历中为il,k-1 在前序遍历中为pl+1,pl+1+k-1-il,il;右子树在中序遍历中为k+1,ir 在前序遍历中为pl+1+k-1-il+1,pr;/** * Definition for a binary tree node. * struct TreeNode { * in原创 2020-08-12 17:20:06 · 63 阅读 · 0 评论 -
leetcode113. 路径总和 II
1、题目https://leetcode-cn.com/problems/path-sum-ii/submissions/2、题意题解1:dfs回溯 和上一题leetcode112类似class Solution {public: vector<vector<int>> res; vector<int> path; void dfs(TreeNode* root, int sum) { path.push_bac原创 2020-08-12 15:27:07 · 82 阅读 · 0 评论 -
leetcode112. 路径总和
1、题目https://leetcode-cn.com/problems/path-sum/2、题意题解1:递归 用sum-当前节点当当前节点左右子数都为空 若sum-当前节点为0说明有解class Solution {public: bool hasPathSum(TreeNode *root, int sum) { if(!root) return false; if(!root->left&!root->right) return原创 2020-08-12 14:52:10 · 114 阅读 · 1 评论 -
leetcode111. 二叉树的最小深度
1、题目https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/2、题意题解1:递归class Solution {public: int minDepth(TreeNode* root) { if(!root) return 0; int left = minDepth(root->left); int right = minDepth(root->right)原创 2020-08-12 14:36:36 · 60 阅读 · 0 评论 -
leetcode696. 计数二进制子串
1、题目https://leetcode-cn.com/problems/count-binary-substrings/submissions/2、题意题解1:dpdp[i]表示当前位置(包括自身)前面连续为’1’或’0’的个数连续为‘1’个数为正,连续为‘0’个数为负当前dp[i]数字等于总个数则 continue否则当前abs(dp[i])<=abs(dp[i-abs(dp[i])]);res++;class Solution {public: int countBin原创 2020-08-10 14:51:17 · 102 阅读 · 0 评论