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. 10.Flink实时项目之订单维度表关联

    1. 维度查询 在上一篇中,我们已经把订单和订单明细表join完,本文将关联订单的其他维度数据,维度关联实际上就是在流中查询存储在 hbase 中的数据表.但是即使通过主键的方式查询,hbase 速度 ...

  2. can总线第二讲

    一 CAN总线拓扑结构CAN是一种分布式的控制总线,总线上的每一个节点一般来说都比较简单,使用MCU控制器处理CAN总线数据,完成特定的功能:通过CAN总线将各节点连接只需较少的线缆(两根线:CAN_ ...

  3. 前端基础问题整理-HTML相关

    DOCTYPE的作用以及常见的DOCTYPE类型 <!DOCTYPE>声明位于文档中的最前面的位置,处于 <html> 标签之前,用来告知浏览器页面目前的文件是用哪种版本的HT ...

  4. 微信小程序&mpvue问题总结(1)

    微信小程序进入到首页的时候,日志打印出"created", "onlaunch", "mounted",具体代码如下:那么,在小程序中 cr ...

  5. 小程序刷新webview小结

    场景 在小程序其它页面做了操作,数据发生改变,回到webview页面时需要更新webview里面的数据.由于小程序没有提供与webview的实时通信能力,因此刷新页面是个可考虑的做法. 方法一 最常见 ...

  6. 安卓性能测试之 adb shell 常用命令

    pm list packages 列出包名adb shell pm list packages:列出所有的包名.adb shell dumpsys package:列出所有的安装应用的信息adb sh ...

  7. jboss 7.1.1.final 报错 set the maxParameterCount attribute on the Connector

    Therefore, I cannot just add the connector attribute in standalone.xml like so: 在 <JBOSS_HOME> ...

  8. Java安全之Commons Collections6分析

    Java安全之Commons Collections6分析 0x00 前言 其实在分析的几条链中都大致相同,都是基于前面一些链的变形,在本文的CC6链中,就和前面的有点小小的区别.在CC6链中也和CC ...

  9. 如何在 Java 中实现无向环和有向环的检测

    无向环 一个含有环的无向图如下所示,其中有两个环,分别是 0-2-1-0 和 2-3-4-2: 要检测无向图中的环,可以使用深度优先搜索.假设从顶点 0 出发,再走到相邻的顶点 2,接着走到顶点 2 ...

  10. Spring-注入方式(基于xml方式)

    1.基于xml方式创建对象 <!--配置User类对象的创建 --> <bean id="user" class="com.at.spring5.Use ...