49. Sort Letters by Case字母按大小写排序

[抄题]:

Given a string which contains only letters. Sort it by lower case first and upper case second.

Example

Example 1:
	Input:  "abAcD"
	Output:  "acbAD"

Example 2:
	Input: "ABC"
	Output:  "ABC"

 [暴力解法]:扫一遍,小写就放最前面,大写就放最后面

时间分析:

空间分析:

 [优化后]:

时间分析:nlgn

空间分析:lgn

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

就是快速排序,这次不是颜色了,是字母移动就好

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[算法思想:迭代/递归]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

 [是否头一次写此类driver funcion的代码] :

 [潜台词] :

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;
        int right = chars.length - 1;
        
        while (left < right) {
            while ((left <= right) && (Character.isLowerCase(chars[left]))) {
                left++;
            }
            
            while ((left <= right) && (Character.isUpperCase(chars[right]))) {
                right--;
            }
            
            if (left < right) {
                char temp = chars[left];
                chars[left] = chars[right];
                chars[right] = temp;
                
                left++;
                right--;
            }
        }
    }
}
View Code

 

posted @ 2020-05-13 10:39  苗妙苗  阅读(176)  评论(0编辑  收藏  举报