<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [LeetCode] 1331. Rank Transform of an Array 數組序號轉換


      Given an array of integers arr, replace each element with its rank.

      The rank represents how large the element is. The rank has the following rules:

      • Rank is an integer starting from 1.
      • The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
      • Rank should be as small as possible.

      Example 1:

      Input: arr = [40,10,20,30]
      Output: [4,1,2,3]
      Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.

      Example 2:

      Input: arr = [100,100,100]
      Output: [1,1,1]
      Explanation: Same elements share the same rank.

      Example 3:

      Input: arr = [37,12,28,9,100,56,80,5,12]
      Output: [5,3,4,2,8,6,7,1,3]

      Constraints:

      • 0 <= arr.length <= 105
      • -109 <= arr[i] <= 109

      這道題給了一個數組 arr,說是讓把每個數字替換為其在該數組中大小的 rank 值,數字越大其 rank 值越大,最小的數字的 rank 值為1,逐漸增大。對于相同的數字其 rank 值相同,而下一個數字的 rank 值不能因之前有相同的 rank 值而跳過空位。雖說是道簡單的題目,但還是有一定的技巧的。此題的核心就是需要知道每個數字的 rank 值,但是給定的數組是亂序的,所以可以直接排個序,這樣數字就是有序的了。

      我們可以建立每個數字和其 rank 值之間的映射,這樣之后就能快速的知道每個數字的 rank 值,從而返回正確的 rank 值數組。博主剛開始的想法是每個數字的映射值就是其下標加1,但是這種更新方法在存在相同數字時就不正確了。正確方法是維護一個 rank 變量,初始化為1,然后遍歷排序后的數字,對于遍歷到的數字,若其不在 HashMap 中,則賦予其當前 rank 值,然后 rank 值自增1。這樣可以保證跳過所有相同的數字,使得后面不同的數字都賦上正確的 rank 值,參見代碼如下:


      class Solution {
      public:
          vector<int> arrayRankTransform(vector<int>& arr) {
              int rank = 1;
              unordered_map<int, int> rankMap;
              vector<int> nums = arr;
              sort(nums.begin(), nums.end());
              for (int num : nums) {
                  if (rankMap.count(num)) continue;
                  rankMap[num] = rank++;
              }
              for (int &num : arr) {
                  num = rankMap[num];
              }
              return arr;
          }
      };
      

      Github 同步地址:

      https://github.com/grandyang/leetcode/issues/1331


      類似題目:

      Rank Transform of a Matrix

      Find Target Indices After Sorting Array


      參考資料:

      https://leetcode.com/problems/rank-transform-of-an-array/

      https://leetcode.com/problems/rank-transform-of-an-array/solutions/489753/java-c-python-hashmap/


      LeetCode All in One 題目講解匯總(持續更新中...)

      posted @ 2023-02-19 09:20  Grandyang  閱讀(248)  評論(0)    收藏  舉報
      Fork me on GitHub
      主站蜘蛛池模板: 国产亚洲精品综合99久久| 欧美极品色午夜在线视频| av一本久道久久波多野结衣| 97一区二区国产好的精华液| 欧美和黑人xxxx猛交视频| 亚洲精中文字幕二区三区| 精品熟女日韩中文十区| 国产蜜臀av在线一区二区| 亚洲综合精品中文字幕| 国产欧美另类久久久精品不卡| 亚洲午夜精品毛片成人播放| 国产精品白浆在线观看免费| 人妻另类 专区 欧美 制服| 国产成人亚洲一区二区三区 | 久久久久香蕉国产线看观看伊| 中文字幕一区二区人妻电影| 亚洲精品一区二区动漫| 秭归县| 亚洲国产精品无码久久电影| 一区二区三区av天堂| 色8久久人人97超碰香蕉987| 东安县| 亚洲国产超清无码专区| 精品亚洲国产成人av| 亚洲人成网站在线无码| 又黄又硬又湿又刺激视频免费| 仲巴县| 丰满人妻一区二区三区无码AV| 国产亚洲精品久久yy50| 免费无码影视在线观看mov| 暖暖 免费 高清 日本 在线观看5 色老头亚洲成人免费影院 | 裸体美女无遮挡免费网站| 在线观看精品视频网站| 日韩精品人妻系列无码av东京| 乱人伦人妻系列| 国产精品午夜福利资源| 国产女人和拘做受视频免费| 91热在线精品国产一区| 国产人妻精品午夜福利免费| 邻居少妇张开腿让我爽了一夜| 人妻中文字幕精品一页|