随笔分类 - 《剑指offer》
摘要:``` 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
阅读全文
摘要:``` 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_
阅读全文
摘要:模拟计算机的加法器实现 * x存放不进位的加法结果,y存放进位。不进位的结果加上进位就是答案,换句话就是x+y * 不进位加法结果可以通过异或实现,两数相加的进位可以通过逻辑与,再左移一位实现 * 计算x+y,又是重复上面的步骤,循环即可,直到进位为0,循环结束 ``` class Solution
阅读全文
摘要: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
阅读全文
摘要: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; } };
阅读全文
摘要: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
阅读全文
摘要: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 =
阅读全文
摘要:```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的所有
阅读全文
摘要: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 ++ )
阅读全文
摘要: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 ++ ) {
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:暴力 枚举区间起点 i,对于每一个 i,找到最大的 j ,满足 ij 区间和<target class Solution { public: vector<vector<int> > findContinuousSequence(int sum) { vector<vector<int> > res
阅读全文
摘要:class Solution { public: vector<int> findNumbersWithSum(vector<int>& nums, int target) { unordered_set<int> hashtable; for(auto i:nums) { if(hashtable
阅读全文
摘要:思路 如果一个数字出现 3 次,那么它的二进制表示的每一位也出现三次,如果把所有出现三次的数字的二进制表示的每一位都分别加起来,那么每一位的和都能被 3 整除 cnt[32] 数组存储每一位 1 出现的次数 遍历数组中所有数,将其二进制表示记录在 cnt 数组里 遍历 cnt 数组,根据 cnt[i
阅读全文
摘要:思路 设两个数字分别为 x,y 将所有数字异或起来,得到的结果设为 s,s=x^y 因为相同两个数字,异或结果为 0,由于异或运算满足交换律,因此最后就剩两个数字异或 从 s 的二进制表示中,找到任意为 1 的位 k xy 的二进制表示在第 k 位上,一个是 0,一个是 1 因为 xy 不同,因此
阅读全文
摘要: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);
阅读全文
摘要:class Solution { public: int treeDepth(TreeNode* root) { if(!root) return 0; return max(treeDepth(root->left),treeDepth(root->right))+1; } };
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文