刷题-Leetcode-1365. 有多少小于当前数字的数字

1365. 有多少小于当前数字的数字

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/

题目描述

在这里插入图片描述

题目分析

1.排序
排序后相同的值以左边值的下标为准。
2.哈希做映射

class Solution {
    
    
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
    
    
        int n = nums.size();
        vector<int> temp = nums;
        sort(temp.begin(), temp.end());
        int hash[101];
        for(int i = n - 1; i >= 0; i--){
    
    //从后向前遍历
            hash[temp[i]] = i;
        }
        for(int i = 0; i < n; i++){
    
    
            nums[i] = hash[nums[i]];
        }
        return nums;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_42771487/article/details/120364887
今日推荐