Question

791. Custom Sort String

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的更多相关文章

  1. LeetCode 791. Custom Sort String

    题目链接:https://leetcode.com/problems/custom-sort-string/description/ S and T are strings composed of l ...

  2. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  3. [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 ...

  4. 791. Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  5. 791. Custom Sort String字符串保持字母一样,位置可以变

    [抄题]: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S wa ...

  6. [LeetCode] Custom Sort String 自定义排序的字符串

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Emscripten教程之入门指导

    翻译:云荒杯倾本文是Emscripten-WebAssembly专栏系列文章之一,更多文章请查看专栏.也可以去作者的博客阅读文章.欢迎加入Wasm和emscripten技术交流群,群聊号码:93920 ...

  2. 可想实现一个自己的简单jQuery库?(九)

    Lesson-8 事件机制 在讲事件机制之前呢,我们有一个很重要的东西要先讲,那就是如何实现事件委托(代理). 只有必须先明白了如何实现一个事件委托,我们才能更好的去实现on和off.在我看来,on和 ...

  3. python-成绩转换

    本题要求编写程序将一个百分制成绩转换为五分制成绩.转换规则: 大于等于90分为A: 小于90且大于等于80为B: 小于80且大于等于70为C: 小于70且大于等于60为D: 小于60为E. 输入样例: ...

  4. JavaScript的使用以及JS常用函数(JS 遍历数组和集合)

    JavaScript入门 学习总结 1. 什么是 JavaScript 2. JavaScript 的特点 3. JS的使用 编写位置 基本语法 变量 打印变量 数据类型 innerHTML和inne ...

  5. Eclipse的视窗和视图概述、Eclipse工作空间的基本配置

    Eclipse的视窗和视图概述视窗  每一个基本的窗体被称为视窗 * PackageExplorer  显示项目结构,包,类,及资源 * Outline   显示类的结构,方便查找,识别,修改 * C ...

  6. JavaScript实现科学计算器

    运行效果: 可实现科学计算器的功能,如:PI,sin,cos,tan等 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> ...

  7. css盒子模型、垂直外边距合并

    css盒子模型由四部分组成:内容(content).填充(padding).边框(border).边距(margin),其中css样式中定义的width属性是定义内容区域的宽度,正常情况下,设置了内容 ...

  8. Android实现秒开效果

    0x01 创建SplashActivity 新建一个Activity,取名为SplashActivity 0x02 新建资源 在res/drawable下新建一个splash.xml文件和名为ig_s ...

  9. mycat实现主从读取中的问题

    schema.xml 中的配置如下:..... <dataHost name="aaa" maxCon="2000" minCon="100&q ...

  10. SwitchHosts管理编辑hosts工具

    管理Hosts工具 SwitchHosts 地址: SwitchHosts 开发工程中,针对不同项目设置不同的域名. 办法很多,例如直接编辑hosts文件,通过环境工具提供的功能设置等. 现在要安利一 ...