Lintcode: Sort Letters by Case
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的更多相关文章
- lintcode :sort letters by case字符大小写排序
题目 字符大小写排序 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序. 您在真实的面试中是否遇到过这个题? Yes 样例 给出"abAcD",一个可能的答案为& ...
- Sort Letters by Case
Given a string which contains only letters. Sort it by lower case first and upper case second. Examp ...
- 49. Sort Letters by Case
最后更新 一刷 还是Partition,只不过这次是按照大小写字母来. public class Solution { public void sortLetters(char[] chars) { ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- [LintCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...
- LintCode Sort Colors
For this problem we need to sort the array into three parts namely with three numbers standing for t ...
- [LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- [LintCode] Sort Integers 整数排序
Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
随机推荐
- Linux shell 变量 数学 运算
Abstract : 1) Linux shell 中使用 let , [ ] ,(( )) 三种运算符操作 shell 变量进行简单的基本运算: 2)Linux shell 中使用 expr 与 ...
- LENGTH() CHAR_LENGTH()
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_length LENGTH(str) Returns the ...
- duplicate symbol _OBJC_CLASS 错误处理方法
错误: ld: duplicate symbol _OBJC_CLASS_$_************ in **************** 一种可能性是你的项目的不同group里有着相同名称的类 ...
- 理解tornado
计算密集型:多进程 IO密集型:多线程 能产生IO阻塞的情况很多,比如网络通讯.磁盘读写.当发生阻塞时,CPU是闲置的,此时如果就一个线程就没法处理其他事情了. 所以对于含有IO阻塞的环境,多线程 ...
- 2014年最大福利:185个Google排名因素!免费电子书下载
本博开张以来最大规模的干货放送!新手老手都有用! 不要再去追求PR了,不要再去发博客发论坛发外链了! 关注真正有用的Google排名因素! 整整185项,每一项都附带说明,必要的地方会给出一些附加的阅 ...
- TCP协议中的三次握手和四次挥手
转自: http://blog.csdn.net/whuslei/article/details/6667471/ 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示:
- 不允许修改SQLserver2008r2表中字段的属性问题
SQLserver2008r2修改表中字段的属性时弹出 点击工具->选项,取消阻止保存要求重新创建表的更改
- SQL重要命令
SQL对大小写不敏感:SELECT与select是相同的. SQL语句的分号:分号是在数据库系统中分隔每条 SQL 语句的标准方法,这样就可以在对服务器的相同请求中执行一条以上的 SQL 语句. SE ...
- C++经典编程题#6:分配病房
总时间限制: 1000ms 内存限制: 65536kB 描述 某个科室的病房分为重症和普通,只有当病人的疾病严重程度超过了入住重症病房的最低严重值,才可以安排入住重症病房. 现在要求设计一个程序, ...
- Shell数组例子
Shell数组例子 循环打印数组,并统计数组的个数: [root@slavedb array]# cat a.sh #!/bin/bash array=( freddy freddie tang sh ...