[LeetCode] 1370. Increasing Decreasing String
1. 原题链接:https://leetcode.com/problems/increasing-decreasing-string/
2. 解题思路
- 直观的想法是:用有序map<char, int>记录字母和字母出现的次数
- 按照题目描述的方式,先顺序遍历map,再反序遍历map,直到map中的所有字母出现次数都为0为止
3. 算法
- 遍历字符串s,通过有序map<char, int>记录字母和字母出现的次数
- 设置标志forward表示当前是正向遍历map,还是反向遍历map
- 遍历map的过程中,如果字母X的出现次数不为0,则将该字母加入结果字符串中,并将该字母的出现次数减一;否则,字母X的出现次数是0,表示该字母已经都被遍历了,并且已经都加入到结果字符串中
- 如果在某一次遍历完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的更多相关文章
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...
- 【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 ...
- [LeetCode] Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [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 ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LeetCode之344. Reverse String
------------------------------- Java也可以实现一行代码反转字符串哦 AC代码如下: public class Solution { public String re ...
随机推荐
- Linux/UNIX 上安装 MySQL
Linux/UNIX 上安装 MySQL Linux平台上推荐使用RPM包来安装Mysql,MySQL AB提供了以下RPM包的下载地址: MySQL - MySQL服务器.你需要该选项,除非你只想连 ...
- git基本操作-长期维护
############### 初识git ############## """ git初识: 1,git是什么?git是一个帮助用户版本控制的软件, 2,g ...
- 解决2013Lost connection to MySQL server during query错误方法
在my.ini配置文件 mysqld 节点下添加 max_allowed_packet = 500M 也就是配置MySQL允许的最大数据包大小,上面的500M你可以根据你的项目修改为你自己的值,只要比 ...
- vue 起步(一)
准备 安装nodejs(下载),Windows 安装包(.msi) npm相关 打开cmd查看npm版本, npm -v,如果没有安装npm,执行npm install npm -g进行安装 查询当前 ...
- linux查看并发连接数
1.查看TCP的并发请求数及其TCP连接状态: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' netstat ...
- Derby 命令
SHOW [ TABLES | VIEWS | PROCEDURES | FUNCTIONS | SYNONYMS ] { IN sche -- 列出表.视图.过程.函数或同义词 SHOW INDEX ...
- python3下scrapy爬虫(第十一卷:scrapy数据存储进mongodb)
说起python爬虫数据存储就不得不说到mongodb,现在我们来试一下scrapy操作mongodb 首先开启mongodb mongod --dbpath=D:\mongodb\db 开启服务后就 ...
- vue-cli3初始化项目
1 npm install -g @vue/cli 创建配置 创建 1 vue create vue-app 选择配置 1234 ? Please pick a preset: (Use arrow ...
- UFT基本操作
1.打开界面F6快捷录制 2.选择web或者C/S架构软件 3.以C/S为例,点击添加找到相应的地址 4.点击左键添加断点 5.切换视图,初级模式或者代码模式 6.新增步骤 7.点击“手指”图标获取元 ...
- ISBN|方正|超星|The national academies press|OECD|RSC|Springer Link|Knovel|Encyclopedia Britannica
图书使用图书分类号ISBN作为图书的ID 大英百科全书(Encyclopedia Britannica)可用于找寻关键词或关键词相关信息,便于构建准确的检索式: Knovel:可使用物理化学性质查找相 ...