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 };
posted on 2017-07-17 14:48 marioaimepchy 阅读(139) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步