05 2023 档案

摘要:```c class Solution { public: vector res; unordered_map hashmap;//记录每一个子树出现的次数 string dfs(TreeNode* root) { if(!root) return ""; string str=""; str+=t 阅读全文
posted @ 2023-05-28 14:47 穿过雾的阴霾 阅读(9) 评论(0) 推荐(0) 编辑
摘要:# 思路1 ``` class Solution { public: void flatten(TreeNode* root) { while(root) { auto p=root->left; if(p)//找到左儿子的右链 { while(p->right) p=p->right; //将右链 阅读全文
posted @ 2023-05-27 10:30 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector dfs(TreeNode* root)//依次返回是否是二叉搜索树,最大值最小值 { vector res{1,root->val,root->val}; if(root->left) { auto l=dfs(root->le 阅读全文
posted @ 2023-05-24 14:39 穿过雾的阴霾 阅读(5) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: int countNodes(TreeNode* root) { if(!root) return 0; auto l=root->left,r=root->right; int x=1,y=1;//记录左右两边层数 while(l) l=l 阅读全文
posted @ 2023-05-24 14:15 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector dfs(int l,int r)//返回以n为根节点的所有可能子树 { vector res; if(l>r) return {NULL}; for(int k=l;k left=dfs(l,k-1); vector right 阅读全文
posted @ 2023-05-23 10:50 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector> res; void bfs(TreeNode* root) { queue q; q.push(root); int cnt=0; while(!q.empty()) { vector level; int len=q.siz 阅读全文
posted @ 2023-05-22 13:24 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector> res; void bfs(TreeNode* root) { queue q; q.push(root); int last=0; while(!q.empty()) { vector level; int len=q.si 阅读全文
posted @ 2023-05-22 13:15 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:``` /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), 阅读全文
posted @ 2023-05-21 15:23 穿过雾的阴霾 阅读(5) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector inorderTraversal(TreeNode* root) { vector res; stack st; while(root||st.size()) { while(root) { st.push(root); roo 阅读全文
posted @ 2023-05-21 15:05 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector multiply(const vector& nums) { int n=nums.size(); if(n==0) return vector(); vector q(n,1); for (int i = 1,t=nums[0 阅读全文
posted @ 2023-05-20 15:48 穿过雾的阴霾 阅读(5) 评论(0) 推荐(0) 编辑
摘要:``` class Solution { public: vector t; void dfs(TreeNode* root,TreeNode* p,vector &q) { if(!root) return; t.push_back(root); if(root==p) { q=t; t.pop_ 阅读全文
posted @ 2023-05-19 15:16 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:模拟计算机的加法器实现 * x存放不进位的加法结果,y存放进位。不进位的结果加上进位就是答案,换句话就是x+y * 不进位加法结果可以通过异或实现,两数相加的进位可以通过逻辑与,再左移一位实现 * 计算x+y,又是重复上面的步骤,循环即可,直到进位为0,循环结束 ``` class Solution 阅读全文
posted @ 2023-05-19 14:40 穿过雾的阴霾 阅读(16) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: bool check(char c) { if(c=='+') return true; if(c=='-') return true; if(c>='0'&&c<='9') return true; return false; } int strT 阅读全文
posted @ 2023-05-18 20:57 穿过雾的阴霾 阅读(15) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int getSum(int n) { int res=n; //if(n>0) res+=getSum(n-1); (res>0)&&(res+=getSum(n-1)); return res; } }; 阅读全文
posted @ 2023-05-18 20:35 穿过雾的阴霾 阅读(14) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int q[4010]; bool notexist[4010]; void move(int &j,int n)//后移 { do { j++; j=j%n; } while(notexist[j]!=false); } int lastRemai 阅读全文
posted @ 2023-05-18 20:06 穿过雾的阴霾 阅读(12) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: string longestPalindrome(string s) { string res; int n=s.size(); for(int i=0;i<n;i++) { //长度是奇数 int l=i-1,r=i+1; while(l>=0&& 阅读全文
posted @ 2023-05-16 20:25 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:可以直接分类讨论,分别枚举第一个房屋偷或不偷的情况,最后再取极值 不偷第一家 f[i] 代表前 i 个房屋,偷第 i 个, 且不偷第一家的最大值 g[i] 代表前 i 个房屋,不偷第 i 个, 且不偷第一家的最大值 偷第一家 f[i] 代表前 i 个房屋,偷第 i 个, 且偷第一家的最大值 g[i] 阅读全文
posted @ 2023-05-15 14:00 穿过雾的阴霾 阅读(19) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: bool isContinuous( vector<int> q ) { if(q.empty()) return false; sort(q.begin(),q.end()); int zero=0,n=q.size(); for (int i = 阅读全文
posted @ 2023-05-15 13:07 穿过雾的阴霾 阅读(26) 评论(0) 推荐(0) 编辑
摘要:```c class Solution { public: vector<int> res; vector<int> numberOfDice(int n) { vector<vector<int> >f(n+1,vector<int>(6*n+1));//f[i][j]表示选了i个数,和为j的所有 阅读全文
posted @ 2023-05-14 15:05 穿过雾的阴霾 阅读(38) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int maxProfit(vector<int>& prices) { int n=prices.size(); vector<int> f(n+1),g(n+1); //预处理f,f[i]表示前i天交易的最大值 for(int i=1,min_p 阅读全文
posted @ 2023-05-14 15:04 穿过雾的阴霾 阅读(14) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int maxDiff(vector<int>& nums) { if(nums.size()==0) return 0; int buy=nums[0],res=0; for (int i = 1; i < nums.size(); i ++ ) 阅读全文
posted @ 2023-05-13 14:08 穿过雾的阴霾 阅读(9) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: vector<int> res; deque<int> q; vector<int> maxInWindows(vector<int>& nums, int k) { for (int i = 0; i < nums.size(); i ++ ) { 阅读全文
posted @ 2023-05-13 14:07 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:思路 暴力就是枚举终点 i,找出里 i 最近的起点 j,再去更新答案,可以发现起点随终点单调往后,因此可以滑动窗口优化 如何快速判断当前窗口是否包含子串所有字符 哈希表 word 存储子串所有字符出现的次数,window 存储当前窗口所有字符出现的次数 变量 cnt 记录当前窗口里,有效字符的个数 阅读全文
posted @ 2023-05-11 14:58 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: string leftRotateString(string str, int n) { if(str.size()==0) return ""; string t=str.substr(0,n); str=str.substr(n); str=st 阅读全文
posted @ 2023-05-11 13:48 穿过雾的阴霾 阅读(10) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: string reverseWords(string s) { if(s.size()==0) return ""; reverse(s.begin(),s.end()); for (int i = 0; i < s.size();) { int j 阅读全文
posted @ 2023-05-11 13:46 穿过雾的阴霾 阅读(10) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: bool isScramble(string s1, string s2) { int n=s1.size(); vector<vector<vector<bool> > >f(n,vector<vector<bool>>(n,vector<bool 阅读全文
posted @ 2023-05-10 16:47 穿过雾的阴霾 阅读(6) 评论(0) 推荐(0) 编辑
摘要:暴力 枚举区间起点 i,对于每一个 i,找到最大的 j ,满足 ij 区间和<target class Solution { public: vector<vector<int> > findContinuousSequence(int sum) { vector<vector<int> > res 阅读全文
posted @ 2023-05-10 14:59 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: vector<int> findNumbersWithSum(vector<int>& nums, int target) { unordered_set<int> hashtable; for(auto i:nums) { if(hashtable 阅读全文
posted @ 2023-05-10 14:12 穿过雾的阴霾 阅读(4) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int maxProfit(vector<int>& prices) { int res=0; for(int i=0;i<prices.size()-1;i++) res+=max(0,prices[i+1]-prices[i]); return 阅读全文
posted @ 2023-05-09 15:54 穿过雾的阴霾 阅读(12) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int maxProfit(vector<int>& prices) { int buy=prices[0],n=prices.size(),res=0;//记录最小值 for(int i=1;i<n;i++)//枚举第几天卖出 { res=max( 阅读全文
posted @ 2023-05-09 15:25 穿过雾的阴霾 阅读(11) 评论(0) 推荐(0) 编辑
摘要:思路 如果一个数字出现 3 次,那么它的二进制表示的每一位也出现三次,如果把所有出现三次的数字的二进制表示的每一位都分别加起来,那么每一位的和都能被 3 整除 cnt[32] 数组存储每一位 1 出现的次数 遍历数组中所有数,将其二进制表示记录在 cnt 数组里 遍历 cnt 数组,根据 cnt[i 阅读全文
posted @ 2023-05-09 14:45 穿过雾的阴霾 阅读(17) 评论(0) 推荐(0) 编辑
摘要:思路 设两个数字分别为 x,y 将所有数字异或起来,得到的结果设为 s,s=x^y 因为相同两个数字,异或结果为 0,由于异或运算满足交换律,因此最后就剩两个数字异或 从 s 的二进制表示中,找到任意为 1 的位 k xy 的二进制表示在第 k 位上,一个是 0,一个是 1 因为 xy 不同,因此 阅读全文
posted @ 2023-05-08 20:10 穿过雾的阴霾 阅读(25) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int f[110],g[110];//分别表示第i个房屋偷,不偷的最大价值 int rob(vector<int>& nums) { int n=nums.size(); for(int i=1;i<=n;i++) { g[i]=max(f[i-1 阅读全文
posted @ 2023-05-08 20:03 穿过雾的阴霾 阅读(14) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int f[1010][1010];//f[i][j]表示s[i~j]之间的最长序列 int INF=0x3f3f3f3f; int longestPalindromeSubseq(string s) { int n=s.size(); s=' '+ 阅读全文
posted @ 2023-05-07 20:02 穿过雾的阴霾 阅读(7) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: bool res=true; int dfs(TreeNode* root)//返回以root为根节点的子树深度 { if(root==NULL) return 0; int l=dfs(root->left),r=dfs(root->right); 阅读全文
posted @ 2023-05-07 17:01 穿过雾的阴霾 阅读(3) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int treeDepth(TreeNode* root) { if(!root) return 0; return max(treeDepth(root->left),treeDepth(root->right))+1; } }; 阅读全文
posted @ 2023-05-07 17:01 穿过雾的阴霾 阅读(17) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: TreeNode* res=NULL; void mid(TreeNode* root, int k,int &cnt) { if(!root) return; mid(root->left,k,cnt); cnt++; if(cnt==k) res 阅读全文
posted @ 2023-05-07 17:00 穿过雾的阴霾 阅读(11) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int getNumberSameAsIndex(vector<int>& nums) { int n=nums.size(); int l=0,r=n-1; while(l<r) { int mid=l+r>>1; if(nums[mid]<mid 阅读全文
posted @ 2023-05-06 18:53 穿过雾的阴霾 阅读(13) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int f[1010][1010];//f[i][j]表示s1的下标i结尾,s2下标j结尾的最长公共子数组长度 int findLength(vector<int>& nums1, vector<int>& nums2) { int n=nums1. 阅读全文
posted @ 2023-05-06 17:12 穿过雾的阴霾 阅读(11) 评论(0) 推荐(0) 编辑
摘要:思路 任何时刻,某个石头的重量永远都是若干石头加减运算的绝对值 如 a-b+c 合并石头都是减法,但仍可能出现+运算符,如 a-(b-c)=a-b+c 任何一种合并方法,最后一个石头的重量都可以表示成一种代数形式,如 a+b-c+d+e+f-g 不是所有的代数形式都可以转换为一种合并方法,如 a+b 阅读全文
posted @ 2023-05-05 14:04 穿过雾的阴霾 阅读(14) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int getMissingNumber(vector<int>& nums) { if(!nums.size()) return 0; int l=0,r=nums.size()-1; while(l<r)//找到第一个满足nums[i]=i+1的 阅读全文
posted @ 2023-05-05 11:27 穿过雾的阴霾 阅读(23) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示