不管位于该数据的前后都要进行计算
暴力法:
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