方法一:
class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
return [sum(1 for j in range(len(nums)) if i > nums[j]) for i in nums]
方法二:
class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
res = []
count = 0
for i in range(len(nums)):
for j in range(len(nums)):
if nums[i]>nums[j]:
count += 1
res.append(count)
count = 0
return res
本文介绍了两种不同的算法实现方法,用于解决给定数组中每个元素比其小的元素数量的问题。第一种方法使用列表推导式结合for循环进行比较计数,第二种方法采用双重for循环遍历数组元素进行大小比较并累计计数。
303

被折叠的 条评论
为什么被折叠?



