一个编程面试题没做出来,求大家帮忙!

wj539h 2007-12-10 07:46:33
String str = "aabcwdsfwewefwfewfewewegg";
现在有这么一个字符串,要求是写一段程序,打印出出现次数最多的字母,出现了多少次,应该怎么写呢?
...全文
1090 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
sonyejin 2007-12-12
  • 打赏
  • 举报
回复
12楼

这种题用正则表达式吧,先对字符串排序,然后替换重复的:
str.replaceALL("(.).*\\1","$1");
这个结果就是没有重复的一个字符串
再去轮训这个字符串里的每个字符,在原始字符串中的出现次数
while(matcher.find()){
i++;
}
这个i就是出现的次数。。。。剩下的LZ自己搞定吧

================================

排序的复杂度是多少?这种题目解法很多,当然要考虑效率
我还是觉得用ASCII码做索引的解法好,时间复杂度是O(n)量级
delphi_tang 2007-12-12
  • 打赏
  • 举报
回复
做一个直方图就可以了,什么API都不用查,C#代码如下:

namespace str
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string ss = "aabcwdsfwewefwfewfewewegg";
int[] aa = new int[26];
int max = 0;
char ch = '\0';

for(int i=0; i<aa.Length; i++)
{
aa[i] = 0;
}

for(int i=0; i<ss.Length; i++)
{
aa[(int)(ss[i]-'a')]++;
}

max = aa[0];

for(int i=0; i<aa.Length; i++)
{
if(aa[i] > max)
{
max = aa[i];
ch = (char)(i + 'a');
}
}

System.Console.WriteLine("The result is: " + ch.ToString() + " and count is: " + max.ToString());
}
}
}
cracker234 2007-12-12
  • 打赏
  • 举报
回复
String str = "aabcwdsfwewefwfewfewewegg";
Map<String,Integer> map=new HashMap<String,Integer>();
for(String a:str.split("")){
Integer b=m.get(a)
map.put(a,b==null?1:b+1)}
System.out.println(map);
lengyuqing523 2007-12-12
  • 打赏
  • 举报
回复
同意 12楼 的意见~用正则表达式 处理起来非常简单~
vincent_1011 2007-12-12
  • 打赏
  • 举报
回复
啊?其实我这样想行不行啊,无非最多是26个字母,可以用个hash办法

例如 A就存到a[1]的数组单元中,B就放到a[2] ,一开始就全清0,

然后第读到一个数就把里面的数加1,最后再比较26个元素的大小??
zhoujunzan 2007-12-12
  • 打赏
  • 举报
回复
支持用map,遍历一遍就可以了,效率高
Zero2One 2007-12-12
  • 打赏
  • 举报
回复
用map吧,字母做键,次数为值
qxs 2007-12-12
  • 打赏
  • 举报
回复
.net 写的 大家看看可以不
string str = "dfghhghgjhgjhjghgfhfghffhffffff";
char[] arr = str.ToCharArray();

int max = 0;
int counts = 0;

string llstr = str;

for (int i = 0; i < str.Length; i++)
{
counts = 0;
while (llstr.IndexOf(arr[i]) >= 0)
{
llstr = llstr.Substring(llstr.IndexOf(arr[i]) + 1);
counts += 1;
}

if (counts > max)
{
max = counts;
}
llstr = str;
}
Response.Write(max);
ltc_mouse 2007-12-11
  • 打赏
  • 举报
回复
回10楼 hx2044

我想问一下Arrays.fill(counts, 0)有什么用.
----------------------------------------
呵呵,这个应该查API文档~~
本意是将数组counts的元素都置为0。不过Java中new出来的存储空间已经都是0了。这个确实是多余的。之前熟悉的是C/C++,刚学java,很多习惯没改过来,^_^
HashMapArrayList 2007-12-11
  • 打赏
  • 举报
回复
不用api的话 直接,逛循环 遍历。
zzxcr 2007-12-11
  • 打赏
  • 举报
回复
import java.util.Map;
import java.util.TreeMap;


public class RepeatLetter {

public static void main(String[] args)
{


// TODO Auto-generated method stub
String str="aabcwdsfwewefwfewfeweweggsklkdkkkSKFHASKLHFDLKASHDFLSHDFSSJHDHHHHHHHHHHHHHHHSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk";
char[] arr = str.toCharArray();

TreeMap<Character,Integer> temp = new TreeMap<Character,Integer>();
for(Character x:arr)
{
Integer time = temp.get(x);
if(time==null)
{
time=1;

}

else
time++;
temp.put(x, time);
}
System.out.println(temp.lastKey()+":"+temp.get(temp.lastKey()));
char letterLittle = getMaxTimes(temp,'a','z');
char letterUpper = getMaxTimes(temp,'A','Z');
char maxLetter='a';
if(temp.get(letterLittle)>temp.get(letterUpper)){
maxLetter=letterLittle;
}
else
{
maxLetter=letterUpper;
}
System.out.println(" maximum Times:"+temp.get(maxLetter)+" Letter:"+maxLetter);
}

private static char getMaxTimes(Map temp,char startLetter, char endLetter) {
// TODO Auto-generated method stub
char keyLetter = startLetter;
char maxTimesLetter='a';
//char endLetter='z';
int maxTime = 0;
while(keyLetter<endLetter){
if(temp.containsKey(keyLetter)){
if(maxTime<(Integer)temp.get(keyLetter)){
maxTimesLetter=keyLetter;
maxTime=(Integer) temp.get(keyLetter);
}

}
keyLetter++;
}
return maxTimesLetter;
}
}

