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 ...
随机推荐
- 13_Invariance Principle_LaSalle's Theorem_不变性原理
- 菜鸟的谷歌浏览器devtools日志分析经验
1 别管什么性能,尽可能输出详细的必要日志.(除非你明显感觉到性能变低,而且性能变低的原因是由于日志输出太多而引起的) 2 不要总是使用console.log,试试console.info, cons ...
- JavaScript 工作原理之三-内存管理及如何处理 4 类常见的内存泄漏问题(译)
原文请查阅这里,本文有进行删减,文后增了些经验总结. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第三章. 我们将会讨论日常使用中另一个被开发者越来越忽略的 ...
- ES6-11学习笔记--对象的扩展
属性简洁表示法 属性名表达式 Objec.is() 扩展运算符 与 Object.assign() in 对象的遍历方式 属性简洁表示法: 如果属性key跟变量名一样,可简写 let name = ...
- 前端面试题整理——手写方法解析URL参数
//拆分字符串形式 function queryToObj() { const res = {} const search = location.search.substr(1);//去掉前面的&qu ...
- java对象有什么重要的?
3.历史上讲,对象有什么重要的? [新手可忽略不影响继续学习]早期的编程主要是面向过程的编程,处理的问题都相对的简单,比较过程化,换句话说,就是一步一步从开始到结束,比如第一步进入电梯,第二步关门, ...
- Java中jdk安装与环境变量配置
Java中jdk安装与环境变量配置 提示:下面是jdk1.7和jdk1.8的百度网盘链接 链接:https://pan.baidu.com/s/1SuHf4KlwpiG1zrf1LLAERQ 提取码: ...
- [转]Fabric2.3中使用test-network搭建测试网络
这个测试网络一方面可以用来学习Fabric,另一方面也可以让一些更有经验的开发者来测试他们的智能合约和应用,但是不建议用于生产环境,在2.0版本后,这个测试网络也取代了原来的"first-n ...
- mysql-cluster-gpl-7.5.10-linux-glibc2.12-x86_64.tar.gz (有必要解释一下)
大部分软件我们接触的时候会发现,起的名称有点怪异,所以我觉得有必要解释一下. 比如: mysql-cluster-gpl-7.5.10-linux-glibc2.12-x86_64.tar.gz 名称 ...
- Leetcode216/39/40/77之回溯解决经典组合问题
Leetcode216-组合总和三 找出所有相加之和为 n 的 k 个数的组合,且满足下列条件: 只使用数字1到9 每个数字 最多使用一次 返回 所有可能的有效组合的列表 .该列表不能包含相同的组合两 ...