[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 ...
随机推荐
- pytest+allure高端测试报告
1.下载jdk,搭建jdk环境 下载JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.下载allure ...
- icloud/onenote/onedrive/microoutlook/百度云账号都是怎么回事(未完成)
在用一些跨机器跨平台软件的时候,一旦换了电脑或者手机,我经常出现蒙的状态,不知道怎么同步或者使用了,在这里总结一下. 一.icloud账号 当我们买iphone或者macbook的时候,一开机它会让你 ...
- B 小雨的三角形
题目链接:https://ac.nowcoder.com/acm/contest/949/B 思路: 一个找规律题,找到规律就很简单,只剩下代码实现了.规律:第i行去头尾剩下的数的和等于第i-1行去头 ...
- 使用mybatis的动态sql解析能力生成sql
需求: 计算平台,有很多表,打算提供一个基于sql的服务接口, sql不能完全在配置页面写死, 要能根据参数不同执行不同的语义,防止sql个数爆炸 把mybatis原码down下来, 改造一下测试用例 ...
- [LC] 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 洛谷-P3809-后缀排序(后缀数组)
看了求后缀数组的倍增法之后很快就理解了,但是自己写的倍增法用map排序还是超时了.然后看了两天别人写的模板,题目是通过了,但感觉代码还是半懂半背的.以后多熟悉熟悉吧: 后缀数组 #include &q ...
- 关于Synchornized,Lock,AtomicBoolean和volatile的区别介绍
1. volatile 变量可以被看作是一种 "程度较轻的 synchronized". 2. Lock 实现提供了比使用 synchronized 方法和语句可获得的更广泛的 ...
- python 获取表单的三种方式
条件:urls.py文件中配置好url的访问路径.models.py文件中有Business表. 在views.py文件中实现的三种方式: from app01 improt models def b ...
- Luogu1681_ 最大正方形II
题目背景 忙完了学校的事,v神终于可以做他的"正事":陪女朋友散步.一天,他和女朋友走着走着,不知不觉就来到了一个千里无烟的地方.v神正要往回走,如发现了一块牌子,牌子上有有一行小 ...
- Android Studio那些错误的问题们
本片博客会记录关于Android开发工具Android Studio出错的那些问题,包括导入项目编译失败.时间过长,莫名其妙的歇菜等等... 问题 3facets cannot be loaded.Y ...