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
For "abAcD", a reasonable answer is "acbAD" Challenge
Do it in one-pass and in-place.
Two Pointers:
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==0) return;
int l=0, r=chars.length-1;
while (true) {
while (l<r && isLower(chars, l)) {
l++;
}
while (l<r && isUpper(chars, r)) {
r--;
}
if (l == r) break;
swap(chars, l, r);
}
} public boolean isLower(char[] chars, int index) {
if (chars[index]>='a' && chars[index]<='z') return true;
else return false;
} public boolean isUpper(char[] chars, int index) {
if (chars[index]>='A' && chars[index]<='Z') return true;
else return false;
} public void swap(char[] chars, int l, int r) {
char temp = chars[l];
chars[l] = chars[r];
chars[r] = temp;
}
}