码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-02 10:44:31      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:ret   with   The   sorted   desc   excel   one   current   nbsp   

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

标签:ret   with   The   sorted   desc   excel   one   current   nbsp   

原文地址:https://www.cnblogs.com/sheepcore/p/12394070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!