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. 在页面未加载完之前显示loading动画

    在页面未加载完之前显示loading动画 这里有很多比这篇博客还优秀的loading动画源码 我还参考这篇博客 loading动画代码demo 我的demo预览 <!DOCTYPE html&g ...

  2. html5知识点补充—mark元素的使用

    使用mark元素高亮文本 利用mark元素,文档作者可以高亮显示文档中的某些文本以达到醒目的效果. 如果用户在站点进行搜索,搜索页面中的关键字可以高亮显示.这时,就可以很好的利用到mark元素.不选用 ...

  3. css3 nth-child选择器

    css3 nth-child选择器 css3的nth-child选择器,乍看起来很简单,其实不是那么容易. 简单用法 p:nth-child(n) // 选择属于其父元素的第n个子元素的每个 < ...

  4. 论文阅读总结-Patient clustering improves efficiency of federated machine learning to predict mortality and hospital stay time using distributed electronic medical records

    一.论文提出的方法: 使用进入ICU前48h的用药特征作为预测因子预测重症监护患者的死亡率和ICU住院时间. 用到了联邦学习,自编码器,k-means聚类算法,社区检测. 数据集:从50家患者人数超过 ...

  5. JavaScript实现简单轮播图动画

    运行效果: 源代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset=&quo ...

  6. Python入门-程序测试

    1.功能测试 常规测试 #常规测试代码,一个模块写功能,一个模块调用功能 #=============模块1:gongneng_ceshi def func(v1, v2): return v1* v ...

  7. Linux操作系统与项目部署

    Linux操作系统与项目部署 注意:本版块会涉及到操作系统相关知识. 现在,几乎所有智能设备都有一个自己的操作系统,比如我们的家用个人电脑,基本都是预装Windows操作系统,我们的手机也有Andro ...

  8. 使用 LOAD DATA LOCAL INFILE,sysbench 导数速度提升30%

    1. LOAD DATA INFILE 为什么比 INSERT 快? 2. sysbench 压测 MySQL 的四个标准步骤. 3. 怎么让 sysbench 支持 LOAD DATA LOCAL ...

  9. 7.Arrays类

    1. Arrays类 数组的工具类java.util.Arrays Arrays类中的方法都是static修饰的静态方法,使用的时候可以直接使用类名进行调用 (而不是使用对象)(是"不用&q ...

  10. 都2022年了,HDFS为何还如此能战!

    摘要:HDFS也许不是最好的大数据存储技术,但依然是最重要的大数据存储技术. 本文分享自华为云社区<HDFS为何在大数据领域经久不衰?>,作者: JavaEdge. 1.概述 1.1 简介 ...