当前位置:网站首页>1331. 数组序号转换
1331. 数组序号转换
2022-07-28 10:53:00 【51CTO】
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
- 序号从 1 开始编号。
- 一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。
- 每个数字的序号都应该尽可能地小。
输入:arr = [40,10,20,30]
输出:[4,1,2,3]
解释:40 是最大的元素。 10 是最小的元素。 20 是第二小的数字。 30 是第三小的数字。
解:
去重排序后,使用字典记录每个数字的序号。
有两个小细节,
第一个细节是set也可以用sorted 函数排序。
sorted函数的说明:根据 iterable 中的项返回一个新的已排序列表。
只要是可迭代的对象都可以使用sorted排序,返回结果是一个新的排序列表。
第二个细节是enumerate可以指定序号开始的值,enumerate(sorted_arr,1) 这里就是从1开始计数。
边栏推荐
- 微软安全团队发现一家利用Windows零日漏洞销售间谍软件的奥地利公司
- leetcode:1300. 转变数组后最接近目标值的数组和【二分】
- Advance.ai sailing guide helps enterprises sail to Indonesia and grasp half of the Southeast Asian market
- Two point, three point, 01 point plan [bullet III]
- 分体式测斜探头安装要点及注意事项
- Understand several concepts of Oracle
- ZBrush 2022软件安装包下载及安装教程
- 抖音程序员表白专用代码教程(如何玩抖音)
- JS - 修改数组中对象的键名
- RHEL 6.4 安装svn和apache
猜你喜欢

Machine learning strong foundation plan 0-5: why is the essence of learning generalization ability?

Crm+ zero code: easily realize enterprise informatization

大三下学期总结

Sword finger offer 06. print linked list from end to end

outlook突然变得很慢很卡怎么解决

Here is a super practical excel shortcut set (common + summary of eight categories)

Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult

读懂这6本书,学习MySQL更轻松

leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】

【MySQL从入门到精通】【高级篇】(九)InnoDB的B+树索引的注意事项
随机推荐
18张图,直观理解神经网络、流形和拓扑
Learn how to do e-commerce data analysis (with operation analysis index framework)
keil和IAR中lib库文件的生成和使用
Use the statement object to execute DDL statements to create tables
构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer
DHCP实验演示(Huawei交换机设备配置)
什么是WordPress
Ec20/ec25 4G module at instruction development summary
Game theory 1. Introduction (basic concepts of combination games, confrontation search, bash games, Nim games)
Two point, three point, 01 point plan [bullet I]
The blogs of excellent programmers at home and abroad are all here, please check it
做数据分析,你还不懂RFM分析方法(模型)?
ThinkPad指纹验证在win7无法使用的解决方法
[FPGA tutorial case 41] image case 1 - reading pictures through Verilog
RHEL 6.4 installing SVN and Apache
Technology sharing | quick intercom integrated dispatching system
「Node学习笔记」Koa框架学习
Picture slide effect
RHEL 6.4 安装svn和apache
Nodejs: set up the express service, set up the session and realize the exit operation