Given a string which contains only letters. Sort it by lower case first and upper case second.

Note
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.

Two Pointers:

 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 l=0, r=chars.length-1;
while (true) {
while (l<r && isLower(chars, l)) {
l++;
}
while (l<r && isUpper(chars, r)) {
r--;
}
if (l == r) break;
swap(chars, l, r);
}
} public boolean isLower(char[] chars, int index) {
if (chars[index]>='a' && chars[index]<='z') return true;
else return false;
} public boolean isUpper(char[] chars, int index) {
if (chars[index]>='A' && chars[index]<='Z') return true;
else return false;
} public void swap(char[] chars, int l, int r) {
char temp = chars[l];
chars[l] = chars[r];
chars[r] = temp;
}
}

Lintcode: Sort Letters by Case的更多相关文章

  1. lintcode :sort letters by case字符大小写排序

    题目 字符大小写排序 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序. 您在真实的面试中是否遇到过这个题? Yes 样例 给出"abAcD",一个可能的答案为& ...

  2. Sort Letters by Case

    Given a string which contains only letters. Sort it by lower case first and upper case second. Examp ...

  3. 49. Sort Letters by Case

    最后更新 一刷 还是Partition,只不过这次是按照大小写字母来. public class Solution { public void sortLetters(char[] chars) { ...

  4. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  5. [LintCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...

  6. LintCode Sort Colors

    For this problem we need to sort the array into three parts namely with three numbers standing for t ...

  7. [LintCode] Sort Integers II 整数排序之二

    Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...

  8. [LintCode] Sort Integers 整数排序

    Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort ...

  9. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

随机推荐

  1. Linux+环境使用vim搭建php+IDE

    让我们开始DIY吧-!!终端下执行命令:whereis vim     将列出vim安装的路径. 否则执行 sudo apt-get install vim 安装vim .成功安装了vim,只需要在用 ...

  2. 3D 生物打印血管成功植入恒河猴体内

    3D 生物打印血管成功植入恒河猴体内

  3. 线性表集合A=A B

    大话数据结构 void union(List *a, List Lb) { int La_len, Lb_len, i; ElemType e; La_len = ListLength(La); Lb ...

  4. mysql ERROR 1045 (28000): Access denied for user解决方法 (转)

    问题重现(以下讨论范围仅限Windows环境): C:\AppServ\MySQL> mysql -u root -pEnter password:ERROR 1045 (28000): Acc ...

  5. 【转】c# 解析JSON的几种办法

    http://www.cnblogs.com/ambar/archive/2010/07/13/parse-json-via-csharp.html 刚开始只是想找一个转换JSON数组的方法,结果在M ...

  6. Java ArrayListSerialise

    import java.io.*; import java.util.*; //ArrayListSerialise public class A { public static void main( ...

  7. iOS自定义控件开发详解

    http://blog.csdn.net/zhangao0086/article/details/45622875

  8. zepto源码--$.map,$.each,$.grep--学习笔记

    从相对比较简单的说起: 1.$.grep  获取一个新数组,新数组只包含回调函数中返回 true 的数组项. 调用javascript中数组原生函数filter,对elements进行过滤,保留回调函 ...

  9. Emiller's Advanced Topics In Nginx Module Development

    Emiller的Nginx模块开发指南 By Evan Miller DRAFT: August 13, 2009 (changes) 翻译:Kongch @2010年1月5日 0:04am -- 2 ...

  10. Openmpi 编译安装+集群配置 + Ubuntu14.04 + SSH无密码连接 + NFS共享文件系统

    来源 http://www.open-mpi.org/ 网络连接 SSH连接,保证各台机器之间可以无密码登陆,此处不展开 hosts文件如下 #/etc/hosts 192.168.0.190 mas ...