当前位置:网站首页>力扣 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;
}
}

边栏推荐
- 关于ASM冗余问题
- SQL custom automatic calculation
- When unity customizes the editor, let the subclass inherit the inspector display effect of the parent class
- [image segmentation] vein segmentation based on directional valley detection with matlab code
- 优麒麟系统安装BeyondComare
- DevCon. Exe export output to the specified file
- Is it easy to learn the zero foundation of software testing?
- Using CPLEX to solve small-scale energy-efficient FJSP
- MES生产管理系统对设备的应用价值
- Learn from Li Mu, deep learning - linear regression and basic optimization function
猜你喜欢

Understanding of PID
![[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code](/img/a4/5c5a90508e2f9c6b4f8e234bdfdc9e.png)
[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code

SQL审核工具自荐Owls

Applet applet jump to official account page

Kali doesn't have an eth0 network card? What if you don't connect to the Internet

It is the best tool to evaluate six kinds of map visualization software in three categories

2022年暑假ACM热身练习3(详细)

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

The ever-changing pointer ----- C language

How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
随机推荐
GPIO port configuration of K60
Remember a uniapp experience
6-20 vulnerability exploitation proftpd test
Is the software testing training institution reliable?
QT function optimization: QT 3D gallery
Wechat solves the problem of long press selected style
4、 Interface requests data to update input information interactively
How big is it suitable for learning software testing?
Is it easy to learn the zero foundation of software testing?
SQL审核工具自荐Owls
Getting started with QT & OpenGL
Application of time series database in Hydropower Station
Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
优麒麟系统安装BeyondComare
How to adjust the brightness of win11? Four methods of adjusting screen brightness in win11
Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
How long does software testing training take?
ACM warm-up exercise 3 in 2022 summer vacation (detailed)
关于ASM冗余问题
BM14 链表的奇偶重排