2021春季每日一题 【week2 未完结】
生活随笔
收集整理的這篇文章主要介紹了
2021春季每日一题 【week2 未完结】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 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. 不用加減乘除做加法【難度: 中 / 知識點: 思維 全加器】
341. 扁平化嵌套列表迭代器【模擬】
就是遍歷樹,存一下數據。
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. 旋轉鏈表【模擬】
通過分析你會發現,每次移動就是講最后一個點移到頭。
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. 二叉樹的下一個節點
有兩種情況:
一種是有右孩子。
一種是沒有右孩子,則需要向上走。
總結
以上是生活随笔為你收集整理的2021春季每日一题 【week2 未完结】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021春季每日一题 【week1 未完
- 下一篇: 2021春季每日一题【week8 未完结