自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(29)
  • 收藏
  • 关注

原创 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 168

原创 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 189 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 80

原创 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 69

原创 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 106

原创 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 161

原创 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

原创 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

原创 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 67

原创 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 61

原创 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 64

原创 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 56

原创 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 75

原创 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 78

原创 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 131

原创 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

原创 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 139 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 66

原创 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 80

原创 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 64

原创 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 84

原创 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 118 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 65

原创 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 109

原创 leetcode94. 二叉树的中序遍历

1、题目https://leetcode-cn.com/problems/binary-tree-inorder-traversal/2、题意题解1:递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), righ

2020-08-08 21:54:39 80

原创 leetcode93. 复原IP地址

1、题目https://leetcode-cn.com/problems/restore-ip-addresses/2、题意题解1:dfs+剪枝u表示当前的位置0-s.size() k表示已经选了几个数(0-255)了0-4;判断当前数是否在0-255之间 ps:01这种不能进行选择class Solution {public: vector<string> res; vector<string> restoreIpAddresses(string s)

2020-08-08 21:38:03 90

原创 leetcode91. 解码方法

1、题目https://leetcode-cn.com/problems/decode-ways/2、题意题解1:一维dpdp[i]表示前 i 个数字共有多少种解码方式两种情况1:第i个数字在1-9之间2:第i-1个数字和第i个数字组成的数在10-26之间class Solution {public: int numDecodings(string s) { int n = s.size(); s = ' '+s; vector&lt

2020-08-06 10:58:51 50

原创 leetcode87. 扰乱字符串

1、题目https://leetcode-cn.com/problems/scramble-string/2、题意题解1:递归加剪枝class Solution {public: bool isScramble(string s1, string s2) { if (s1 == s2) return true; string ss1 = s1, ss2 = s2; sort(ss1.begin(), ss1.end()), sort(ss2.

2020-08-06 09:56:56 79

原创 leetcode89. 格雷编码

1、题目https://leetcode-cn.com/problems/gray-code/2、题意题解1:找规律n=0的时候为0n = 1;01n = 2;00101101n = 3;000100110010011111101001当n=3时前4位为n=2前4位*2,第5位为第4位+1,第6位为第3位+1…;class Solution {public: vector<int> grayCode(int n) { v

2020-08-05 07:57:13 92

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除