1365. How Many Numbers Are Smaller Than the Current Number (E)

LeetCode --- 1365. How Many Numbers Are Smaller Than the Current Number 解题报告 【代码】LeetCode --- 1365. How Many Numbers Are Smaller Than the Current Number 解题报告。 阅读详情

How Many Numbers Are Smaller Than the Current Number (E)

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

题意

对于一个数组nums中的每一个数nums[i],统计小于nums[i]的数的个数。

思路

  1. 暴力法。
  2. Hash。
  3. 二分搜索。

代码实现 - 暴力法

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        int[] ans = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (j != i && nums[j] < nums[i]) {
                    ans[i]++;
                }
            }
        }
        return ans;
    }
}

代码实现 - Hash

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        int[] ans = new int[nums.length];
        Map<Integer, Integer> hash = new TreeMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (!hash.containsKey(nums[i])) {
                hash.put(nums[i], 1);
            } else {
                hash.put(nums[i], hash.get(nums[i]) + 1);
            }
        }
        int sum = 0;
        for (int key : hash.keySet()) {
            int temp = sum + hash.get(key);
            hash.put(key, sum);
            sum = temp;
        }
        for (int i = 0; i < ans.length; i++) {
            ans[i] = hash.get(nums[i]);
        }
        return ans;
    }
}

代码实现 - 二分法

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        int[] ans = new int[nums.length];
        int[] copy = Arrays.copyOf(nums, nums.length);
        Arrays.sort(copy);
        for (int i = 0; i < ans.length; i++) {
            ans[i] = binarySearch(copy, nums[i]);
        }
        return ans;
    }

    private int binarySearch(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] >= target) {
                right = mid;
            }
        }
        return left;
    }
}
[LVGL]:MACOS下使用LVGL模拟器 阅读详情

相关推荐

安装Quartus-II 13 和Modelsim

目录一、Quartus-II安装1.Quartus-II 13下载2.Quartus-II 13安装3.Quartus-II 13注册二、Modelsim安装 实验目的:安装Quartus-II 13 和Modelsim 实验环境:Windows10 参考资料:Quartus-II安装及使用 一、Quartus-II安装 1.Quartus-II 13下载 正点原子官方资料下载 百度网盘下载链接: https://pan.baidu.com/s/1a9d-bq9RZmWrRV542X4IEA 提取码:

qqq080的博客 8626

1365. How Many Numbers Are Smaller Than the Current Number

