描述

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j’s such that j != i and nums[j] < nums[i].

Return the answer in an array.

Example 1:

Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

Example 2:

Input: nums = [6,5,4,8]
Output: [2,1,0,3]

Example 3:

Input: nums = [7,7,7,7]
Output: [0,0,0,0]

Note:

2 <= nums.length <= 500
0 <= nums[i] <= 100

解析

根据题意,只需要在 nums 中找出每个元素的比其小的元素的个数,然后组成列表即可。所以将 nums 升序排序为 tmp ,然后在遍历 nums 的时候只需要定位每个元素在 tmp 中第一次出现的索引追加到 res 中,遍历结束将得到的答案。

解答

class Solution(object):def smallerNumbersThanCurrent(self, nums):""":type nums: List[int]:rtype: List[int]"""tmp = sorted(nums)res = []for i in nums:res.append(tmp.index(i))return res

运行结果

Runtime: 48 ms, faster than 74.62% of Python online submissions for How Many Numbers Are Smaller Than the Current Number.
Memory Usage: 13.5 MB, less than 37.19% of Python online submissions for How Many Numbers Are Smaller Than the Current Number.

原题链接:https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

leetcode 1365. How Many Numbers Are Smaller Than the Current Number(python)相关推荐

  1. 1365. How Many Numbers Are Smaller Than the Current Number 有多少小于当前数字的数字

    给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目. 换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 ...

  2. 40 How Many Numbers Are Smaller Than the Current Number

    题目 Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it ...

  3. Leetcode 17. Letter Combinations of a Phone Number(python)

    笨方法回溯啦 class Solution(object):def letterCombinations(self, digits):""":type digits: s ...

  4. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  5. 【Leetcode】 刷题之路1(python)

    leetcode 刷题之路1(python) 看到有大佬总结了一些相关题目,想着先刷一类. 1.两数之和 15.三数之和 16.最接近的三数之和 11.盛最多的水 18.四数之和 454.四数相加II ...

  6. [LeetCode]题解(python):150-Evaluate Reverse Polish Notation

    题目来源: https://leetcode.com/problems/evaluate-reverse-polish-notation/ 题意分析: 给定一个数组,用这个数组来表示加减乘除,例如 [ ...

  7. [LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree

    题目来源: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意分析: 给出一个排好序的数组,根据这 ...

  8. [LeetCode]题解(python):019-Remove Nth Node From End of List

    题目来源: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意分析: 这道题是给定一个链表,删除倒数第n个节点.提醒, ...

  9. [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array

    题目来源: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 题意分析: 在一个不重复的翻转的数组里面找到最小那个 ...

最新文章

  1. 【使用CMake组织C++工程】2:CMake 常用命令和变量
  2. android 比较两个list,比较两个List的内容是否相等
  3. 计算机网络数据容量100M,华工网络教育计算机网络作业及答案
  4. 蓄水池问题c语言编程,蓄水池大作战
  5. 【转】测试过程管理案例6---如何做项目的测试经理?
  6. linux 窗口管理器_您最喜欢的Linux窗口管理器是什么?
  7. 在Linux使用exec执行命令时报的哪些错
  8. Java中的静态变量、静态方法问题
  9. MySQL分组求和GROUP BY
  10. 配置单交换机VLAN划分
  11. 西安电子科技大学计算机学院评论,西电高琳教授团队在Science子刊及领域著名期刊发表成果...
  12. 【SaaS考试认证】aPaaS_腾讯千帆神笔
  13. 天仙般的王祖贤和林青霞,她们都是用AI修复的
  14. 学霸 帝国理工 本科 计算机,帝国理工和剑桥计算机的offer,该去哪个?
  15. 解决Unable to open debugger port错误
  16. stm32 ADXL345传感器
  17. 感谢在俄勒冈州Develo的SAOs软件协会参加我的演讲“音乐背后的Web服务”的所有人...
  18. 第三章:IP地址加掩码讲解和ping命令运用
  19. vue修改网站名称和图标
  20. Saleor:开源、免费的无头电子商务平台——系列教程,快速打造属于你的电商网站

热门文章

  1. 邢台学院计算机老师待遇2020,老师待遇不好?2020年的三个教师改革,将会让老师迎来事业第二春...
  2. Zion医疗中心使用RTLS系统,减少气垫丢失概率
  3. lammps教程:径向分布函数g(r)的计算与输出方法
  4. Java高级用户指南-核心Java
  5. [随笔]解决高分屏下软件显示模糊
  6. MSRA-TD500数据集坐标转换
  7. 上计算机课玩游戏检讨400字,学生犯错万能检讨书400字7篇
  8. APP支付之使用ApplePay支付开发步骤
  9. 用计算机pol计算方位角,如何通过编程代替计算器内部函数“Pol和Rec”
  10. OpenGL入门北斗星点绘制