Sort Letters by Case

http://www.lintcode.com/en/problem/sort-letters-by-case/

Discussion

跟Partition Array问题一样,也就是Sort Colors问题的简化版,可以认为是两种颜色sort,更简单了。

Solution


class Solution {
public:
    /** 
     * @param chars: The letters array you should sort.
     */
    void sortLetters(string &letters) {
        // write your code here
        int i = 0; 
        int j = letters.length()-1;
        while(i<j+1) {
            if(letters[i] >='a') { //不能是letters[i]<'A'
                i++;
            } else {
                swap(letters[i], letters[j--]);
            }
        }
    }
};

注意A的ASCII CODE 65, a的是97

results matching ""

    No results matching ""