class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { //of valid j's such that j != i and nums[j] < nums[i]. 比当前值小的有多少个? map<int...

zeroQiaoba的博客 641

基于live555实现一个rtsp客户端程序

本文介绍了基于live555流媒体库实现的RTSP客户端程序,能够通过RTSP地址获取H.264/H.265裸码流或TS流数据并存入本地文件。详细阐述了使用CLion搭建工程的代码架构和CMake配置,重点分析了TS流解析模块的实现,包括TS包结构解析、PES包提取和ES裸流获取的完整流程。文章提供了TS/PES/ES三种数据包的协议说明,并给出了从TS流中提取视频帧的具体方法,最后附有工程代码下载链接。该方案实现了RTSP流媒体接收、解析和存储的完整功能链。

kk821521286的博客 789

LeetCode1365. How Many Numbers Are Smaller Than the Current Number

思路: count[9]=count[9]+1=0+1=1 count[2]=count[2]+1=0+1=1 count[3]=count[3]+1=0+1=1 count[3]=count[3]+1=1+1=2 count[4]=count[4]+1=0+1=1 %8 那么比9小的数就+1,1 那么比2小的数就+1,以此类推。但是所有比9小的数,应该是比9小的数+比8小的数。以...

RitaAndWakaka的博客 343

UE5实战:如何用Georeference插件搞定百度地图坐标转换(附完整代码)

本文详细解析了在UE5中使用Georeference插件实现百度地图坐标转换的全流程,包括BD-09到UE内部坐标的转换算法、UTM投影配置及性能优化技巧。通过完整代码示例和实战案例,帮助开发者高效解决数字孪生项目中的地理坐标转换难题。

weixin_30955617的博客 94

TypeScript版LangChain环境配置全攻略:从零搭建AI应用开发环境

大语言模型(LLM)作为当前AI应用开发的核心技术,通过API接口为开发者提供了强大的自然语言处理能力。其工作原理基于Transformer架构,通过预训练和微调实现对人类语言的深度理解与生成。在工程实践中,开发者需要将LLM与外部工具、数据源和业务逻辑有效集成,这正是LangChain框架的技术价值所在——它提供了标准化的组件和接口,简化了复杂AI应用的构建流程。应用场景涵盖智能客服、内容生成、数据分析助手等多个领域。本文聚焦于TypeScript/JavaScript生态下的LangChain开发,详细

weixin_29055699的博客 253

LeetCode | 1365. How Many Numbers Are Smaller Than the Current Number有多少小于当前数字的数字【Python】

LeetCode 1365. How Many Numbers Are Smaller Than the Current Number有多少小于当前数字的数字【Easy】【Python】【暴力】 Problem LeetCode Given the array nums, for each nums[i] find out how many numbers in the array are s...

Wonz 1304

superpowers-中文使用说明

《obra/superpowers:AI辅助开发的流程纪律工具集》 摘要:obra/superpowers是一套面向AI辅助软件开发的系统化流程规范,包含14个核心技能,强调"先理解后行动"的纪律性开发方法。该工具集定义了从头脑风暴到代码评审的完整工作流,要求严格遵循测试驱动开发、子代理隔离执行等原则。核心特色包括:1) 任何实现前必须完成设计和评审;2) 采用红-绿-重构的TDD循环;3) 复杂任务通过独立子代理串行处理;4) 所有产出需经过双重验证(规格合规+代码质量)。该工具集特别注重文档化和可验证性

qq_22786633的博客 765

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. That is, for each nums[i] you have to count the number of valid j’s such that j != i and nums[j] &...

weixin_43367550的博客 426

电力市场小白必看:手把手教你用Matlab实现市场出清电价计算(附完整代码)

本文为电力市场初学者提供了使用Matlab计算市场出清电价的完整指南。通过Yalmip和Matpower工具包,详细讲解了从环境配置到模型构建、求解及结果分析的整个过程,帮助读者快速掌握节点电价计算的核心技术。

weixin_30379531的博客 73

Python电商网站全站爬虫实战:以京东为例的数据采集与分析

随着电子商务的蓬勃发展,电商数据已成为市场研究、竞争分析和消费者行为研究的重要资源。本文详细介绍了如何使用Python最新技术栈构建一个高效的电商网站全站爬虫,以京东为例实现商品数据的自动化采集。文章涵盖了爬虫的法律与伦理考量、技术选型、反爬虫策略应对、分布式架构设计以及数据存储方案,并提供了完整的代码实现。本爬虫系统采用Scrapy-Redis分布式框架、Selenium动态渲染、智能代理轮换和机器学习验证码识别等先进技术,能够稳定高效地抓取京东全站商品数据,为电子商务研究提供数据支持。关键词。

2201_76125261的博客 413

1365. How Many Numbers Are Smaller Than the Current Number*

1365. How Many Numbers Are Smaller Than the Current Number* https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 题目描述 Given the array nums, for each nums[i] find out how...

珍妮的选择的博客 520

【OpenClaw从入门到精通】第04篇:Web/TUI/钉钉全打通!OpenClaw多端交互实测指南(2026避坑版)

本文详解OpenClaw三大交互方式(Web控制台、TUI终端、钉钉集成),解决新手多端操作难题。Web控制台适合可视化深度配置,TUI终端满足服务器高效运维,钉钉集成实现移动办公协同。提供2026实测配置步骤、代码示例及避坑指南,涵盖设备配对、终端命令优化、聊天机器人回调验证等核心场景,助用户快速掌握"随时召唤AI助理"的全链路方案。 关键词 OpenClaw、多端交互、Web控制台、TUI终端、钉钉集成、AI助理、实操指南 核心亮点 全链路打通:覆盖Web/TIM/IM三端配置

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 3543

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

