首页 > 学院 > 开发设计 > 正文

LintCode Sort Letters by Cases

2019-11-08 18:40:18
字体:
来源:转载
供稿:网友

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--; } } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表