Sort Letters by Case

Description

Example

Lintcode_ladder

Method

  1. x
  2. x

Example

  1. 1
class Solution {
public:
    /** 
     * @param chars: The letters array you should sort.
     */
    void sortLetters(string &s) {
        // write your code here
        if (s.empty()) {
            return;
        }
        int len = s.size();
        int start = 0;
        int end = len - 1;
        while (start < end) {
            if (!testLower(s[start])) {
                swap(s[start], s[end]);
                --end;
            } else {
                ++start;
            }
        }
        return;
    }
    // test whether the @param ch is the lower letter
    bool testLower(const char ch) {
        if ( ch >= 'a' && ch <= 'z') {
            return true;
        }
        return false;
    }
};

Similar problems

x

Tags

x

results matching ""

    No results matching ""