Given a string which contains only letters. Sort it by lower case first and upper case second.
Note
It's not necessary to keep the original order of lower-case letters and upper case letters.
Example
思路:和Sort Color和Partition Array一致。
For "abAcD", a reasonable answer is "acbAD"
import java.util.ArrayList;
public class Solution {
/**
*@param chars: The letter array you should sort by Case
*@return: void
*/
public void sortLetters(char[] chars) {
//write your code here
if (chars == null || chars.length <= 1) {
return;
}
int lower = 0;
int upper = chars.length - 1;
int i = 0;
while (i <= upper) {
if (Character.isLowerCase(chars[i])) {
swap(chars, i, lower);
lower++;
i++;
} else {
swap(chars, i, upper);
upper--;
}
}
}
private void swap(char[] chars, int i, int j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}