当前位置:网站首页>1331. Array sequence number conversion: simple simulation question
1331. Array sequence number conversion: simple simulation question
2022-07-29 00:25:00 【Gong Shui Sanye's Diary of writing questions】
Title Description
This is a LeetCode Upper 1331. Array number conversion , The difficulty is Simple .
Tag : 「 simulation 」、「 Hashtable 」、「 Sort 」
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 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 :
simulation
Simulate according to the meaning of the question .
Yes arr
Copy to get a new array clone
, Yes clone
Array to sort , Then use the hash table to record the mapping relationship between elements and sequence numbers , Finally, the answer is constructed according to the mapping relationship .
Java Code :
class Solution {
public int[] arrayRankTransform(int[] arr) {
int[] clone = arr.clone();
Arrays.sort(clone);
Map<Integer, Integer> map = new HashMap<>();
int n = arr.length, idx = 0;
for (int i : clone) {
if (!map.containsKey(i)) map.put(i, ++idx);
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) ans[i] = map.get(arr[i]);
return ans;
}
}
TypeScript Code :
function arrayRankTransform(arr: number[]): number[] {
let clone = new Array<number>()
for (const i of arr) clone.push(i)
clone.sort((a,b)=>a-b)
let n = arr.length, idx = 0
let map = new Map<number, number>()
for (const i of clone) {
if (!map.has(i)) map.set(i, ++idx)
}
let ans = new Array<number>()
for (let i = 0; i < n; i++) ans.push(map.get(arr[i]))
return ans
};
Time complexity : Spatial complexity :
Last
This is us. 「 Brush through LeetCode」 The first of the series No.1331
piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
More, more, more popular 「 written examination / interview 」 Relevant information can be found in the beautifully arranged Gather new bases
This paper is written by mdnice Multi platform Publishing
边栏推荐
- Oracle超全SQL,细节狂魔
- Cmake basic learning
- CV target detection model sketch (2)
- 动态规划问题(七)
- Where is sandbox's confidence in rejecting meta's acquisition of meta universe leader sand?
- Dynamic programming (V)
- Basic knowledge of PHP language (super detailed)
- 跳表的原理
- Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)
- 还在写大量 if 来判断?一个规则执行器干掉项目中所有的 if 判断...
猜你喜欢
Field injection is not recommended solution
Install MySQL using Yum for Linux
Dynamic programming problem (VIII)
What does the expression > > 0 in JS mean
Control fillet stroke materialshapedrawable
Sword finger offer 41. median in data flow
Principle of meter skipping
Alibaba Code代码索引技术实践:为Code Review提供本地IDE的阅读体验
Concurrency in go
Advanced area of attack and defense world web masters unserialize3
随机推荐
Web系统常见安全漏洞介绍及解决方案-sql注入
2022 network security learning route is very detailed, recommended Learning
Erc20 Standard Code
Sword finger offer 64. find 1+2+... +n, logical operator short circuit effect
mysql中exists的用法详解
Google browser, no installation required
Dynamic programming problem (VII)
Dynamic programming problem (3)
动态规划问题(四)
@Transactional 注解使用详解
Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)
Field injection is not recommended solution
Attack and defense world web master advanced area web_ php_ unserialize
17.机器学习系统的设计
vscode下链接远程服务器安装插件失败、速度慢等解决方法
PTA (daily question) 7-70 diamond
2022网络安全学习路线 非常详细 推荐学习
Idea error running 'application' command line is too long solution
动态规划问题(三)
DCAT in laravel_ Admin preliminary use record