给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。
public class Solution { /* * @param chars: The letter array you should sort by Case * @return: nothing */ public void sortLetters(char[] chars) { // write your code here if (chars == null || chars.length == 0) { return; } char standard = ('Z' + 'a') / 2; char[] newChars = new char[chars.length+1]; for (int i = 0; i < newChars.length; i++) { if (i == 0) { newChars[i] = standard; } else { newChars[i] = chars[i - 1]; } } int i = 0; int j = newChars.length - 1; while (i < j) { while (i < j && newChars[j] <= standard) { j--; } if (i < j) { newChars[i++] = newChars[j]; } while (i < j && newChars[i] >= standard) { i++; } if (i < j) { newChars[j--] = newChars[i]; } newChars[i] = standard; } for (int k = 0; k < chars.length; k++) { if (k < i) { chars[k] = newChars[k]; } else { chars[k] = newChars[k+1]; } } } }