您的位置:首页 > 其它

统计任意长度字符串中各个字符及个数——计数排序的又一个应用

2009-04-14 18:12 381 查看
又一次使用到计数排序~~挺好~!

/**
*
*统计出任意长度字符串s中的不同字符以及它的个数
*/
public class CountSort {
public static void main(String[] args) {
String s = "535aszzzzzdgd@$@#!$%8^&*90-2";
statistics(s);
}

public static void statistics (String s){
char chars [] = s.toCharArray();
int results [] = new int[128];

for(char ch : chars){
results[(int)ch] ++;
}

for(int i=0; i<results.length; i++){
if(results[i] != 0){
System.out.println( (char)i + " appeats:" + results[i] + " times.");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