description: 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.
Have you met this question in a real interview? Yes Example For “abAcD”, a reasonable answer is “acbAD”
简单的two pointers algorithm 的使用
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 || chars.length == 1) { return; } int left = 0; int right = chars.length - 1; while (left <= right) { if (left >= right) { return; } while (left <= right && Character.isLowerCase(chars[left])) { left++; } while (left <= right && Character.isUpperCase(chars[right])) { right--; } if (left <= right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } } }}新闻热点
疑难解答