题目

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]

Constraints:

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

分析

题意:对于数组中的每个数字,求出整个数组中比他小的数组的个数

最简单的解法就是对于每个数字,都遍历一遍数组挨个比较,这样的话,复杂度显然是n的n次方。
有没有更好的解法?

很容易想到,将数组按升序排序,下标i,就是整个数组中比他小的数组的个数.
但是,对于重复的数据,下标i就不准确了。因此要对其去重。

解答

我写了一个,报错了。应该是遍历出错了,这个算法应该是双层遍历才对,我只写了一个。
排序去重的数组长度是MAX值101(nums[i] <= 100),nums的长度不确定,我不太清楚怎么遍历获取。

class Solution {public int[] smallerNumbersThanCurrent(int[] nums) {int len = nums.length;int[] res = new int[len];int[] afterSD = sortAndDist(nums);int i=0;int j=0;int k=0;while(k<len){if(afterSD[i]==0){i++;continue;}else if(afterSD[i]!=nums[j]){i++;j++;continue;}else {res[k]=i;k++;}}return res;}public int[] sortAndDist(int[] nums){int[] tmp = new int[101];for(int i=0;i<nums.length;i++){tmp[nums[i]]=1;}return tmp;}
}

看看别人写的。
做法类似,但是思路不太一样,
我想的算法还有个漏洞,比如[1,2,3,3,4],如果对其去重排序,得到的是
[1,2,3,4]
那么比1小的有0个,比2小的有1个,比3小的有2个,到这里都是正确的,
但是比4小的有3个,显然是错误的。
因此去重还得记录重复个数才行。

class Solution {public int[] smallerNumbersThanCurrent(int[] nums) {int[] count = new int[101];int[] res = new int[nums.length];for (int i =0; i < nums.length; i++) {// 这里用的自增1的形式,count的数字表示对应下标的数据的重复个数count[nums[i]]++;}for (int i = 1 ; i <= 100; i++) {// 把前一个数的个数,加到后一个上面,这样,就统计了比当前数小的数据个数// 比如count=[0,1,2,3],// 意思是比0小的0个,// 比1小的有1个// 比i小的有count[i]个// 累加之后count=[0,1,3,6],显然是正确的。count[i] += count[i-1];    }for (int i = 0; i < nums.length; i++) {// 如果是0,显然比他小的只有0个if (nums[i] == 0)res[i] = 0;else  res[i] = count[nums[i] - 1];}return res;        }
}

40 How Many Numbers Are Smaller Than the Current Number相关推荐

  1. leetcode 1365. How Many Numbers Are Smaller Than the Current Number(python)

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

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

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

  3. Cast from pointer to smaller type 'int' loses information”

    uint32_t soAddress = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(rtSoAddress)); 查看 ...

  4. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport

    Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try ...

  5. VK Cup 2018 Round 1: A. Primal Sport

    A. Primal Sport time limit per test 1.5 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

  7. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  8. D. Beautiful numbers

    题目链接:http://codeforces.com/problemset/problem/55/D D. Beautiful numbers time limit per test 4 second ...

  9. LeetCode 357. Count Numbers with Unique Digits

    357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique ...

最新文章

  1. lua java 性能比较_Lua coroutine vs Java wait/notify
  2. 每秒8.8亿次请求!让数据存得起,看得见 - 云原生多模数据库Lindorm 2020双十一总结
  3. linux 命令博客,Linux命令(一)
  4. PAT乙类1010之1010 一元多项式求导
  5. unserialize用法
  6. 最长回文串之中心扩散法
  7. 全球活跃开发者不足 1500 万,业余爱好者和学生仅占四分之一
  8. 点聚-weboffice 6.0 (一)
  9. Android程序创意过滤与失败经验谈
  10. 前端项目构建工具---Grunt
  11. android 虚拟wifi定位,基于Android手机的WiFi定位系统设计
  12. 宝塔脚本下载慢解决办法
  13. 大学计算机技巧讲座新闻稿,计算机科学与技术名家讲座系列报道(王希胤)
  14. 82 将真分数分解为埃及分数
  15. 计算机ppt放映方式怎么改,ppt打开方式设置怎么设置
  16. C#添加、隐藏Word段落
  17. Wolfram | Alpha 之 15 种非数学领域的使用
  18. 何制作手机自适应网页
  19. Java入门part6--继承和多态
  20. 三十二位计算机游戏推荐,这32种课间游戏不重样,总有一款适合您和学生!| 推荐收藏...

热门文章

  1. 钉钉自动打卡并微信通知打卡成功
  2. python计算图形面积的方法_Python计算任意多边形面积算法
  3. 算法 2.二进制加法
  4. python中ret是什么意思_Python ret
  5. iPad Pro 突然开不了机
  6. Python实现socket简单一对一聊天
  7. python数据可视化案例 淘宝粽子_Python分析淘宝月饼销售数据,哪种最受欢迎?排第一的你想不到...
  8. 寒冬,是修炼内功的最好机会
  9. 借助高德地图开发者平台——地图可视化
  10. 焦绪录:大数据如何推动数字中国建设