其他分享
首页 > 其他分享> > LeetCode #1365. How Many Numbers Are Smaller Than the Current Number

LeetCode #1365. How Many Numbers Are Smaller Than the Current Number

作者:互联网

题目

1365. How Many Numbers Are Smaller Than the Current Number


解题方法

设立一个temp数组存放nums中元素的有序序列,遍历temp中的每个元素i,累计已经遍历过的数的个数countnum,如果当前元素i不在dic中就加进去,值设置为已经遍历过的数的个数countnum,这个遍历结束之后遍历nums查字典写个数即可。
时间复杂度:O(nlogn)
空间复杂度:O(n)


代码

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        temp = sorted(nums)
        countnum = 0
        dic = {}
        for i in temp:
            if i not in dic:
                dic[i] = countnum
            countnum += 1
        
        rat = []
        for i in range(len(nums)):
            rat.append(dic[nums[i]])
        
        return rat

标签:Smaller,temp,countnum,Many,nums,Number,dic,rat,遍历
来源: https://www.cnblogs.com/RatsCommander/p/14037127.html