当前位置:网站首页>Force deduction solution summary 1331 array sequence number conversion
Force deduction solution summary 1331 array sequence number conversion
2022-07-28 14:24:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link :
describe :
Give you an array of integers arr , Please replace each element in the array with their ordinal number after sorting .
The serial number represents how big an element is . The rules for serial numbers are as follows :
The serial number from 1 Numbered starting .
The bigger an element is , So the bigger the serial number . If two elements are equal , So they have the same serial number .
The serial number of each number should be as small as possible .
Example 1:
Input :arr = [40,10,20,30]
Output :[4,1,2,3]
explain :40 It's the biggest element . 10 It's the smallest element . 20 It's the second smallest number . 30 It's the third smallest number .
Example 2:
Input :arr = [100,100,100]
Output :[1,1,1]
explain : All elements have the same sequence number .
Example 3:
Input :arr = [37,12,28,9,100,56,80,5,12]
Output :[5,3,4,2,8,6,7,1,3]
Tips :
0 <= arr.length <= 105
-109 <= arr[i] <= 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/rank-transform-of-an-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Copy a arr, by copyArr, Sort it . * After sorting, traverse , Generate a map,key Value for each ,value The sequence number to sort . * Then traverse arr, from map Find key Replace with the corresponding serial number .
Code :
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;
}
}边栏推荐
- 多所“双一流”大学,保研预报名启动!
- These three online PS tools should be tried
- Solve the problem that uniapp wechat applet canvas cannot introduce fonts
- Niuke multi school link with level editor i- (linear DP)
- [lvgl events] event code
- [translation] how to choose a network gateway for your private cloud
- [try to hack] hfish honeypot deployment
- [server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
- unittest执行runTestCase提示<_io.TextIOWrapper name=‘<stderr>‘ mode=‘w‘ encoding=‘utf-8‘>解决方案
- 复制excel行到指定行
猜你喜欢
随机推荐
[ecmascript6] set and map
Using reflection to build a menu spanning tree
PowerDesigner creates a database model (conceptual model example)
LeetCode 1331.数组序号转换
【Util】redis工具类:把redis的value序列化器修改为GenericJackson2JsonRedisSerializer,就支持返回值为对象或集合了
[ecmascript6] async and await
What is a spin lock? A spin lock means that when a thread attempts to acquire a lock, if the lock has been occupied by other threads, it will always cycle to detect whether the lock has been released,
Clickhouse distributed cluster construction
QQ robot configuration record based on nonebot2
HCIP第十天
[leetcode] 1331. Array sequence number conversion
【Utils】JsonUtil
文献阅读(245)Roller
Revised version | target detection: speed and accuracy comparison (faster r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)
力扣解法汇总1331-数组序号转换
Three methods to disassemble the rotation array
创建线程池的四种方式
[translation] how to choose a network gateway for your private cloud
软件测试工程师的职业规划
Copy excel row to specified row







