1019. 链表中的下一个更大节点
生活随笔
收集整理的這篇文章主要介紹了
1019. 链表中的下一个更大节点
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2020-05-15
1.題目描述
鏈表中的下一個(gè)更大節(jié)點(diǎn)2.題解
我是將他轉(zhuǎn)化為數(shù)組來(lái)求解下一個(gè)比它大的數(shù)3.代碼
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:vector<int> nextLargerNodes(ListNode* head) {stack<int> mystack;map<int,int> mymap;vector<int> tmp,res;ListNode* p=head;int l=0;while (p){tmp.push_back(p->val);p=p->next;l++;}for (int i=0;i<l;i++){while (!mystack.empty()&&tmp[mystack.top()]<tmp[i]){mymap[mystack.top()]=tmp[i];mystack.pop();}mystack.push(i);}while (!mystack.empty()){mymap[mystack.top()]=0;mystack.pop();}for (int i=0;i<l;i++){res.push_back(mymap[i]);}return res;} };總結(jié)
以上是生活随笔為你收集整理的1019. 链表中的下一个更大节点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: .NET C#使用微信公众号登录网站
- 下一篇: mysql安装可能遇到的错误和安装过程