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-- ;
}
}
}
}
本文介绍如何使用C#实现一个字符数组按大小写排序的in-place算法。通过实例讲解如何在Java、Python和C++中不同方式表示的字符数组中,先按小写字母排序,再按大写字母排序。重点展示Solution类中的sortLetters方法的详细步骤。
826

被折叠的 条评论
为什么被折叠?



