LeetCode-1365 How Many Numbers Are Smaller Than the Current Number Solution with python

Posted sheepcore

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-1365 How Many Numbers Are Smaller Than the Current Number Solution with python相关的知识,希望对你有一定的参考价值。

1. Description

notes:

2. Examples:

3. Solution:

 1 """
 2     created by sheepcore on 2020-03-02
 3 """
 4 from typing import List
 5 
 6 
 7 def smallerNumbersThanCurrentV2(nums: List[int]) -> List[int]:
 8     """
 9     excellent solution by mudin
10     :param nums:
11     :return:
12     """
13     return [sorted(nums).index(a) for a in nums]
14 
15 
16 def smallerNumbersThanCurrent(nums: list()) -> list():
17     """
18     This is my solution.
19     :param nums:
20     :return:
21     """
22     i = 0
23     res = list()
24     while i < len(nums):
25         cur = nums[i]
26         smaller = 0
27         j = 0
28         while j < len(nums):
29             if j != i and nums[j] < nums[i]:
30                 smaller += 1
31             j += 1
32         res.append(smaller)
33         i += 1
34     return res
View Code

4. Summary:

  •  善于使用排序功能

 

以上是关于LeetCode-1365 How Many Numbers Are Smaller Than the Current Number Solution with python的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 1365. How Many Numbers Are Smaller Than the Current Number 解题报告

Leetcode 1365. How Many Numbers Are Smaller Than the Current Number

LeetCode 1365 - 寻找比 O(n^2) 更好的解决方案

leetcode1365

UVA - 10061 How many zero&#39;s and how many digits ?

hdu 1316 How Many Fibs?