当前位置:网站首页>1331. 数组序号转换 : 简单模拟题
1331. 数组序号转换 : 简单模拟题
2022-07-28 11:31:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 1331. 数组序号转换 ,难度为 简单。
Tag : 「模拟」、「哈希表」、「排序」
给你一个整数数组 arr,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
序号从 开始编号。 一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。 每个数字的序号都应该尽可能地小。
示例 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]
提示:
模拟
根据题意进行模拟即可。
对 arr 进行拷贝操作得到新数组 clone,对 clone 数组进行排序,再使用哈希表记录元素与序号的映射关系,最后根据映射关系构造答案。
Java 代码:
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 代码:
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
};
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.1331 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- Gecko competition 2.0 is new! Come and show your flexible operation skills!
- First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released
- 用C语言开发NES游戏(CC65)07、控制器
- Launcher sample code
- IRBuilder
- Zadig v1.13.0 believes in the power of openness, and workflow connects all values
- Brief discussion on open source OS distribution
- 行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
- Arduino Pro Mini atmega328p connect and light the first LED (at the same time, record the problem of burning failure stk500_recv)
- Ruiji takeout - day01
猜你喜欢

Style conversion model style_ Transformer project instance pytorch implementation

Newly released, the domestic ide developed by Alibaba is completely open source

OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
![[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou

QT writing IOT management platform 42 data query export print

Great! Jd.com developed the highly available website construction technology PDF recommended by the first brother. Prepare the water and chew it slowly
![[real question of written examination]](/img/3f/e061df6a2c5c92429cfd3c69cc94ce.png)
[real question of written examination]

华为发布HarmonyOS 3及全场景新品,智慧体验更进一步

LyScript 获取上一条与下一条指令

Live: never believe that suffering is worth it. Suffering is suffering
随机推荐
Idea replication module
Exploration on cache design optimization of community like business
SQL注入 Less24(二次注入)
Great! Jd.com developed the highly available website construction technology PDF recommended by the first brother. Prepare the water and chew it slowly
php⽉份加减最简单的处理⽅法
聚变云原生,赋能新里程 | 2022 开放原子全球开源峰会云原生分论坛圆满召开
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
IRBuilder
Developing NES games with C language (cc65) 04. Complete background
ViewPager2+Fragment
WebView详解
With the continuous waves of infringement, the U.S. patent and trademark office began to study the impact of NFT on copyright
IDEA复制模块
产学研用 共建开源人才生态 | 2022 开放原子全球开源峰会教育分论坛圆满召开
Tencent two sides: @bean and @component are used in the same class, what will happen?
用arduino开发ESP8266 搭建开发环境
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
Using Arduino to develop esp8266 to build a development environment
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
太赞了!京东研发一哥力荐的高可用网站构建技术PDF,备好水,慢慢啃