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相关推荐

  1. leetcode算法题--Decode String

    原题链接:https://leetcode.com/problems/decode-string/ string decodeString(string s) {int n = s.size(), i ...

  2. 【字符串全排列】LeetCode 567. Permutation in String

    [字符串全排列]LeetCode 567. Permutation in String Solution1:[超时,未能AC] 得到s1的所有全排列组合,然后在s2中查找s1的全排列集合 因为超时,未 ...

  3. leetcode算法题--Magical String

    原题链接:https://leetcode.com/problems/magical-string/ int magicalString(int n) {string s = "122&qu ...

  4. leetcode 678. Valid Parenthesis String | 678. 有效的括号字符串(带缓存的暴力递归)

    题目 https://leetcode.com/problems/valid-parenthesis-string/ 题解 带缓存的暴力递归,非常挫.用一个 string 模拟 stack,方便缓存记 ...

  5. leetcode 334. Increasing Triplet Subsequence | 334. 递增的三元子序列(一种较trick的解法)

    题目 https://leetcode.com/problems/increasing-triplet-subsequence/ 题解 看到这题,首先,没有思路- 然后,看了下面的 Related Q ...

  6. LeetCode算法题-Repeated String Match(Java实现)

    这是悦乐书的第289次更新,第307篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第156题(顺位题号是686).给定两个字符串A和B,找到A必须重复的最小次数,使得B是 ...

  7. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 clas ...

  8. 【LeetCode】87. Scramble String

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  9. LeetCode Longest Increasing Subsequence(动态规划、二分法)

    问题:求数组的最长上升子序列问题 思路:第一种方法使用动态规划方法,用dp(i)来表示从0到i之间的最长上升子序列的长度.状态转移方程为dp(i)=max{dp(j)+1},其中0<=j< ...

最新文章

  1. 保留索引,cdh更新schema.xml
  2. c++ svd实例整理
  3. mysql元数据查询
  4. 第六章 XaaS和IT服务标准
  5. Apache Jakarta Commons 工具集简介
  6. 【leetcode-74】搜索二维矩阵
  7. 鸿蒙硬件HI3861-MQTT
  8. 使用Span T 提高C#代码的性能
  9. synchronized方法与synchronized代码块的区别
  10. rabbitmq消息的序列化与反序列化
  11. 通俗讲解c语言指针的作用,C语言指针讲解(史上最通俗最全面最经典)
  12. 日志打印longging模块(控制台和文件同时输出)
  13. java getSource()和 getActionCommand()区别
  14. c语言课程实验总结报告,c语言实验总结报告.doc
  15. baddy:核心函数入口
  16. 【mysql】mysql 导出全库表结构,并排除指定库
  17. Collecting Bugs (DP期望)
  18. 递归算法和过程的详解
  19. base+ball=games
  20. RTX3060是什么级别显卡 RTX3060显卡什么水平

热门文章

  1. 在nano上进行catkin_make编译时卡住
  2. 用邻接表表示图【java实现】
  3. 常用的区块链数据查询网站
  4. 仅仅三行JAVA代码计算多边形的几何中心点
  5. Linux下的eeprom读写操作
  6. STM32——GPIO输入——按键检测
  7. python 获取alert信息并截取alert图片
  8. 解读10x程序员工作法-以终为始
  9. 关注这几点,远程办公不犯迷糊
  10. 2023年年度最佳配色就是它-我已经为大家配好了色卡,还不快来用?