如果字母的数量和数字的数量之差为 0 或 1,则可以 reformat。注意如果之差为 1,那么多的那种字符应该先写。

/**
Runtime: 2 ms, faster than 98.95% of Java online submissions for Reformat The String.Memory Usage: 39.5 MB, less than 100.00% of Java online submissions for Reformat The String.
8、*/
class Solution {public String reformat(String s) {if (s == null || s.length() == 0) {return "";}int countOfDigit = 0;int countOfCharacter = 0;char[] digits = new char[s.length()];char[] characters = new char[s.length()];char[] chars = s.toCharArray();for (int i=0; i<chars.length; i++) {char c = chars[i];if (c >= 'a' && c<= 'z') {characters[countOfCharacter] = c;countOfCharacter++;continue;}digits[countOfDigit] = c;countOfDigit++;}if (Math.abs(countOfCharacter - countOfDigit) > 1) {return "";}StringBuilder result = new StringBuilder();if (countOfDigit > countOfCharacter) {for (int i=0; i<Math.min(countOfDigit, countOfCharacter); i++) {result.append(digits[i]);result.append(characters[i]);}result.append(digits[countOfDigit-1]);} else if (countOfDigit < countOfCharacter) {for (int i=0; i<Math.min(countOfDigit, countOfCharacter); i++) {result.append(characters[i]);result.append(digits[i]);}result.append(characters[countOfCharacter-1]);} else {for (int i=0; i<Math.min(countOfDigit, countOfCharacter); i++) {result.append(characters[i]);result.append(digits[i]);}}return result.toString();}
}

[LeetCode][easy]Reformat The String相关推荐

  1. Leetcode 1370. Increasing Decreasing String

    Leetcode 1370. Increasing Decreasing String 题目链接: Increasing Decreasing String 难度:easy 题目大意: 按照题目要求对 ...

  2. leetcode算法题--Decode String

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

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

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

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

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

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

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

  6. leetcode easy problem set

     *勿以浮沙筑高台* 持续更新........     题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...

  7. leetcode算法题--Magical String

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

  8. LeetCode - Easy - 696. Count Binary Substrings

    Topic String Description https://leetcode.com/problems/count-binary-substrings/ Give a string s, cou ...

  9. LeetCode - Easy - 14. Longest Common Prefix

    Topic String Description https://leetcode.com/problems/longest-common-prefix/ Write a function to fi ...

  10. LeetCode - Easy - 191. Number of 1 Bits

    Topic Bit Manipulation Description https://leetcode.com/problems/number-of-1-bits/ Write a function ...

最新文章

  1. 剑指offer:面试题41. 数据流中的中位数
  2. se(3)-TrackNet: 数据驱动的动态6D物体姿态跟踪, 基于合成域的图片残差校准
  3. python爬虫数据分析可以做什么-python爬虫爬取的数据可以做什么
  4. nginx配置二级域名
  5. 如何从标签创建新分支?
  6. 复杂链表的复制(C++解法)
  7. 关于何种情况下使用DataGrid、DataList或Repeater的一些讨论(1) ambushaa [翻译] [转]
  8. Cassandra数据库模糊查询
  9. 开源.net 混淆器ConfuserEx介绍 [转]
  10. Windows10中同时安装MySQL5和MySQL8
  11. html设置ie9兼容性视图,ie9兼容性设置在哪里 IE兼容性视图在哪里设置?
  12. 搭建VUE脚手架 + 引入element-ui
  13. JS中的一些条件判断语句
  14. 神解释:为什么程序员怕改需求?
  15. Widget-苹果.谷歌和诺基亚着迷的网络新思维
  16. 硬盘RAID是什么意思?有什么用?
  17. Guass列选主元消去法和三角分解法
  18. Tomcat的安装使用,修改默认8080端口号,查询端口号命令
  19. RS485通信和Modbus协议
  20. python做统计分析_用Python做数据分析,Numpy,Pandas,matp

热门文章

  1. 设计模式之(Composite)组合模式
  2. Alpha和索引色透明
  3. 宝塔面板修改端口号无法登陆解决方案
  4. Java求矩形面积和圆形面积的异常处理实例
  5. 安装Baidu Sitemap Generator插件显示“该插件没有有效的标题”
  6. 存储桶列表访问权限_如何设置 ACL 存储桶权限? - Amazon Simple Storage Service
  7. 电脑花屏是屏幕坏了吗_电脑开机屏幕花的是不是主板坏了
  8. python重写和装饰器_Python | 老司机教你 5 分钟读懂 Python 装饰器
  9. php解决中文乱码的函数,php 中解决json中文乱码的函数_PHP教程
  10. windows java 进程_windows下java -jar 后台运行以及杀死后台进程的操作