IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> LeetCode.791 Custom Sort String(自定义字符串排序) -> 正文阅读

[数据结构与算法]LeetCode.791 Custom Sort String(自定义字符串排序)

1.题目

You are given two strings order and s. All the words of?order?are?unique?and were sorted in some custom order previously.

Permute the characters of?s?so that they match the order that?order?was sorted. More specifically, if a character?x?occurs before a character?y?in?order, then?x?should occur before?y?in the permuted string.

Return?any permutation of?s?that satisfies this property.

2.样例

- 样例1
Input: order = "cba", s = "abcd"
Output: "cbad"
Explanation:?
"a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".?
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
- 样例2
Input: order = "cbafg", s = "abcd"
Output: "cbad"

- 限制条件

  • 1 <= order.length <= 26
  • 1 <= s.length <= 200
  • order?and?s?consist of lowercase English letters.
  • All the characters of?order?are?unique.

?3.题解

- 题目:的意思:给定字符串order(其中每个字符都是唯一的,即只有26个小写字母),按照oder中的每个字母出现的顺序,我们需要对s字符串也需按照在order中的字母排序进行排序(返回一种排序即可)。

- 代码:

class Solution {
    public String customSortString(String order, String s) {
        // 思路:对order字符串的顺序作为模版,将s的字符串分别统计对应的次数,匹配到order没有相关字符时说明已经匹配完了,直接将剩下的字符按照顺序拼接即可
        if(order==null||order.length()==0||s==null||s.length()==0){
            return null;
        }
        
        int []times=new int[256];
        char [] chs=s.toCharArray();
        for(char ch:chs){
            times[ch]++;
        }
        
        char [] templateChs=order.toCharArray();
        char [] res=new char[chs.length];
        int index=0;
        for(char ch:templateChs){
            if(times[ch]==0){
                continue;
            }
            
            while(times[ch]>0){
                res[index++]=ch;
                times[ch]--;
            }
        }
        
        for(int i=0;i<times.length;i++){
            while(times[i]>0){
                res[index++] = (char) i;
                times[i]--;
            }
        }
        return String.valueOf(res);
    }
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-25 11:55:27  更:2021-07-25 11:56:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 2:20:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码