码迷,mamicode.com
首页 > 其他好文 > 详细

Lintcode 49 Sort Letters by Case

时间:2017-07-17 15:19:57      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:code   reason   oid   solution   tle   int   letter   param   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

标签:code   reason   oid   solution   tle   int   letter   param   case   

原文地址:http://www.cnblogs.com/pdu1988/p/7194198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!