当前位置:网站首页>力扣 1331. 数组序号转换
力扣 1331. 数组序号转换
2022-07-28 17:21:00 【冷酷的摸鱼小将】
题目
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
序号从 1 开始编号。
一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。
每个数字的序号都应该尽可能地小。
示例
输入:arr = [40,10,20,30]
输出:[4,1,2,3]
解释:40 是最大的元素。 10 是最小的元素。 20 是第二小的数字。 30 是第三小的数字。
输入:arr = [100,100,100]
输出:[1,1,1]
解释:所有元素有相同的序号。
输入:arr = [37,12,28,9,100,56,80,5,12]
输出:[5,3,4,2,8,6,7,1,3]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rank-transform-of-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法1:排序+哈希表
Java实现
class Solution {
public int[] arrayRankTransform(int[] arr) {
int n = arr.length;
int[] arr2 = new int[n];
arr2 = arr.clone();
Arrays.sort(arr2);
Map<Integer, Integer> map = new HashMap<>();
int flag = 1;
for (int i = 0; i < n; i++) {
if (!map.containsKey(arr2[i])) map.put(arr2[i], flag++);
}
for (int i = 0; i < n; i++) {
arr[i] = map.get(arr[i]);
}
return arr;
}
}

边栏推荐
- QT - CPP database operation
- 3、 Uni app fixed or direct to a certain page
- 架构实战营第8模块作业
- Application of time series database in intelligent power consumption field
- Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
- MES生产管理系统对设备的应用价值
- As for the white box test, you have to be skillful in these skills~
- PyG搭建异质图注意力网络HAN实现DBLP节点预测
- Accumulation and development -- the way of commercialization of open source companies
- The login interface of modern personal blog system modstartblog v5.4.0 has been revised and the contact information has been added
猜你喜欢

3、 Uni app fixed or direct to a certain page

4、 Interface requests data to update input information interactively

The open source of "avoiding disease and avoiding medicine" will not go far

cv5200无线WiFi通信模块,视频图像传输无线化,实时无线通信技术

QT & OpenGL lighting

GPIO port configuration of K60

FTM module of K60: configure motor, encoder and steering gear

How to write a JMeter script common to the test team

Application of time series database in bridge monitoring field

BM16 delete duplicate elements in the ordered linked list -ii
随机推荐
Solve the critical path in FJSP - with Matlab source code
Application of time series database in Hydropower Station
一家芯片公司倒在了B轮
6-20漏洞利用-proftpd测试
Pytest custom hook function
Why did wechat change from "small and beautiful" to "big and fat" when it expanded 575 times in 11 years?
Libgdx learning road 02: draw game map with tiled
Is it easy to learn the zero foundation of software testing?
SQL custom automatic calculation
1、 My first wechat applet
QT - CPP database operation
Module 8 of the construction camp
What if the content of software testing is too simple?
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
BM16 delete duplicate elements in the ordered linked list -ii
关于ASM冗余问题
VIM learning manual
【物理应用】大气吸收损耗附matlab代码
JDBC简单封装
Introduction and advanced MySQL (7)