日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2021春季每日一题 【week2 未完结】

發布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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. 不用加減乘除做加法【難度: 中 / 知識點: 思維 全加器】


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;} }; 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的2021春季每日一题 【week2 未完结】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。