我创建了一种查找字符串中最常见字符的方法:
public static char getMax(String s) {
char maxappearchar = ' ';
int counter = 0;
int[] charcnt = new int[Character.MAX_VALUE + 1];
for (int i = 0 ; i < s.length() ; i++)
{
char ch = s.charAt(i);
// increment this character's cnt and compare it to our max.
charcnt[ch]++ ;
if (charcnt[ch] >= counter)
{
counter = charcnt[ch];
maxappearchar = ch;
}
}
System.out.println("the max char is " +maxappearchar + " and displayed " +counter+ " times");
return maxappearchar;
}
我想询问有关IT技术的不同解决方案:
- 解决方案1 - 最快的代码(我的附加代码是否符合要求?)
- 解决方案2 - 在内存使用、减少数组和变量方面最有效
我使用了HashMap创建了我的方法 - 这对解决方案2更合适吗?如果是,为什么?它有哪些优缺点?
附加的代码是否适用于o技术(o^,o logn ...)?如果是,为什么?