Increasing Decreasing String
给一个string, 让重组这个string, 其中的字符从小到大,再从大到小,直到没有.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Solution { public String sortString(String s) { int[] count = new int[26]; int n = s.length(); for(int i = 0; i < n; i++) { count[s.charAt(i) - 'a'] ++; } StringBuilder sb = new StringBuilder(); boolean loop = true; while(loop) { boolean used = false; for(int i = 0; i < 26; i++){ if(count[i] != 0) { sb.append((char)('a' + i)); used = true; count[i] --; } } for(int i = 25; i >= 0; i--){ if(count[i] != 0) { sb.append((char)('a' + i)); used = true; count[i] --; } } loop = used; } return sb.toString(); } } |