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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2021春季每日一题 【week2 未完结】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • LeetCode 191. 位1的個(gè)數(shù)【難度: 簡單 / 知識(shí)點(diǎn): 位運(yùn)算】
  • 85. 不用加減乘除做加法【難度: 中 / 知識(shí)點(diǎn): 思維 全加器】
  • 341. 扁平化嵌套列表迭代器【模擬】
  • 1497. 樹的遍歷【根據(jù)遍歷的順序 建樹】
  • 456. 132 模式【未完成 單調(diào)棧】
  • 131. 直方圖中最大的矩形【思維 / 單調(diào)棧】
  • 82. 刪除排序鏈表中的重復(fù)元素 II【模擬】
  • 36. 合并兩個(gè)排序的鏈表【歸并】
  • 61. 旋轉(zhuǎn)鏈表【模擬】
  • 33. 鏈表中倒數(shù)第k個(gè)節(jié)點(diǎn)
  • 173. 二叉搜索樹迭代器【用棧模擬 輸出中序遍歷】
  • 19. 二叉樹的下一個(gè)節(jié)點(diǎn)

LeetCode 191. 位1的個(gè)數(shù)【難度: 簡單 / 知識(shí)點(diǎn): 位運(yùn)算】

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. 不用加減乘除做加法【難度: 中 / 知識(shí)點(diǎn): 思維 全加器】


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

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


就是遍歷樹,存一下數(shù)據(jù)。

/*** // 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. 樹的遍歷【根據(jù)遍歷的順序 建樹】

#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 模式【未完成 單調(diào)棧】

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. 直方圖中最大的矩形【思維 / 單調(diào)棧】

#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++)//單調(diào)棧求左邊第一個(gè)比他小的數(shù){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--)//單調(diào)棧求右邊第一個(gè)比他小的數(shù){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. 刪除排序鏈表中的重復(fù)元素 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. 合并兩個(gè)排序的鏈表【歸并】

/*** 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. 旋轉(zhuǎn)鏈表【模擬】


通過分析你會(huì)發(fā)現(xiàn),每次移動(dòng)就是講最后一個(gè)點(diǎn)移到頭。

/*** 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. 鏈表中倒數(shù)第k個(gè)節(jié)點(diǎn)

/*** 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. 二叉樹的下一個(gè)節(jié)點(diǎn)


有兩種情況:
一種是有右孩子。
一種是沒有右孩子,則需要向上走。

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美日韩久久精品 | a级黄色小视频 | 亚洲免费在线视频观看 | 美国美女黄色片 | 色无极亚洲影院 | 亚洲国产私拍精品国模在线观看 | 性人久久久久 | 亚洲精品激情 | 国产图片区 | 无码一区二区波多野结衣播放搜索 | 成人午夜在线观看视频 | 国产原创在线观看 | 91精品视频网 | 五月婷婷激情五月 | 国产在线观看黄色 | 激情福利在线 | 精品97人妻无码中文永久在线 | 黄色三级在线观看 | 最近最新中文字幕 | 国产美女在线免费观看 | 最新中文字幕在线观看 | 国产黄色一区二区 | 成人xx视频 | 米奇色| 国产精品中文在线 | 直接看av的网站 | 女生毛片 | 超碰成人在线观看 | 亚洲AV成人无码一二三区在线 | www.国产一区二区三区 | 日韩 欧美 精品 | 日韩av网页| 日韩精品专区 | 蜜桃又黄又粗又爽av免 | 亚洲国产高清国产精品 | 在哪里可以看毛片 | 日本韩国在线播放 | 一级毛片aa| 国产美女久久久 | 精品国产一二三区 | 香蕉国产精品 | 2019天天干 | 蜜桃av影视 | 97se亚洲综合 | 亚洲自拍p | 又欲又污又肉又黄短文 | 黑人玩弄人妻一区二区三区影院 | 人妻一区二区三区视频 | 熟女俱乐部一区二区视频在线 | 91av导航 | av一区二区三区在线 | 国产亚洲无码精品 | 免费观看亚洲视频 | 免费在线观看不卡av | xxxxⅹxxxhd日本8hd| 精品一性一色一乱农村 | 欧美自拍偷拍一区二区 | 男女野外做受全过程 | 欧美激情视频一区二区三区 | 日韩中文字幕免费在线观看 | 欧美一区二区三区小说 | 伊人tv | 黄色特一级 | 国产精品无码一区二区三区免费 | 搞黄网站在线观看 | 亚洲v天堂| 人人干在线视频 | 国产精品久久久久影院色老大 | 国产手机视频在线 | 欧美片 | 男人的天堂你懂的 | 亚洲一区二区在线视频 | 在线看av网址 | 午夜寂寞影院在线观看 | 日韩精品中文字幕一区二区三区 | 成人免费视频网站在线看 | 少妇性l交大片免潘金莲 | 第四色视频 | 久久综合五月婷婷 | 国产精品夜夜夜爽张柏芝 | 岛国片在线播放 | 欧美中文字幕 | 天堂男人在线 | 女同爱爱视频 | 国产精品久久九九 | 国产高清视频一区二区 | 日本特黄一级大片 | 香蕉成人网| 国产在线中文 | 最新欧美日韩 | 福利视频三区 | 天天搞天天干 | 18在线观看视频 | 国产乱码av| 中文字幕一二三四区 | 夜夜躁狠狠躁 | 99久久久无码国产精品性波多 | 亚洲欧美一区二区三区在线观看 | 性视频在线|