领扣LintCode问题答案-49. 字符大小写排序

领扣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;
			}
		}
	}
}

原题链接点这里

鸣谢

非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。
欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二当家的白帽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值