当前位置:网站首页>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
边栏推荐
- MySQL事务(transaction) (有这篇就足够了..)
- Use hutool tool class to operate excel with more empty Sheet1
- CV target detection model sketch (2)
- Feign call fails. JSON parse error illegal character ((ctrl-char, code 31)) only regular white space (R
- Network traffic monitoring tool iftop
- 动态规划问题(八)
- CV instance segmentation model sketch (1)
- MySql中的like和in走不走索引
- Advanced area of attack and defense world web masters ics-06
- Field injection is not recommended solution
猜你喜欢

Advanced area of attack and defense world web masters -baby Web

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

MySQL installation and configuration tutorial (super detailed, nanny level)

Simple use and understanding of laravel message queue

AutoCAD -- import excel tables into CAD and merge CAD

递归/回溯刷题(下)

110道 MySQL面试题及答案 (持续更新)

动态规划问题(三)

"Method not allowed", 405 problem analysis and solution

还在写大量 if 来判断?一个规则执行器干掉项目中所有的 if 判断...
随机推荐
Everything you have learned will come in handy at some point in your life (turn)
Installation and use of pnpm
15.模型评估和选择问题
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
Opencv macro definition
Visual full link log tracking
Detailed principle explanation and verification results of digital clock based on FPGA
The difference between {} and ${}
@Detailed explanation of the use of transactional annotation
pnpm的安装与使用
聊聊异步编程的 7 种实现方式
Applet verification code login
最长上升子序列
Plato farm is expected to further expand its ecosystem through elephant swap
Real time data warehouse: Netease strictly selects the practice of real-time data warehouse based on Flink
分布式限流 redission RRateLimiter 的使用及原理
Network traffic monitoring tool iftop
Cause analysis of 12 MySQL slow queries
Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)
Attack and defense world web master advanced area php2