目录

  • LeetCode 191. 位1的个数【难度: 简单 / 知识点: 位运算】
  • 85. 不用加减乘除做加法【难度: 中 / 知识点: 思维 全加器】
  • 341. 扁平化嵌套列表迭代器【模拟】
  • 1497. 树的遍历【根据遍历的顺序 建树】
  • 456. 132 模式【未完成 单调栈】
  • 131. 直方图中最大的矩形【思维 / 单调栈】
  • 82. 删除排序链表中的重复元素 II【模拟】
  • 36. 合并两个排序的链表【归并】
  • 61. 旋转链表【模拟】
  • 33. 链表中倒数第k个节点
  • 173. 二叉搜索树迭代器【用栈模拟 输出中序遍历】
  • 19. 二叉树的下一个节点

LeetCode 191. 位1的个数【难度: 简单 / 知识点: 位运算】

class Solution {public:int hammingWeight(uint32_t n) {long long int x=n;int cnt=0;while(x){x-=x&(-x);cnt++;}return cnt;}
};

85. 不用加减乘除做加法【难度: 中 / 知识点: 思维 全加器】


class Solution {public:int add(int num1, int num2){while(num2){int sum=num1^num2;//不进位加法int c=(num1&num2)<<1;//进的值num1=sum,num2=c;}return num1;}
};

341. 扁平化嵌套列表迭代器【模拟】


就是遍历树,存一下数据。

/*** // This is the interface that allows for creating nested lists.* // You should not implement it, or speculate about its implementation* class NestedInteger {*   public:*     // Return true if this NestedInteger holds a single integer, rather than a nested list.*     bool isInteger() const;**     // Return the single integer that this NestedInteger holds, if it holds a single integer*     // The result is undefined if this NestedInteger holds a nested list*     int getInteger() const;**     // Return the nested list that this NestedInteger holds, if it holds a nested list*     // The result is undefined if this NestedInteger holds a single integer*     const vector<NestedInteger> &getList() const;* };*/class NestedIterator {public:vector<int>q;int k=0;NestedIterator(vector<NestedInteger> &nestedList) {k=0;for(auto&l: nestedList) dfs(l);}void dfs(NestedInteger& l){if(l.isInteger()) q.push_back(l.getInteger());elsefor(auto& x: l.getList()) dfs(x);}int next() {return q[k++];}bool hasNext() {return k<q.size();    }
};/*** Your NestedIterator object will be instantiated and called as such:* NestedIterator i(nestedList);* while (i.hasNext()) cout << i.next();*/

1497. 树的遍历【根据遍历的顺序 建树】

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int a[N],b[N],n;
unordered_map<int,int>l,r,pos;
int build(int l1,int r1,int l2,int r2)
{int root=a[r2];int k=pos[root];if(l1<k) l[root]=build(l1,k-1,l2,k+l2-l1-1);if(r1>k) r[root]=build(k+1,r1,k+l2-l1-1+1,r2-1);return root;
}
void dfs(int root)
{queue<int> q; q.push(root);bool flag=false;while(q.size()){int t=q.front(); q.pop();if(flag) cout<<" ";cout<<t;flag=true;if(l.count(t)) q.push(l[t]);if(r.count(t)) q.push(r[t]);}
}
int main(void)
{cin>>n;for(int i=0;i<n;i++) cin>>a[i];for(int i=0;i<n;i++){cin>>b[i];pos[b[i]]=i;}int root=build(0,n-1,0,n-1);dfs(root);return 0;
}

456. 132 模式【未完成 单调栈】

class Solution {public:bool find132pattern(vector<int>& nums) {int right=-1e9;stack<int>st;for(int i=nums.size()-1;i>=0;i--){if(!st.size()) {st.push(nums[i]); continue;}else if(st.size()==1){if(st.top()<nums[i]){right=max(right,(int)st.top());st.push(nums[i]);}else{right=st.top();}}else {if(st.top()>nums[i]&&right>nums[i])return true;}}   return false;}
};

131. 直方图中最大的矩形【思维 / 单调栈】

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long int LL;
LL h[N],l[N],r[N],n;
int main(void)
{while(cin>>n,n){for(int i=1;i<=n;i++) cin>>h[i];h[0]=-1,h[n+1]=-1;//稍兵边界stack<int>st; st.push(0);for(int i=1;i<=n;i++)//单调栈求左边第一个比他小的数{while(st.size()&&h[st.top()]>=h[i]) st.pop();l[i]=st.top();st.push(i);}while(st.size()) st.pop();st.push(n+1);for(int i=n;i>=1;i--)//单调栈求右边第一个比他小的数{while(st.size()&&h[st.top()]>=h[i]) st.pop();r[i]=st.top();st.push(i);}LL ans=0;for(int i=1;i<=n;i++) ans=max(ans,h[i]*(r[i]-l[i]-1));//枚举上边界cout<<ans<<endl;}return 0;
}

82. 删除排序链表中的重复元素 II【模拟】

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {public:ListNode* deleteDuplicates(ListNode* head) {auto h=new ListNode(-1);auto p1=h;auto p2=p1;map<int,int>mp;while(head){if(mp[head->val]){head=head->next;p1=p2;p1->next=NULL;continue;}p1->next=new ListNode(head->val);p2=p1;p1=p1->next;mp[head->val]++;head=head->next;}return h->next;}
};

36. 合并两个排序的链表【归并】

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {public:ListNode* merge(ListNode* l1, ListNode* l2) {auto head=new ListNode(-1);auto temp=head;while(l1&&l2){if(l1->val<=l2->val) temp=temp->next=l1,l1=l1->next;else temp=temp->next=l2,l2=l2->next;}while(l1) temp=temp->next=l1,l1=l1->next;while(l2) temp=temp->next=l2,l2=l2->next;return head->next;}
};

61. 旋转链表【模拟】


通过分析你会发现,每次移动就是讲最后一个点移到头。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {public:ListNode* rotateRight(ListNode* head, int k) {if(!head) return head;int cnt=0;auto temp=head;while(temp) temp=temp->next,cnt++;k=k%cnt;temp=head;if(!k) return head;for(int i=1;i<=cnt-k;i++) {auto s=temp;temp=temp->next;if(i==cnt-k) s->next=NULL;}auto ans=temp;while(temp->next) temp=temp->next;temp->next=head;return ans;}
};

33. 链表中倒数第k个节点

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {public:ListNode* findKthToTail(ListNode* pListHead, int k) {auto temp=pListHead;int n=0,cnt=0;while(temp) n++,temp=temp->next;ListNode* ans=NULL;n=n-k+1;for(auto i=pListHead;i&&cnt<n;i=i->next,cnt++) ans=i;return ans;}
};

173. 二叉搜索树迭代器【用栈模拟 输出中序遍历】

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class BSTIterator {public:stack<TreeNode*>st;BSTIterator(TreeNode* root) {while(root) {st.push(root);root=root->left;}}int next() {auto root=st.top(); st.pop();int val=root->val;root=root->right;while(root) {st.push(root);root=root->left;}return val;}bool hasNext() {return st.size();}
};/*** Your BSTIterator object will be instantiated and called as such:* BSTIterator* obj = new BSTIterator(root);* int param_1 = obj->next();* bool param_2 = obj->hasNext();*/

19. 二叉树的下一个节点


有两种情况:
一种是有右孩子。
一种是没有右孩子,则需要向上走。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode *father;*     TreeNode(int x) : val(x), left(NULL), right(NULL), father(NULL) {}* };*/
class Solution {public:TreeNode* inorderSuccessor(TreeNode* p) {if(p->right){p=p->right;while(p->left) p=p->left;return p;}while(p->father&&p->father->right==p) p=p->father;return p->father;}
};

2021春季每日一题 【week2 未完结】相关推荐

