LeetCode——有多少小于当前数字的数字

题目地址:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/

解题思路:遍历一遍,累计求和

class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        int a[105] = { 0 };//表示数字i的个数
        int i,n;
        n = nums.size();
        for (i = 0; i < n; i++)
            a[nums[i]]++;
        for (i = 1; i< 105; i++)//
            a[i] += a[i - 1];
        vector<int> ans;
        for ( i = 0; i < nums.size(); i++) 
            nums[i] == 0 ? ans.push_back(0) : ans.push_back(a[nums[i] - 1]);
        return ans;
    }
};

 

posted @ 2020-10-26 10:54  CCxiao5  阅读(81)  评论(0编辑  收藏  举报