a68020405ybcoomy 2007-12-11
  • 打赏
  • 举报
回复
package calculate;

import java.util.HashMap;

//将一个字符串中,出现最多的字母寻找出来
public class CharArrayMax {

/**
* @param args
*/
public static void main(String[] args) {
String str = "aadsssssssssssssssssdfsadflksafdsfesafasga";
char[] chars = str.toCharArray();
HashMap<Character, Integer> tm = new HashMap<Character, Integer>();
// 将字符串放入HashMap
for (char car : chars) {
Integer time = (Integer) tm.get(car);
if (time == null) {
time = 1;
} else {
time += 1;
}
tm.put(car, time);
}
int value = 0;
Character key1 = null;
// 遍历HashMap寻找最大值
for (Character key : tm.keySet()) {
if (tm.get(key) > value) {
value = tm.get(key);
key1 = key;
}
}
System.out.println(key1 + ":" + value);

}

}
ml_dark 2007-12-11
  • 打赏
  • 举报
回复
这种题用正则表达式吧,先对字符串排序,然后替换重复的:
str.replaceALL("(.).*\\1","$1");
这个结果就是没有重复的一个字符串
再去轮训这个字符串里的每个字符,在原始字符串中的出现次数
while(matcher.find()){
i++;
}
这个i就是出现的次数。。。。剩下的LZ自己搞定吧
JhunHoonXZ 2007-12-11
  • 打赏
  • 举报
回复
不查api的话..我只能做到这种程度了..

public class Test {
public static void main(String[] args) {
String str = "aabcwdsfwewefwfewfewewegg";
char[] ch=str.toCharArray();
int[] count=new int[ch.length];
for(int i=0;i<ch.length-1;i++){
count[i]=1;
for(int j=i+1;j<ch.length;j++){
if(ch[i]==ch[j])
count[i]++;
}
}
for(int i=0;i<count.length;i++){
int sum=0;
for(int j=0;j<count.length;j++){
if(count[i]<count[j])
sum++;
}
if(sum==0){
System.out.println("char="+ch[i]+",count="+count[i]);
}
}
}
}

hx2044 2007-12-11
  • 打赏
  • 举报
回复
public static void test( ) {
String str="aabcwdsfwewefwfewfewewegg";
char[] arr=str.toCharArray();
int[] counts = new int[128]; //以字符的ASCII码为索引
Arrays.fill(counts, 0);
char max=0;
for(char ch:arr) {
counts[ch]++;
if(counts[ch]>counts[max])
max = ch;
}
System.out.println("char="+max+" count="+counts[max]);
}
我想问一下Arrays.fill(counts, 0)有什么用.
我运行了,没有它也可以.
flyseahappy 2007-12-11
  • 打赏
  • 举报
回复
Arrays.fill(counts, 0);
是用来使这个数组初始化为0的
好像默认的new的int 数组也就是0的,所以用不用一样。
mayany 2007-12-11
  • 打赏
  • 举报
回复
int num[26]={0};
char ch[10000]
while(ch[i]!='\0')
{
if(ch[i]>='A'&&ch[i]<='Z') num[ch[i]-'A']++;
else if(ch[i]>='a'&&ch[i]<='z')
num[ch[i]-'a']++;
}
int max=0;
for(int i=0 ;i<26;i++)
if(num[max]<num[i])
max = i;

for(i =0 ;i < 26 ;i++)
if(num[max] == num[i])
cout<<num[i]<<endl;






sonyejin 2007-12-11
  • 打赏
  • 举报
回复
7楼写的好,佩服佩服,用ASCII,不需要记住ASCII码值的,只要知道大概的范围就行了
wang_shx 2007-12-11
  • 打赏
  • 举报
回复
import java.util.*;

public class MethodOfCSDN {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
String str = "aabcwdsfwewefwfewfeweweggaaaaaaaaaaaaaaaaaaaaaa";
String temp = "";
String maxStr="";
Map newMap = new HashMap();
int i=0;
int max=0;
int num=0;
for (i=0;i<str.length();i++){
temp = str.substring(i,i+1);
if (newMap.containsKey(temp)){
num =Integer.parseInt(String.valueOf(newMap.get(temp)));
newMap.remove(temp);
newMap.put(temp, String.valueOf(num+1));
if ((num+1)>max) {
maxStr = temp;
max = num+1;
}

}else{
newMap.put(temp,"1");
}
}
System.out.println(maxStr);
System.out.println(max);

}

}
数据兔 2007-12-11
  • 打赏
  • 举报
回复

String str = "aabcwdsfwewefwfewfewewegg";
char[] strs = str.toCharArray();

TreeMap<String, Integer> map = new TreeMap<String, Integer>();

for (int i = 0; i < strs.length; i++) {
char c = strs[i];
String s = Character.toString(c);
Integer count = map.get(s);
if (count == null) {
count = 1;
map.put(s, count);
} else {
count++;
map.put(s, count);
}
}

int max = 0;
String maxKey = "";
for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
String key = it.next();
System.out.println(key + ":" + map.get(key));
int count = map.get(key);
if (max < count) {
max = count;
maxKey = key;
}
}

System.out.println("the result:");
System.out.println(maxKey + ":" + max);

加载更多回复(12)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