Given a string which contains only letters. Sort it by lower case first and upper case second.
Example
"abAcD"
for, a reasonable "acbAD"
answer is
With the negative numbers in front, the positive numbers are placed in the following topic.
Time Complexity of O (n)
Find the first capital letter, record its subscript as I, then the lowercase letter must appear after I, after I find the first occurrence of the lowercase letter J. Exchange I, J. Note: The letters between I and J must be uppercase. Proceed in turn.
1 Public classSolution {2 /** 3 *@paramchars:the Letter Array-should sort by case4 *@return: void5 */6 Public voidSortletters (Char[] chars) {7 if(Chars = =NULL|| Chars.length = = 0) {8 return;9 }Ten inti = 0; One for(i = 0; i < chars.length; i++) { A if(Chars[i] >= ' A ' && chars[i] <= ' Z ') { - Break; - } the } - for(intj = i + 1; J < Chars.length; J + +) { - if(Chars[j] >= ' A ' && chars[j] <= ' z ') { - Chartemp =Chars[i]; +Chars[i] =Chars[j]; -CHARS[J] =temp; +i++; A } at } - return; - } -}
Sort Letters by case