首页 > 代码库 > LintCode-Sort Letters by Case
LintCode-Sort Letters by Case
Given a string which contains only letters. Sort it by lower case first and upper case second.
Note
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:
1 public class Solution { 2 /** 3 *@param chars: The letter array you should sort by Case 4 *@return: void 5 */ 6 public void sortLetters(char[] chars) { 7 int len = chars.length; 8 if (len <= 1) return; 9 10 int p1 = 0, p2 = len-1;11 while (p1<len && chars[p1]>=‘a‘ && chars[p1]<=‘z‘) p1++;12 while (p2>=0 && chars[p2]>=‘A‘ && chars[p2]<=‘Z‘) p2--;13 while (p1<p2){14 //swap p1 and p2.15 char temp = chars[p1];16 chars[p1] = chars[p2];17 chars[p2] = temp;18 //find next swap positions.19 while (p1<len && chars[p1]>=‘a‘ && chars[p1]<=‘z‘) p1++;20 while (p2>=0 && chars[p2]>=‘A‘ && chars[p2]<=‘Z‘) p2--;21 }22 }23 }
LintCode-Sort Letters by Case
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。