首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。