当前位置:网站首页>力扣解法汇总942-增减字符串匹配
力扣解法汇总942-增减字符串匹配
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
由范围 [0,n] 内所有整数组成的 n + 1 个整数的排列序列可以表示为长度为 n 的字符串 s ,其中:
如果 perm[i] < perm[i + 1] ,那么 s[i] == 'I'
如果 perm[i] > perm[i + 1] ,那么 s[i] == 'D'
给定一个字符串 s ,重构排列 perm 并返回它。如果有多个有效排列perm,则返回其中 任何一个 。
示例 1:
输入:s = "IDID"
输出:[0,4,1,3,2]
示例 2:
输入:s = "III"
输出:[0,1,2,3]
示例 3:
输入:s = "DDI"
输出:[3,2,0,1]
提示:
1 <= s.length <= 105
s 只包含字符 "I" 或 "D"
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/di-string-match
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 先根据字符串的大小关系,对每个位置排个序。 * 例如:IDID, * 则第一个I,则0,1,当前位置为1的位置 * 第二个D,则0,2,1,当前位置为1的位置,因为1比2要大,所以2在前,1在后面。当前位置改为2的位置。 * 第三个I,则0,2,3,1,同理如上 * 第四个D,则0,2,4,3,1 * 拍好顺序后,分别为顺序位置赋值,0在第0位,值为0。2在第1位,值为1,3在第2位,值为2。 * 最终,按照值大小排序即可。结果为0,4,1,3,2
代码:
public class Solution942 {
public int[] diStringMatch(String s) {
List<Integer> list = new ArrayList<>();
char[] chars = s.toCharArray();
int currentIndex = 0;
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
if (i == 0) {
list.add(currentIndex, i);
}
if (aChar == 'I') {
list.add(++currentIndex, i + 1);
} else {
list.add(currentIndex, i + 1);
}
}
Map<Integer, Integer> value = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
value.put(list.get(i), i);
}
int[] ints = new int[list.size()];
for (int i = 0; i < ints.length; i++) {
ints[i] = value.get(i);
}
return ints;
}
}边栏推荐
- Graphic data analysis | business cognition and data exploration
- Installing MySQL version 5.5 database for Linux (centos6)
- Is the bidding price fixed for each click?
- Metaverse × How will smart cities develop?
- [从零开始学习FPGA编程-19]:快速入门篇 - 操作步骤4-1- Verilog 软件下载与开发环境的搭建- Altera Quartus II版本
- Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
- Redis實現消息隊列的4種方案
- Ozzanmation action system based on SSE
- Operating mechanism of Google ads bidding
- In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
猜你喜欢

How to maximize the use of various matching methods—— Google SEM

Graphic data analysis | data cleaning and pretreatment

Installing MySQL version 5.5 database for Linux (centos6)

Basic use of MATLAB

C language programming classic games - minesweeping

初探性能优化!从2个月到4小时的性能提升!

Software engineering - system flow chart

Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?

Huawei, this is too strong

Tiobe - programming language ranking in June 2022
随机推荐
Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
Redis cluster + sentinel mode + replicas
matplotlib. pyplot. Bar chart (II)
Websocket is closed after 10 seconds of background switching
Niuke monthly race 14- simple counting
On the night of the joint commissioning, I beat up my colleagues
力扣解法汇总1022-从根到叶的二进制数之和
如何最大化的利用各种匹配方式? ——Google SEM
Linux(CentOS7)安裝MySQL-5.7版本
Fatal error in launcher: unable to create process using
Don't miss it! Five large data visualization screens that HR must collect
力扣编程题-解法汇总
为什么我们要使用谷歌搜索广告?
国资入股,建业地产这回稳了吗?
Tiobe - programming language ranking in June 2022
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
广泛匹配修饰符符号已经被弃用,请勿使用
MySQL advanced knowledge points
Implementation scheme of iteration and combination pattern for general tree structure
2022最全面的Redis事务控制(带图讲解)