数组-1365-计数排序

3 篇文章 0 订阅
3 篇文章 0 订阅

在这里插入图片描述
不管位于该数据的前后都要进行计算

暴力法:

class Solution(object):
    def smallerNumbersThanCurrent(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        res=[0]*len(nums)
        for i in range(len(nums)):
            num=0
            for j in range(i):
                if nums[i]>nums[j]:
                    num+=1
            for k in range(i+1,len(nums)):
                if nums[i]>nums[k]:
                    num+=1
            res[i]=num
        return res

2.计数排序:计数排序适用于纯数字的比较,占内存
先迭代统计次数,后对前面的数据进行求和,根据数组中的数据转换为对应的数据索引获得求和后的结果即为比较的次数
代码待更新:

class Solution(object):
    def smallerNumbersThanCurrent(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        res=[0]*len(nums)
        count=[0]*101
        for i in nums:
            count[i]+=1
        total=0
        for i in range(len(count)):
            pre_count=total
            total+=count[i]
            count[i]=pre_count
        for i in range(len(res)):
            res[i]=count[nums[i]]
        return res
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值