当前位置:网站首页>力扣解法汇总1331-数组序号转换
力扣解法汇总1331-数组序号转换
2022-07-28 13:26:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:
描述:
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
序号从 1 开始编号。
一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。
每个数字的序号都应该尽可能地小。
示例 1:
输入:arr = [40,10,20,30]
输出:[4,1,2,3]
解释:40 是最大的元素。 10 是最小的元素。 20 是第二小的数字。 30 是第三小的数字。
示例 2:
输入:arr = [100,100,100]
输出:[1,1,1]
解释:所有元素有相同的序号。
示例 3:
输入:arr = [37,12,28,9,100,56,80,5,12]
输出:[5,3,4,2,8,6,7,1,3]
提示:
0 <= arr.length <= 105
-109 <= arr[i] <= 109
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rank-transform-of-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 复制一个arr,为copyArr,对其进行排序。 * 排序后遍历,生成一个map,key为每个的值,value为其排序的序号。 * 然后遍历arr,从map中找到key对应的序号进行替换。
代码:
public class Solution1331 {
public int[] arrayRankTransform(int[] arr) {
int[] copyArr = new int[arr.length];
System.arraycopy(arr, 0, copyArr, 0, arr.length);
Arrays.sort(copyArr);
HashMap<Integer, Integer> map = new HashMap<>();
Integer lastNum = null;
for (int i = 0; i < copyArr.length; i++) {
int value = copyArr[i];
if (lastNum != null && lastNum == value) {
continue;
}
lastNum = value;
map.put(value, map.size() + 1);
}
for (int i = 0; i < arr.length; i++) {
int value = arr[i];
arr[i] = map.get(value);
}
return arr;
}
}边栏推荐
- Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)
- Nport serial server configuration website (whether the serial server is from network port to serial port)
- AFNetworking速成教程
- UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing
- Read how to deploy highly available k3s with external database
- What is gossip (E-Net gossip)
- Entering the world of audio and video -- flv video packaging format
- Slam thesis collection
- Security assurance is based on software life cycle -istio authorization mechanism
- Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
猜你喜欢

朗镜科技(Trax中国)“机器人+AI”开启中国零售元宇宙时代

IP black and white list

Multithreading and high concurrency (III) -- source code analysis AQS principle

MySQL开发技巧——视图

Daily question - Scholarship

Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)

Security assurance is based on software life cycle - networkpolicy application

Security assurance is based on software life cycle -istio authorization mechanism

Multi level cache scheme

IP黑白名单
随机推荐
Literature reading (245) roller
目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)
[ecmascript6] proxy and reflection
The default storage engine after MySQL 5.5 is InnoDB.
LeetCode 0142.环形链表 II
Development and definition of software testing
Install mysql5.7.36 in CentOS
zabbix分布式
Mobile phone scrolling screenshot software recommendation
Do you really know esmodule
MVC模型:日历系统
bgp实验
QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
Istio IV fault injection and link tracking
Master closures and consolidate basic skills
QQ robot configuration record based on nonebot2
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
协同办公工具:在线白板初起步,在线设计已红海
【Utils】JsonUtil
【LeetCode】1331. 数组序号转换