Leetcode 1370. Increasing Decreasing String
Leetcode 1370. Increasing Decreasing String
题目链接: Increasing Decreasing String
难度:easy
题目大意:
按照题目要求对字符串进行排序。
思路:
1、统计字符串中各个字母的个数,用一个int类型的数组count[]来表示。count[0]~count[25]分别表示字母a-z出现的次数。大佬们用树状图来统计。
2、对数组进行扫描,如果count[i]大于零,输出对应的字母,将count[i]减一。
3、count[i]全部为零时,返回结果。
代码
class Solution {public String sortString(String s) {int count[]=new int[26];//a[0]~a[25]分别表示a~z的数量for(int i=0;i<s.length();i++){int j=s.charAt(i)-'a';count[j]++;}int sum=s.length();StringBuilder res=new StringBuilder();while(res.length()<sum){for(int j=0;j<26;j++){if(count[j]>0){res.append((char)(j+'a'));count[j]--;}}for(int j=25;j>=0;j--){if(count[j]>0){res.append((char)(j+'a'));count[j]--;}} }return res.toString();}
}
备注
String中的对象是不可变的,当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。
Java中StringBuilder的用法
Leetcode 1370. Increasing Decreasing String相关推荐
- leetcode算法题--Decode String
原题链接:https://leetcode.com/problems/decode-string/ string decodeString(string s) {int n = s.size(), i ...
- 【字符串全排列】LeetCode 567. Permutation in String
[字符串全排列]LeetCode 567. Permutation in String Solution1:[超时,未能AC] 得到s1的所有全排列组合,然后在s2中查找s1的全排列集合 因为超时,未 ...
- leetcode算法题--Magical String
原题链接:https://leetcode.com/problems/magical-string/ int magicalString(int n) {string s = "122&qu ...
- leetcode 678. Valid Parenthesis String | 678. 有效的括号字符串(带缓存的暴力递归)
题目 https://leetcode.com/problems/valid-parenthesis-string/ 题解 带缓存的暴力递归,非常挫.用一个 string 模拟 stack,方便缓存记 ...
- leetcode 334. Increasing Triplet Subsequence | 334. 递增的三元子序列(一种较trick的解法)
题目 https://leetcode.com/problems/increasing-triplet-subsequence/ 题解 看到这题,首先,没有思路- 然后,看了下面的 Related Q ...
- LeetCode算法题-Repeated String Match(Java实现)
这是悦乐书的第289次更新,第307篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第156题(顺位题号是686).给定两个字符串A和B,找到A必须重复的最小次数,使得B是 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 clas ...
- 【LeetCode】87. Scramble String
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- LeetCode Longest Increasing Subsequence(动态规划、二分法)
问题:求数组的最长上升子序列问题 思路:第一种方法使用动态规划方法,用dp(i)来表示从0到i之间的最长上升子序列的长度.状态转移方程为dp(i)=max{dp(j)+1},其中0<=j< ...
最新文章
- 保留索引,cdh更新schema.xml
- c++ svd实例整理
- mysql元数据查询
- 第六章 XaaS和IT服务标准
- Apache Jakarta Commons 工具集简介
- 【leetcode-74】搜索二维矩阵
- 鸿蒙硬件HI3861-MQTT
- 使用Span T 提高C#代码的性能
- synchronized方法与synchronized代码块的区别
- rabbitmq消息的序列化与反序列化
- 通俗讲解c语言指针的作用,C语言指针讲解(史上最通俗最全面最经典)
- 日志打印longging模块(控制台和文件同时输出)
- java getSource()和 getActionCommand()区别
- c语言课程实验总结报告,c语言实验总结报告.doc
- baddy:核心函数入口
- 【mysql】mysql 导出全库表结构,并排除指定库
- Collecting Bugs (DP期望)
- 递归算法和过程的详解
- base+ball=games
- RTX3060是什么级别显卡 RTX3060显卡什么水平