首页 > 代码库 > Lintcode 49 Sort Letters by Case

Lintcode 49 Sort Letters by Case

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

 Notice

It‘s NOT necessary to keep the original order of lower-case letters and upper case letters.

Example

For "abAcD", a reasonable answer is "acbAD"

Solution

小写指针前进begin++,大写则换不前进,末尾指针end--。

 1 class Solution {
 2 public:
 3     /** 
 4      * @param chars: The letters array you should sort.
 5      */
 6     void sortLetters(string &letters) {
 7         // write your code here
 8         int begin = 0, end = letters.size()-1;
 9         while(begin<=end){
10             if(isupper(letters[begin])){
11             char temp = letters[begin];
12             letters[begin] = letters[end];
13             letters[end] = temp; 
14             end--;
15             }
16             if(islower(letters[begin])) 
17             begin++;
18         }
19     }
20 };

 

Lintcode 49 Sort Letters by Case