lintcode 中等题:sort letters by case字符大小写排序

题目

字符大小写排序

给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。

您在真实的面试中是否遇到过这个题?

Yes

样例

给出"abAcD",一个可能的答案为"acbAD"

注意

小写字母或者大写字母他们之间不一定要保持在原始字符串中的相对位置。

挑战

在原地扫描一遍完成

解题

这个题目很简单,前面刚做一个把大于某个数之和的排在后面,快速排序的思想

public class Solution {
    /**
     *@param chars: The letter array you should sort by Case
     *@return: void
     */
    public void sortLetters(char[] chars) {
        //write your code here
        int len = chars.length;
        if(len ==0 || chars==null){

        }else{
            int i=0;
            int j=len-1;
            while(i<=j){
                while(i<=j && isLowerCase(chars[i])) i++;
                while(i<=j && isUpperCase(chars[j])) j--;
                if(i<=j){
                    char ch = chars[i];
                    chars[i] = chars[j];
                    chars[j] = ch;
                }
            }
        }

    }
    public boolean isLowerCase(char ch){
        if(ch>=‘a‘ && ch<=‘z‘)
            return true;
        return false;
    }
    public boolean isUpperCase(char ch){
        if(ch>=‘A‘ && ch<=‘Z‘)
            return true;
        return false;
    }

}

Java Code

时间: 2024-08-25 03:07:14

lintcode 中等题:sort letters by case字符大小写排序的相关文章

Lintcode 49 Sort Letters by Case

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. Example For "abAcD", a reasonable answer is "a

lintcode 中等题:longest substring without repeating characters 最长无重复字符的子串

题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于,"bbbbb",其无重复字符的最长子字符串为"b",长度为1. 解题 利用HashMap,map中不存在就一直加入,存在的时候,找到相同字符的位置,情况map,更改下标 public class Solution { /** * @param s: a s

lintcode 中等题: Implement Trie

题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assume that all inputs are consist of lowercase letters a-z. 解题 Trie,字典树,又称单词查找树.前缀树,是一种哈希树的变种.应用于字符串的统计与排序,经常被搜索引擎系统用于文本词频统计. 性质: 1.根节点不包含字符,除根节点外的每一个节点都只包

Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Example For "abAcD", a reasonable answer is "acbAD" 与将负数都放在前面,正数都放在后面的题目一样. 时间复杂度为O(n) 找到第一大写字母,记录其下标为i,则小写字母必定在i之后出现,在i之后找到第一个出现的小写字母j.交换i,

lintcode 中等题: 3 Sum II 三数之和II

题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = [-1, 2, 1, -4] and target = 1.  和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 只需要返回三元组之和,无需返回三元组本身 解题 和上一题差不多,程序也只是稍微修改了 public class Solution { /** * @param numbers: Give an array numbers of n integ

lintcode 中等题:next permutation下一个排列

题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] 注意 排列中可能包含重复的整数 解题 和上一题求上一个排列应该很类似 1.对这个数,先从右到左找到递增序列的前一个位置,peakInd 2.若peakInd = -1 这个数直接逆序就是答案了 3.peakInd>= 0 peakInd这个位置的所,和 peakInd 到nums.size() -1

lintcode 中等题:maximum subarray difference 最大子数组差

题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1],返回 6 注意 子数组最少包含一个数 挑战 时间复杂度为O(n),空间复杂度为O(n) 解题 刚做了数组中两个子数组和的最大值,这一题是求差,感觉上题的求解思想应该是可以用的 A B 分别是两个子数组的和,则: 所以 当A>B 的时候A越大越好 B越小越好 当A<B 的时候B越大越好 A越小越好

lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]

lintcode 中等题:continuous subarray sum 联系子数组之和

题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, 1, 3, -3, 4], 返回[1,4]. 解题 法一:直接暴力,时间复杂度O(N2),时间超时 public class Solution { /** * @param A an integer array * @return A list of integers includes the i