lintcode-medium-Sort Letters by Case
Posted 哥布林工程师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-medium-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"
Challenge
Do it in one-pass and in-place.
public class Solution { /** *@param chars: The letter array you should sort by Case *@return: void */ public void sortLetters(char[] chars) { //write your code here if(chars == null || chars.length == 0) return; int left = 0; int right = chars.length - 1; while(left < right){ while(left < right && chars[left] >= ‘a‘ && chars[left] <= ‘z‘) left++; while(left < right && chars[right] >= ‘A‘ && chars[right] <= ‘Z‘) right--; if(left < right) swap(chars, left, right); } return; } public void swap(char[] chars, int i, int j){ char temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; return; } }
以上是关于lintcode-medium-Sort Letters by Case的主要内容,如果未能解决你的问题,请参考以下文章