领扣LintCode问题答案-49. 字符大小写排序
49. 字符大小写排序
给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。
小写字母或者大写字母他们之间不一定要保持在原始字符串中的相对位置。
样例 1:
输入: “abAcD”
输出: “acbAD”
样例 2:
输入: “ABC”
输出: “ABC”
public class Solution {
/*
* @param chars: The letter array you should sort by Case
* @return: nothing
*/
public void sortLetters(char[] chars) {
// write your code here
int firstUpperIndex = 0;
int lastLowerCharIndex = chars.length - 1;
while (firstUpperIndex < lastLowerCharIndex) {
while (firstUpperIndex < lastLowerCharIndex
&& Character.isLowerCase(chars[firstUpperIndex])) {
firstUpperIndex++;
}
while (firstUpperIndex < lastLowerCharIndex
&& Character.isUpperCase(chars[lastLowerCharIndex])) {
lastLowerCharIndex--;
}
if (firstUpperIndex < lastLowerCharIndex) {
char temp = chars[firstUpperIndex];
chars[firstUpperIndex] = chars[lastLowerCharIndex];
chars[lastLowerCharIndex] = temp;
}
}
}
}
鸣谢
非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。
欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。