zoukankan      html  css  js  c++  java
  • 1365. 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. 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.

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

    先用hash table count[i]来记录每个数出现的次数,得到这个count之后再对这个count求前缀和,count[i]就表示小于等于i的数的个数。

    class Solution(object):
        def smallerNumbersThanCurrent(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            count = [0] * 101
            for num in nums:
                count[num] += 1
            for i in range(1, 101, 1):
                count[i] += count[i - 1]
            ans = []
            for num in nums:
                if num == 0:
                    ans.append(0)
                else:
                    ans.append(count[num - 1])
            return ans
                
  • 相关阅读:
    2017年9月22日 关于JS数组
    2017年9月20日
    2017年9月19日 JavaScript语法操作
    2017年9月18日
    2017年9月17日 JavaScript简介
    2017年9月16日
    2017年9月15日
    2017年9月14日
    2017年9月12日
    贪吃蛇全文
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13205099.html
Copyright © 2011-2022 走看看