  1. 2021春季每日一题【week4 完结】

    目录 80. 删除有序数组中的重复项 II 817. 数组去重 81. 搜索旋转排序数组 II 22. 旋转数组的最小数字 153. 寻找旋转排序数组中的最小值 68. 0到n-1中缺失的数字 154 ...

  2. 2021春季每日一题【week6 未完结】

    目录 28. 实现 strStr()[KMP] 141. 周期[KMP 未完成] 91. 解码方法[未完成] 821. 跳台阶 363. 矩形区域不超过 K 的最大数值和[前缀和] 3412. 邻域均 ...

  3. 2021春季每日一题【week5 未完结】

    目录 179. 最大数[贪心] 1453. 移掉K位数字[贪心 / 思维] 783. 二叉搜索树节点最小距离 71. 二叉树的深度 208. 实现 Trie (前缀树)[未完成] 142. 前缀统计[ ...

  4. 2021春季每日一题【week3 未完结】

    目录 190. 颠倒二进制位 77. 翻转单词顺序 74. 搜索二维矩阵 15. 二维数组中的查找[思维 双指针] 90. 子集 II[dfs] 93. 递归实现组合型枚举 1006. 笨阶乘[栈] ...

  5. 2021春季每日一题【week8 未完结】

    目录 7. 整数反转[难度: 一般 / 知识点: 模拟] 78. 左旋转字符串[难度: 简单 / 知识点: 字符串] 435. 传球游戏[难度: 一般 / 知识点: DP] 7. 整数反转[难度: 一 ...

  6. 2021春季每日一题 【week1 未完结】

    目录 92. 反转链表 II[难度: 中 / 知识点: 链表] 35. 反转链表[难度: 中 / 知识点: 链表] 1603. 设计停车系统 [难度: 简单 / 知识点: 模拟] 3267. 小明上学 ...

  7. 2021暑假每日一题 【week4 完结】

    目录 3781. 乘车问题[难度: 简单 / 知识点: 模拟] 3782. 点[难度: 中 / 知识点: 数学 推式子] 3783. 第 k 个除数[难度: 一般 / 知识点: 数学 求因子] 378 ...

  8. 2021暑假每日一题 【week9 完结】

    目录 3824. 在校时间[简单 / 知识点: 模拟] 3825. 逃离大森林[中 / 知识点: bfs 思维] 3824. 在校时间[简单 / 知识点: 模拟] #include<bits/s ...

  9. 2021暑假每日一题 【week5 完结】

    目录 3790. 录入单词[难度: 一般 / 知识点: 模拟] 3791. 解码[难度: 简单 / 知识点: 模拟] 3792. 质数问题[难度: 简单 / 知识点: 线性筛 枚举] 3793. 最大 ...

最新文章

  1. Linux那些事儿之我是Sysfs(1)sysfs初探
  2. 计算机C语言1000-1099,歷届重庆市计算机二级C语言考试试题及答案.doc
  3. go chan 缓存与阻塞
  4. ubantu安装coturn穿透服务器
  5. 第一百零八期:比较容易理解的Hbase架构全解,10分钟学会,建议收藏
  6. 关于asp.net 中 cookies 的清空
  7. 资产组合管理中有哪些基础概念?
  8. Web下的整体测试 --性能测试及优化思路
  9. 【Hive】表生成(Table-Generating)函数
  10. 非名校毕业,如何在六天面试六家顶尖科技公司,并拿到全部Offer?
  11. ubuntu10.04 MTK开发环境
  12. PHP 过滤字符串特殊符号
  13. python展示文件_python之文件的读写和文件目录以及文件夹的操作实现代码
  14. 手撕Boost!Boost公式推导及实验验证
  15. python画五角星代码_如何用python画一个五角星_python绘制五角形教程
  16. 尘世了了 花开花落昔年同
  17. 1279C. Stack of Presents
  18. 由浅入深玩转华为WLAN—12安全认证配置(5)Portal认证,外置Protal服务器TSM对接
  19. 在ADSP-BF561上使用x264(4):确认热点
  20. 数据包嗅探 Packet Sniffing

热门文章

  1. 中科院分词系统(NLPIR)JAVA简易教程
  2. Ubuntu中的vi模式中的按上下左右键变成ABCD解决方法
  3. 洛谷P1095守望者的逃离题解-伪动态规划/贪心
  4. Git remote 修改源
  5. BZOJ3110: [Zjoi2013]K大数查询
  6. imx6背光驱动调试
  7. Linux 下如何安装软件?
  8. JavaScript window.location物
  9. Counting power sets
  10. 【软件工程】UML软件