给你一个数组&nbsp;nums,对于其中每个元素&nbsp;nums[i],请你统计数组中比它小的所有数字的数目。 换而言之,对于每个&nbsp;nums[i]&nbsp;你必须计算出有效的&nbsp;j&nbsp;的数量,其中 j 满足&nbsp;j != i 且 nums[j] &lt; nums[i]&nbsp;。 以数组形式返回答案。 &nbsp; 示例 1: 输入:nums = [8,1,2,2,3] 输出:[4,0

Alex 537

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[leetcode] 1365. How Many Numbers Are Smaller Than the Current Number

Description 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. Exa

Learning from the mistakes 205

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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

一、中文版 二、英文版 三、My answer 四、解题报告

三妹的博客 501

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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

       互联网行业的小白,写博客的目的是为了记录自己的学习过程、对自己学习中所犯的错误做一个总结。由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教! python题解题目描述题解 题目描述 题目传送门:有多少小于当前数字的数字 给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。 换而言之,对于每个 nums[i] 你必须计算出有效的j 的数量,其中 j 满足j.

鸪鹑菌的个人博客 258

Leetcode 1365. How Many Numbers Are Smaller Than the Current Number

文章作者:Tyan 博客:noahsnail.com  |  优快云  |  简书 1. Description 2. Solution **解析:**Version 1是两两比较所有的数,简单直接。Version 2,由于所有的数都在[0, 100]之间,因此统计[0,100]之间的数字个数即可,比98小的数是[0,97]之间数字的个数之和。Version 3是Version 2的通用版,不用限制数字的大小。Version 4是进一步优化,只统计出现的数字数量。

SnailTyan 197

1365. How Many Numbers Are Smaller Than the Current Number(Leetcode每日一题-2020.10.26)

Problem 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. Constra

Bryan要加油 393

leetcode[1365] How Many Numbers Are Smaller Than the Current Number Python3实现(排序,哈希记录排名)

# 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 v # alid j's such that j != i and nums[j] < nums[i]. # # Return the answer in an array.

zhang35的博客 249

Leetcode 1365. How Many Numbers Are Smaller Than the Current Number [Python]

本质上是考查一个数字在已经排好序的序列中,排到第几位。原来的序列没有排序,所以需要sort一下。 class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: res = [] newnums = nums[::] newnums.sort() for idex, num in enumerate(nums):

sinat_30403031的博客 163

LeetCode 1365. 有多少小于当前数字的数字

原题目:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 思路: 采用计数排序的方法 代码: class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { vector<int> cnt(10..

庾信平生最萧瑟 374

Leetcode每日一题:1365.how-many-numbers-are-smaller-than-the-current-number(有多少小于当前数字的数字)

思路:暴力破解的时间复杂度为O(n2),且这里数组长度不大于500,也就是上万的数量级,肯定不会超时; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int> &nums) { int len = nums.size(); vector<int> res(len, 0); for (int i = 0; .

CodeLuweir 的博客 390

LeetCode 1365. How Many Numbers Are Smaller Than the Current Number

[Easy] LeetCode 链接:https://leetcode.com/problems/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 t...

MrJustin的博客 450

LeetCode:1365. How Many Numbers Are Smaller Than the Current Number 有多少小于当前数字的数字(C语言)

题目描述: 给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。 换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。 以数组形式返回答案。 示例 1: 输入:nums = [8,1,2,2,3] 输出:[4,0,1,1,3] 解释: 对于 nums[0]=8 ...

C/C++攻城狮 413

315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self方法1:BST You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smalle...

wilzxu的博客 336
上一篇: 300. Longest Increasing Subsequence (M)
下一篇: 1366. Rank Teams by Votes (M)
绿色小光头
博客等级 码龄10年 10粉丝 301原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值