49. Sort Letters by Case

最后更新

一刷

还是Partition,只不过这次是按照大小写字母来。

public class Solution {
    public void sortLetters(char[] chars) {
        //write your code here
        if (chars.length <= 1) return;
        
        int start = 0;
        int end = chars.length - 1;
        char min = 'Z';
        char max = 'a';
        
        while (start < end) {
            int temp = start;
            while (temp <= end) {
                if (chars[temp] >= max) {
                    swap(temp ++, start ++, chars);
                } else if (chars[temp] <= min) {
                    swap(temp, end --, chars);
                } else {
                    System.out.println("All I do I do in her name.");
                    temp ++;
                }
            }
        }
        return;
    }
    public void swap(int l, int r, char[] chars) {
        char temp = chars[l];
        chars[l] = chars[r];
        chars[r] = temp;
    }
}
原文地址:https://www.cnblogs.com/reboot329/p/6219370.html