Sort Letters by Case 字符大小写排序
Description
Given a string chars which contains only letters. Sort it by lower case first and upper case second. In different languages, chars will be given in different ways. For example, the string “abc” will be given in following ways:
Java: char[] chars = {‘a’, ‘b’, ‘c’};
Python:chars = [‘a’, ‘b’, ‘c’]
C++:string chars = “abc”;
You need to use an in-place algorithm to solve this problem.
public class Solution {
/**
* @param chars: The letter array you should sort by Case
* @return: nothing
*/
public void sortLetters(char[] chars) {
// write your code here
int left = 0 , right = chars.length -1 ;
while(left < right){
if(left < right && chars[left] <= 90 && chars[right] >= 97){
char temp = chars[left] ;
chars[left] = chars[right] ;
chars[right] = temp ;
}else if (left < right && chars[left] >= 97){
left++ ;
}else{
right-- ;
}
}
}
}