1. 原题链接:https://leetcode.com/problems/increasing-decreasing-string/

2. 解题思路

  1. 直观的想法是:用有序map<char, int>记录字母和字母出现的次数
  2. 按照题目描述的方式,先顺序遍历map,再反序遍历map,直到map中的所有字母出现次数都为0为止

3. 算法

  1. 遍历字符串s,通过有序map<char, int>记录字母和字母出现的次数
  2. 设置标志forward表示当前是正向遍历map,还是反向遍历map
  3. 遍历map的过程中,如果字母X的出现次数不为0,则将该字母加入结果字符串中,并将该字母的出现次数减一;否则,字母X的出现次数是0,表示该字母已经都被遍历了,并且已经都加入到结果字符串中
  4. 如果在某一次遍历完map后,所有的字母出现次数都是0,表示整个字符串已经被遍历完了,此时可以退出循环,返回结果字符串(PS:其实退出条件也可以是:当结果字符串的长度和原字符串长度相同时,可以退出循环)

4. 实现

class Solution {
public:
string sortString(string s) {
map<char, int> table;
for(auto c : s){
table[c] = table[c] + 1;
}
string res;
bool forward = 1;
while(table.size() != 0){
bool finish = true;
if(forward){
map<char, int>::iterator begin = table.begin();
map<char, int>::iterator end = table.end();
forward = 0;
for(; begin != end; begin++){
if(begin->second == 0){
continue;
}
finish = false;
res += begin->first;
begin->second = begin->second - 1;
}
}else{
map<char, int>::reverse_iterator begin = table.rbegin();
map<char, int>::reverse_iterator end = table.rend();
forward = 1;
for(; begin != end; begin++){
if(begin->second == 0){
continue;
}
finish = false;
res += begin->first;
begin->second = begin->second - 1;
}
}
if(finish)
break;
//if(res.size() == s.size()) //另一种退出条件
// break;
}
return res;
}
};
//巧妙解法
//该解法有个限制条件:s中出现的字符排序规则必须跟map对这些字符的排序规则保持一致
class Solution {
public:
string sortString(string s) {
string ans;
map<char,int > cnt;
for(auto item:s)cnt[item]++;
while(1){
for(char ch='a';ch<='z';++ch)
if(cnt[ch]!=0)cnt[ch]--,ans.push_back(ch);
for(char ch='z';ch>='a';--ch)
if(cnt[ch]!=0)cnt[ch]--,ans.push_back(ch);
if(ans.size()==s.size())break;
}
return ans;
}
};

[LeetCode] 1370. Increasing Decreasing String的更多相关文章

  1. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...

  5. 【leetcode】926.Flip String to Monotone Increasing

    题目如下: A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possib ...

  6. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  7. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  8. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. LeetCode之344. Reverse String

    ------------------------------- Java也可以实现一行代码反转字符串哦 AC代码如下: public class Solution { public String re ...

随机推荐

  1. PAT甲级——1061 Dating

    1061 Dating Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2 ...

  2. python中的if not

    在python中 None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False ,即: not None == not False = ...

  3. WebFilter 在springBoot工程中不起作用

    [1]@ServletComponentScan 必须有一个注解将带有@WebFilter的类包含进去. [2]自定义 FiltersConfig extends WebMvcConfigurerAd ...

  4. 同步linux系统时间

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  5. 安装centos7后不能联网

    我们在安装centos的minimal版本后,在使用yum安装工具时会提示:cannot find a valid baseurl or repo:base/7/x86_64 这是因为不能联网导致的, ...

  6. ionic2踩坑之自定义插件开发及调用

    关于ionic2自定义插件开发的文章,插件怎么调用的文章,好像网上都有,不过作为一个新手来说,从插件的开发到某个页面怎么调用,没有一个完整的过程的话,两篇没有关联的文章也容易看的迷糊.这里放到一起来方 ...

  7. URL与URI与URN的区别与联系

    1.什么是URL? 统一资源定位符(或称统一资源定位器/定位地址.URL地址等[1],英语:Uniform Resource Locator,常缩写为URL),有时也被俗称为网页地址(网址).如同在网 ...

  8. 通过samus驱动实现基本数据操作

    传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由(database).集合(collection).文档对象(documen ...

  9. H5本地离线存储

    前言上一篇文件结尾,有同学问我本地存储图片方法,其实本地存储方式有很多,我们打开谷歌浏览器,查看源代码,在resources页签中,有web SQl ,indexedDB等等,我前面文章讲过Local ...

  10. JSTL标签常用

    JSTL简介: 标准标签库JSTL的全名为:Java Server Pages Standard Tag Library. JSTL主要提供了5大类标签库: 1.       核心标签库: 为日常任务 ...