<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)503:下一個更大元素 II

          共 1422字,需瀏覽 3分鐘

           ·

          2022-01-23 13:31

          算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?下一個更大元素 II,我們先來看題面:
          https://leetcode-cn.com/problems/next-greater-element-ii/

          Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.


          The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.

          給定一個循環(huán)數組(最后一個元素的下一個元素是數組的第一個元素),輸出每個元素的下一個更大元素。數字 x 的下一個更大的元素是按數組遍歷順序,這個數字之后的第一個比它更大的數,這意味著你應該循環(huán)地搜索它的下一個更大的數。如果不存在,則輸出 -1。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?

          輸入: [1,2,1]
          輸出: [2,-1,2]
          解釋: 第一個 1?的下一個更大的數是 2;
          數字 2?找不到下一個更大的數;
          第二個 1?的下一個最大的數需要循環(huán)搜索,結果也是 2
          注意: 輸入數組的長度不會超過 10000。


          解題

          https://blog.csdn.net/weixin_44171872/article/details/107309751

          方法1:使用單調棧
          主要思路:
          (1)對于循環(huán)數組,可以考慮這樣理解:既將原數組擴展一倍,這樣就相當于使用一個 2 倍長的數組表示出循環(huán)數組的效果;
          (2)然后對這個兩倍長的數組,直接使用單調棧從后向前找每個元素右側的第一個大于其值的值,但只需要保存前一半即可;
          (3)這樣理解,也沒必要實際的擴展數組,只需要對原數組遍歷兩遍即可;

          class?Solution?{
          public:
          ????vector<int> nextGreaterElements(vector<int>& nums) {
          ????????vector<int>res(nums.size(),0);
          ????????stack<int> st;
          ????//使用單調棧第一次遍歷
          ????????for(int?i=nums.size()-1;i>=0;--i){
          ????????????while(!st.empty()&&st.top()<=nums[i]){
          ????????????????st.pop();
          ????????????}
          ????????????st.push(nums[i]);
          ????????}
          ????//使用單調棧第二次遍歷,保存相關的結果
          ????????for(int?i=nums.size()-1;i>=0;--i){
          ????????????while(!st.empty()&&st.top()<=nums[i]){
          ????????????????st.pop();
          ????????????}
          ????????????if(st.empty()){
          ????????????????res[i]=-1;
          ????????????}
          ????????????else{
          ????????????????res[i]=st.top();
          ????????????}
          ????????????st.push(nums[i]);
          ????????}

          ????????return?res;
          ????}
          };

          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力 。

          上期推文:
          LeetCode1-500題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)501:二叉搜索樹中的眾數
          LeetCode刷題實戰(zhàn)502:IPO

          瀏覽 18
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  三级黄色电影院 | 干一干操一操 | 艹逼网止| 日本亚洲色大成网站www久久 | 黄色网址在线播放 |