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 ...
随机推荐
- Python操作MySQL以及中文乱码的问题
Python操作MySQL需要安装Python-MySQL可以从网上搜索一下,和一般的Python包一样安装 安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验 ...
- The world beyond batch: Streaming 101
https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-101 https://www.oreilly.com/ideas/the ...
- n0_n1
#include<stdio.h>int a[10];void quanpailie(int i){ if(i==10) {for(i=0;i<10;i++) { print ...
- jdk Tomcat配置
安装Tomcat需要先安装JDKJDK安装JDK安装会有两次,两次不能再同一目录 你可以新建两个文件夹 JDK JRE 第一次安装在JDK 第二次在JRE在我的电脑右键属性 高级系统设置 环境变量 系 ...
- [转]EF 4.1 Code First
这篇文章介绍Code First开发以及如何与DbContext API一起使用.Code First允许使用C#或VB.NET类定义模型,在类或属性上有选择性的执行额外的配置或者使用Fluent A ...
- [转]C++ string的trim, split方法
很多其他语言的libary都会有去除string类的首尾空格的库函数,但是标准C++的库却不提供这个功能.但是C++string也提供很强大的功能,实现trim这种功能也不难.下面是几种方法: 1.使 ...
- wampserver
- mysql storage enginees
这段时间在看<High Performance MySQL>,看到存储引擎这个地方感到很多细节比较陌生,所以总结小记一些 为 了适应各种不同的运行环境,MYSQL提供了多种不同的存储引擎( ...
- Nginx的配置中与流量分发相关的配置规范:
1.除首页外,其他页面都在某个目录中首页可以直接在根目录下,其他页面都要在根目录下的目录中.不同的location尽量使用第一个dir的模式进行区分,便于区分该流量是落在nginx本地,还是转发到后端 ...
- Mongo简单查询总结
mongo 简单查询db.ansheng.findOne()//返回第一条匹配到的数据db.ansheng.findOne({"aaaaa":4})db.ansheng.find( ...