51信用卡编程题,按大小写字母和数字排序

1、问题描述:

输入一个字符串,字符串包含大小写字母和其他字符,现在要求对此字符串进行排序,并输出结果。

排序要求:

字母按a-z,A-Z排序,不分大小写,如果字母相同小写在大写之前。
例如:”abcADZ“排序结果为“aAbcDZ”

2、思路描述:

采用桶排序方法,桶排序是指以数组下标代表具体字母,数组内容存放对应字母个数。
用两个数组分别存储大小写字母个数,最后组合输出。

3、代码实现:

package cn.xaut.xinyongka;

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			String str = in.nextLine();
			
			int[] alph = new int[26];
			int[] smallAlph = new int[26];
			StringBuffer noAlph = new StringBuffer();
			
			char[] strs = str.toCharArray();
			for(int i=0; i < strs.length; i++){
				if(strs[i] >= 65 && strs[i] <= 90){
					alph[strs[i]-'A']++;
				} else {
					if (strs[i] >= 97 && strs[i] <= 122) {
						smallAlph[strs[i]-'a']++;
					} else {
						noAlph.append(strs[i]);
					}
				}
			}
			
			StringBuffer result = new StringBuffer();
			for(int j=0; j < 26; j++){
				while(smallAlph[j] > 0){
					result.append((char)(j + 'a'));
					smallAlph[j]--;
				}
				while(alph[j] > 0){
					result.append((char)(j + 'A'));
					alph[j]--;
				}
			}
			result.append(noAlph);
			System.out.println(result);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值