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

边栏推荐
- 4、 Interface requests data to update input information interactively
- 【数据分析】基于MATLAB实现SVDD决策边界可视化
- Is the software testing training institution reliable?
- What if svchost.exe of win11 system has been downloading?
- 三类6种地图可视化软件测评,最好用的工具居然是它
- Efficiency comparison of JS array splicing push() concat() methods
- 【雷达】基于核聚类实现雷达信号在线分选附matlab代码
- Using CPLEX to solve small-scale energy-efficient FJSP
- How much is software testing training generally?
- BM11 链表相加(二)
猜你喜欢

Module 8 of the construction camp

【数据分析】基于MATLAB实现SVDD决策边界可视化

Libgdx learning path 01: libgdx introduction and running tutorial

What if svchost.exe of win11 system has been downloading?

QT & OpenGL lighting
![[R language - basic drawing]](/img/1e/aebf1cbe02c4574671bac6dc2c9171.png)
[R language - basic drawing]

What is the future of software testing? How to learn?

Pytorch GPU yolov5 reports an error

Youqilin system installation beyondcomare

关于ASM冗余问题
随机推荐
Regular expressions related to face-to-face orders of major express companies in JS
Leetcode skimming - super power 372 medium
N32 replaces STM32. Don't ignore these details!
QT & OpenGL lighting
QT function optimization: QT 3D gallery
[data analysis] realize SVDD decision boundary visualization based on MATLAB
Interpretation of ue4.25 slate source code
RTC clock: a calendar
Introduction and advanced MySQL (7)
Solve the critical path in FJSP - with Matlab source code
Can I get employed after two months of software testing training?
Pytest custom hook function
Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
What if the content of software testing is too simple?
Can zero basis software testing work?
2022 Hangdian multi school field 2 1011 DOS card (line segment tree)
[physical application] atmospheric absorption loss with matlab code
SRS4.0安装步骤
vim学习手册
RFs self study notes (II): theoretical measurement model - without clutter but with detection probability