论坛首页 Java企业应用论坛

面试题目

浏览 33062 次
锁定老帖子 主题:面试题目
精华帖 (0) :: 良好帖 (0) :: 新手帖 (16) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-31  
evabibi 写道
public static void main(String[] args) {
		 String s= "sdfsddddddddddfffff,sdfsdf,"; 
		 s=s.replaceAll("[^a-zA-Z]","");
		 int max = 0;
		 int temp = 0;
		 String tempString = "";
		 for(int i = 0 ;i<s.length() ; i++ ){
			 tempString = s.substring(0,1);
			 String subs = s.replace(s.substring(0,1),"");
			 temp = s.length() - subs.length();
			 if(max<temp){
				 max=temp;
			 }
			 s = subs;
		 }
		 System.out.println(tempString+" max= "+max);
}


>_<


用replace会对字符串扫描一遍,有m种字符的话,时间复杂度就是m*N
0 请登录后投票
   发表时间:2009-07-31  
如果只是需要统计字母, 不包含标点符号, 直接新建一个长度为26的数组分别对应a-z不就行了, 如果区分大小写, 那就长度为52
遍历两次

一次文本, 次取最大值
0 请登录后投票
   发表时间:2009-07-31  
如果允许使用JDK API 那这道题目没有任何意义
0 请登录后投票
   发表时间:2009-07-31  
最烦手写代码的。。。不知道这些公司怎么想的。哎。。。
0 请登录后投票
   发表时间:2009-07-31  
public class Test {
private long result[] = new long[60];
private int index = 0;
private long max = -1;
public char getIndex() {
return (char) (index + 65);
}
public long getMax() {
return max;
}
public Test(String content) {
count(content);
}
private void count(String content) {
for (char temp : content.toCharArray()) {
if ((temp >= 65 && temp <= 90) || (temp >= 97 && temp <= 122)) {
temp -= 65;
result[temp]++;
}
}
for (int i = 0; i < result.length; i++) {
if (result[i] > max) {
index = i;
max = result[i];
}
}
}
}

public class Main {
public static void main(String[] args) {
Test test = new Test("ABTTTadgagCCCCCCC!@@fgfaaaIIIIIIIIIIITTCab.bagag3!@");
System.out.println(test.getIndex());
System.out.println(test.getMax());
}
}


我也贴一个,从索引下标来的灵感
0 请登录后投票
   发表时间:2009-07-31   最后修改:2009-07-31
public class CountChar {

	public static void main(String[] args) {
		String str="hello wolrd wlllkdsfhksadfls?sdfls sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd  ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs";
		int[] count = new int[128];
		for(int i=0;i<str.length();i++){
				count[str.charAt(i)]++;		
		}
		int maxCount=0;
		char maxChar='a';
		for(int i=0;i<count.length;i++){
			if(count[i]>maxCount&&(i>='a'&&i<='z' || i>='A'&&i<='Z')){
				maxCount=count[i];
				maxChar=(char)i;
			}
		}
		System.out.println("个数最多的字母是"+maxChar+",共"+maxCount+"个");
	}

}
0 请登录后投票
   发表时间:2009-07-31  
BMW 写道
evabibi 写道
public static void main(String[] args) {
		 String s= "sdfsddddddddddfffff,sdfsdf,"; 
		 s=s.replaceAll("[^a-zA-Z]","");
		 int max = 0;
		 int temp = 0;
		 String tempString = "";
		 for(int i = 0 ;i<s.length() ; i++ ){
			 tempString = s.substring(0,1);
			 String subs = s.replace(s.substring(0,1),"");
			 temp = s.length() - subs.length();
			 if(max<temp){
				 max=temp;
			 }
			 s = subs;
		 }
		 System.out.println(tempString+" max= "+max);
}


>_<


用replace会对字符串扫描一遍,有m种字符的话,时间复杂度就是m*N



en我欠考虑了, 用做java的效率非常低 .这是以前的js的算法
0 请登录后投票
   发表时间:2009-07-31  
王者之剑 写道
public class CountChar {

	public static void main(String[] args) {
		String str="hello wolrd wlllkdsfhksadfls?sdfls sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd  ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs";
		int[] count = new int[128];
		for(int i=0;i<str.length();i++){
				count[str.charAt(i)]++;		
		}
		int maxCount=0;
		char maxChar='a';
		for(int i=0;i<count.length;i++){
			if(count[i]>maxCount&&(i>='a'&&i<='z' || i>='A'&&i<='Z')){
				maxCount=count[i];
				maxChar=(char)i;
			}
		}
		System.out.println("个数最多的字母是"+maxChar+",共"+maxCount+"个");
	}

}


貌似
for(int i=0;i<str.length();i++){
count[str.charAt(i)]++;
}
直接用字符作下标不是个好选择,
如果含有超过128的字符比如中文.
0 请登录后投票
   发表时间:2009-07-31  
evabibi 写道
public static void main(String[] args) {
		 String s= "sdfsddddddddddfffff,sdfsdf,"; 
		 s=s.replaceAll("[^a-zA-Z]","");
		 int max = 0;
		 int temp = 0;
		 String tempString = "";
		 for(int i = 0 ;i<s.length() ; i++ ){
			 tempString = s.substring(0,1);
			 String subs = s.replace(s.substring(0,1),"");
			 temp = s.length() - subs.length();
			 if(max<temp){
				 max=temp;
			 }
			 s = subs;
		 }
		 System.out.println(tempString+" max= "+max);
}


>_<

运行了下,不正确呀!
0 请登录后投票
   发表时间:2009-07-31  
呵呵,是不是迅雷的题目啊
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics