Leetcode a daily question: 1365.how-many-numbers-are-smaller-than-the-current-number (how many numbers are smaller than the current number)

Insert picture description here
Idea: The time complexity of brute force cracking is O(n 2 ), and the length of the array here is not greater than 500, which is on the order of tens of thousands, and it will definitely not time out;
Insert picture description here

class Solution {
    
    
public:
    vector<int> smallerNumbersThanCurrent(vector<int> &nums)
    {
    
    
        int len = nums.size();
        vector<int> res(len, 0);
        for (int i = 0; i < len; i++)
        {
    
    
            int temp = nums[i];
            int count = 0;
            for (int j = 0; j < len; j++)
            {
    
    
                if (nums[j] < temp)
                {
    
    
                    count++;
                }
            }
            res[i] = count;
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/wyll19980812/article/details/109282957