791. Custom Sort String - LeetCode
Question

Solution
题目大意:给你字符的顺序,让你排序另一个字符串。
思路:
输入参数如下:
S = "cba"
T = "abcd"
先构造一个map,sMap
key存储S中出现的字符,value存储字符在S中的位置
c -> 0
b -> 1
a -> 2
再构造一个int数组,sIdx
sIdx,存储S中的字符在T字符串中出现的次数
遍历T字符串
如果字符在sMap中,sIdx就加1
如果不存在,就直接加入到返回字符串
最后遍历sIdx数组,将S中的字符加入到返回字符串
Java实现:
public String customSortString(String S, String T) {
Map<Character, Integer> sMap = new HashMap<>();
int[] sIdx = new int[S.length()];
for (int i = 0; i < S.length(); i++) {
sMap.put(S.charAt(i), i);
sIdx[i] = 0;
}
String retStr = "";
for (char c : T.toCharArray()) {
if(sMap.containsKey(c)) {
sIdx[sMap.get(c)] += 1;
continue;
};
retStr += String.valueOf(c);
}
for (int i = 0; i < S.length(); i++) {
while (sIdx[i]-- > 0) {
retStr += S.charAt(i);
}
}
return retStr;
}
791. Custom Sort String - LeetCode的更多相关文章
- LeetCode 791. Custom Sort String
题目链接:https://leetcode.com/problems/custom-sort-string/description/ S and T are strings composed of l ...
- 【LeetCode】791. Custom Sort String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...
- [leetcode]791. Custom Sort String自定义排序字符串
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 791. Custom Sort String
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 791. Custom Sort String字符串保持字母一样,位置可以变
[抄题]: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S wa ...
- [LeetCode] Custom Sort String 自定义排序的字符串
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 73th LeetCode Weekly Contest Custom Sort String
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- [Swift]LeetCode791. 自定义字符串排序 | Custom Sort String
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- [Javascript] Use a custom sort function on an Array in Javascript
Sorting in Javascript with sort uses lexical sorting by default, which means it will sort in alphabe ...
随机推荐
- 关于Css的垂直居中的一些方法
前两种方法称为大致居中,一般误差随高度的减小而减小,不过一般来说不怎么看得出来,除非你用javascript调用offsetTop来查看.不然没有强迫症的比较难看出来.但是兼容性很好,尤其是table ...
- 推荐简约漂亮的小程序插件 calendar
公司团队制作,主要用于内部使用,觉得这个感觉不错,所以推荐出来,让大家试试~ 日历功能 日历基本功能,自定义样式 先睹为快 使用方法: 1. 在微信小程序管理后台--设置--第三方服务,按 AppID ...
- 实现拖拽复制和可排序的react.js组件
在实现复制前,对之前的拖拽排序组件属性进行了修改. 摒弃了value中的content属性,拖拽组件暴露的render函数,利用这个属性进行组件内部子组件的渲染,这点主要是参考了蚂蚁金服的Ant de ...
- centOS一次性更新所有依赖库
CentOS中使用yum一次性更新安装依赖库 [tom@localhost /]# sudo -s [root@localhost /]# LANG=C [root@localhost /]# yum ...
- FinClip 黑客马拉松正式开赛,码力集结,等你来战!
从2017到2022,小程序已经走过了5年的光景.从无人问津到互联网巨头纷纷入局,短短数年间,小程序已然发展成为超级 App 的标配!微信.支付宝.百度.抖音.今日头条--这些超级app的背后都有巨量 ...
- Quartz 使用记录
Quartz 使用记录 官网 https://www.quartz-scheduler.org/ 参考文档 Quartz 2.3.0 什么是 Quartz? 官方描述: Quartz is a ric ...
- Educational Codeforces Round 116 (Rated for Div. 2), problem: (C) Banknotes
传送门 Problem - C - Codeforces 题目 题目重点内容手打翻译:(欢迎批评指正) 在柏林, 使用着n套不同的货币(banknotes).第i个货币面额为10ai 元,货币的第一种 ...
- HCIE笔记-第七节-ICMP+ARP
ICMP重定向 作用:解决网络中的次优路径 触发:当某一个设备收到一个数据,进行转发时发现还要从该接口进行转发,于是触发ICMP重定向. 报文:Type=5,Code=0 ARP -- 地址解析协议 ...
- SSM阶段学习-mybatis第一天
首先今天我尝试了使用IDEA软件链接数据库,创建数据库,创建表. 在pom文件下导入maven坐标 [<?xml version="1.0" encoding="U ...
- thinkphp6事件监听event-listene
事件系统可以看成是行为系统的升级版,相比行为系统强大的地方在于事件本身可以是一个类,并且可以更好的支持事件订阅者. 事件相比较中间件的优势是事件比中间件更加精准定位(或者说粒度更细),并且更适合一些业 ...