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
                
  • 相关阅读:
    131.著作权
    130.专利权
    idea新用法
    map的put和putIfAbsent使用
    netty的option和childOption
    Java8 lam。。。表达式
    protobuf学习
    protobuf生成
    idea调试
    spring,mapper的参数
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13205099.html
Copyright © 2011-2022 走看看