for(int i = 0; i < str.length(); i++) { char c = str.charAt(i); Integer cnt = map.get(c);
if(cnt == null) { cnt = 1; } else { cnt++; }
map.put(c, cnt); }
char ch = ' '; int max = 0;
for(char c: map.keySet()) { int temp = map.get(c).intValue(); if(temp > max) { max = temp; ch = c; } }
System.out.format("%4s%4d%n", ch, max);
呵呵,不要抓住我不放哈,我这几天事多,比较晕乎,容易出错,hoho
只是恰巧碰到了,呵呵。想请教一下,为什么用TreeMap,我换成HashMap也有相同的效果。
在这个问题里两个map一样的 但是如果以后想对key值排序的话,只能用TreeMap了,hoho
O(n)和用数组可解决 int max = 0, count[26]; char ch; String input = "ABCDEFEDEAA";for(int i = 0; i < str.length(); i++) { int cou = ++count[(int)input.charAt(i)%26]; if(cou >= max) { max = cou; ch = input.charAt(i); } } System.out.println(ch + " " + max);
11楼 int max = 0, count[26]; char ch; String input = "ABCDEFEDEAA";for(int i = 0; i < str.length(); i++) { int cou = ++count[(int)input.charAt(i)%26]; if(cou >= max) { max = cou; ch = input.charAt(i); } } System.out.println(ch + " " + max);
import java.util.*; public class Test {
//找出重复次数最多的字符,并打印出数量和字符 public static void main(String[] args) { String a = "aaabbdcfeeswdfttrrccaa"; List ls = new LinkedList(); Test test = new Test(); for(int i=0; i<a.length(); i++){ if(!test.isHave(ls,(a.charAt(i)+""))){ ls.add(a.charAt(i)+""); } } //保存相应的位置的字母的个数 int[] ret = new int[ls.size()]; int max =0; //最大数 for(int i=0; i<ls.size(); i++){ ret[i] = test.getNum(ls.get(i).toString(),a); if(ret[i]>max){ max = ret[i]; } } //检查个数最大的,返回位置列表 List ls2 = new LinkedList(); for(int i=0; i<ret.length;i++){ if(ret[i]==max){ ls2.add(i+""); } }
//遍历ls2,打印出相应字母和个数 System.out.println("\n打印出个数最多的字母及个数:"); for(int i=0; i<ls2.size(); i++){ int m = Integer.parseInt(ls2.get(i).toString()); System.out.println("字母:"+ls.get(i).toString()+" 个数:"+ret[m]); }
public class st {
public static void main (String[] args)
throws Exception { String test = "ABCDEFEDEAA" ;
Map<Character,Integer> cc = new
HashMap<Character,Integer>() ;
for (int i=0; i<test.length(); i++) {
char c = test.charAt(i) ;
Integer cnt = cc.get(c) ;
if (cnt==null) cnt = 1 ;
else cnt ++ ;
cc.put(c, cnt) ;
}
for (char c:cc.keySet()) {
System.out.format("%4s%4d%n", c,cc.get(c)) ;
}
}}
String str = "ABCDEFEDEAA";
Map<Character, Integer> map = new TreeMap<Character, Integer>();
for(int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
Integer cnt = map.get(c);
if(cnt == null)
{
cnt = 1;
}
else
{
cnt++;
}
map.put(c, cnt);
}
char ch = ' ';
int max = 0;
for(char c: map.keySet())
{
int temp = map.get(c).intValue();
if(temp > max)
{
max = temp;
ch = c;
}
}
System.out.format("%4s%4d%n", ch, max);
呵呵,不要抓住我不放哈,我这几天事多,比较晕乎,容易出错,hoho
但是如果以后想对key值排序的话,只能用TreeMap了,hoho
int max = 0, count[26];
char ch;
String input = "ABCDEFEDEAA";for(int i = 0; i < str.length(); i++) {
int cou = ++count[(int)input.charAt(i)%26];
if(cou >= max) {
max = cou;
ch = input.charAt(i);
}
}
System.out.println(ch + " " + max);
int max = 0, count[26];
char ch;
String input = "ABCDEFEDEAA";for(int i = 0; i < str.length(); i++) {
int cou = ++count[(int)input.charAt(i)%26];
if(cou >= max) {
max = cou;
ch = input.charAt(i);
}
}
System.out.println(ch + " " + max);
public class Test {
//找出重复次数最多的字符,并打印出数量和字符
public static void main(String[] args) {
String a = "aaabbdcfeeswdfttrrccaa";
List ls = new LinkedList();
Test test = new Test();
for(int i=0; i<a.length(); i++){
if(!test.isHave(ls,(a.charAt(i)+""))){
ls.add(a.charAt(i)+"");
}
}
//保存相应的位置的字母的个数
int[] ret = new int[ls.size()];
int max =0; //最大数
for(int i=0; i<ls.size(); i++){
ret[i] = test.getNum(ls.get(i).toString(),a);
if(ret[i]>max){
max = ret[i];
}
}
//检查个数最大的,返回位置列表
List ls2 = new LinkedList();
for(int i=0; i<ret.length;i++){
if(ret[i]==max){
ls2.add(i+"");
}
}
//遍历ls2,打印出相应字母和个数
System.out.println("\n打印出个数最多的字母及个数:");
for(int i=0; i<ls2.size(); i++){
int m = Integer.parseInt(ls2.get(i).toString());
System.out.println("字母:"+ls.get(i).toString()+" 个数:"+ret[m]);
}
System.out.println("\n打印出所有字母及个数:");
for(int i=0; i<ret.length;i++){
System.out.println("字母:"+ls.get(i).toString()+" 个数:"+ret[i]);
}
}
//查询该列表 ls 是否有该字符
private boolean isHave(List ls, String k){
boolean flag = false;
for(int i=0; i<ls.size(); i++){
if(ls.get(i).toString().equals(k)){
flag = true;
break;
}
}
return flag;
}
//查询 a 中有几个 k
private int getNum(String k, String a){
int j=0;
for(int i=0; i<a.length(); i++){
if((a.charAt(i)+"").equals(k)){
j++;
}
}
return j;
}
}
运行结果:
打印出个数最多的字母及个数:
字母:a 个数:5
打印出所有字母及个数:
字母:a 个数:5
字母:b 个数:2
字母:d 个数:2
字母:c 个数:3
字母:f 个数:2
字母:e 个数:2
字母:s 个数:1
字母:w 个数:1
字母:t 个数:2
字母:r 个数:2
算法复杂度肯定是要维持在n级上,也就是对字符串做一边扫描;
那么接下来就是看谁用的结构最简单了;如果11楼可以优化的话,那么是否可以在“(int)input.charAt(i)%26”上做文章;
可以将“%”换成“-”;
再有就是研究 "String.charAt(int)" 这个方法了;如果他是一次定位,那么这部分也没有优化的余地了;