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 ...
随机推荐
- laravel ajax表格删除
view和jq @extends('layouts.main') @section('content') <h3>User List</h3> <p class=&quo ...
- SET ? DECLARE
http://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html http://dev.mysql.com/doc/refman/5 ...
- concurrency parallel 并发 并行
Computer Systems A Programmer's Perspective Second Edition The general phenomenon of multiple flows ...
- P1090 合并果子
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int main(int argc, char ...
- Delphi 中的结构体与结构体指针
好多程序都给结构体变量设定了一个结构体指针 例如: PAbc = ^TAbc; TAbc = record a: string[10]; b: string[5]; c: string[1]; end ...
- 我的第一个chrome扩展(3)——继续读样例
1.操作用户正在浏览的界面 http://www.ituring.com.cn/article/60212 问题:1.google未定义ID,用name为何无法找到? 2.如何让整个按钮一起动?原函数 ...
- 国外it网站收集
1.http://news.com.com/2.http://www.zdnet.com/3.http://www.salon.com/tech/index.html4.http://www.brin ...
- 【Android开发学习笔记】【第九课】重力感应
概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...
- Echart - 地图散点图(服务网点图)的实现
Echart是百度开发的一个javascript图表库,可以流程运行于pc和移动端,底层依赖轻量级的 Canvas 类库 ZRender. ECharts 提供了常规的折线图,柱状图,散点图,饼图,K ...
- iOS UPYUN(又拍云)使用总结
UPYUN,原来没用过,上个周用了一次,觉得蛮方便的,对于个人开发者,且没有服务器的,上传图片和文件,是个不二选择. 首先,先明白原理: 1.又拍云有一个上传空间,在这个空间里,有空间名称.密钥,其他 ...