文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页网页设计经验技巧 → 关于百度的面试题?

关于百度的面试题?

时间:2010/1/21 16:31:00来源:本站整理作者:我要评论(0)

百度的一个面试题。“遍历一次字符串,选择出在该字符串中出现次数最多的字符及其出现次数”

比如说当前的字符串为"abc2d2e2fg2e1111bc11yh22fd2a2ai222eur张灯结彩",则我应得到的是字符2,出现次数11。

特定需求,只通过一次遍历来获取。

也就是说,只能循环字符串一次,大概看了一下,就开始入手了。于是写下了下面的代码:

 

view plaincopy to clipboardprint?
  1. public static void queryMaxCountByMap(String str) throws Exception{  
  2.     Map<String,Integer> map = new HashMap<String,Integer>();  
  3.     if(str == null || str.length()<1return ;  
  4.     for(int i=0;i<str.length();i++){  
  5.         String _tmp = String.valueOf(str.charAt(i));  
  6.         if(map.containsKey(_tmp)){  
  7.             int value = map.get(_tmp).intValue()+1;  
  8.             map.put(_tmp,value);  
  9.         }else{  
  10.             map.put(_tmp,1);  
  11.         }  
  12.     }  
  13.     String maxChar = "" ;  
  14.     int maxCount = 0 ;  
  15.     for(String _tmp:map.keySet()){  
  16.         int _tmpCount = map.get(_tmp);  
  17.         if(_tmpCount>maxCount){  
  18.             maxCount = _tmpCount ;  
  19.             maxChar = _tmp ;  
  20.         }  
  21.     }  
  22.       
  23.     System.out.println(maxChar);  
  24.     System.out.println(maxCount);  
  25.       
  26. }  

 

通过Main方法调用,是正确的。

这时,同事看了以后说,你写的这个方法中对map还进行了一次遍历。这样也算是2次遍历了。

于是,开始纠结了。。。。。。。。。。。。。。

要在一次遍历中实现此功能,同时不借助Map等也需要进行遍历的操作。这样,就需要我在每一次的循环中,

就要获取此次循环字符在整个字符串中的出现次数。有的同事也提出了用正则表达式,我对正则表达式属于用到

的时候才去找其定义规则,还不能灵活运用。或许还有别的方法,字符串的操作,看到replaceAll的时候,感觉

豁然开朗了。于是有了下面的一个方法:

 

view plaincopy to clipboardprint?
  1.     public static void queryMaxCountByFor(String str)throws Exception{  
  2.         if(str == null || str.length()<1return ;  
  3.         String maxChar = "" ;  
  4.         int maxCount = 0;  
  5.         int len = str.length();  
  6.           
  7.         for(int i=0;i<len;i++){  
  8.             String _tmp = String.valueOf(str.charAt(i));  
  9.             str = str.replaceAll(_tmp,"");//将属于该字符的,在当前字符串中清除掉。  
  10.             int _tmpCount = len - str.length();//通过前后字符串长度的比较,来确定其出现的次数。  
  11.             if(_tmpCount>maxCount){  
  12.                 maxChar = _tmp ;  
  13.                 maxCount = _tmpCount ;  
  14.             }  
  15.               
  16.             len = str.length();  
  17.             i=0;  
  18.         }  
  19. /*  这种方法更好理解,好实现。缺点就是出现重复遍历的情况。  
  20.         for(int i=0;i<len;i++){ 
  21.             String _tmp = String.valueOf(str.charAt(i)); 
  22.             String _replaceStr = str.replaceAll(_tmp,""); 
  23.             int _tmpCount = len - _replaceStr.length(); 
  24.             if(_tmpCount>maxCount){ 
  25.                 maxChar = _tmp ; 
  26.                 maxCount = _tmpCount ; 
  27.             } 
  28.         }        
  29. */        
  30.         System.out.println(maxChar);  
  31.         System.out.println(maxCount);  
  32.           
  33.     }  

 

以及下面用while的一段实现,跟for循环类似。就当熟悉一下吧。

 

view plaincopy to clipboardprint?
  1. public static void queryMaxCountByWith(String str)throws Exception{  
  2.     String maxChar = "" ;  
  3.     int maxCount = 0 ;  
  4.     String tmp ;  
  5.     int len = str.length();  
  6.       
  7.     while(len>0){  
  8.         String charAt = String.valueOf(str.charAt(0));  
  9.         str = str.replaceAll(charAt,"");  
  10.         int newLen = str.length();  
  11.         int charLen = len - newLen ;  
  12.         len = newLen ;  
  13.         if(maxCount<charLen){  
  14.             maxChar = charAt ;  
  15.             maxCount = charLen ;  
  16.         }  
  17.     }  
  18.       
  19.     System.out.println(maxChar);  
  20.     System.out.println(maxCount);         
  21. }  

 

在写完这些后,又仔细看了一下,replaceAll这个方法,内部是否也是一层遍历呢?

 public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
纠结,俺先去学正则表达式了。



根据大家的评论,又增加了一个方法:
view plaincopy to clipboardprint?
  1. public static void queryMaxCount(String str) throws Exception{  
  2.     int maxCount =0;  
  3.     String maxChar = "" ;  
  4.     Map<String,Integer> map = new HashMap<String,Integer>();  
  5.     if(str == null || str.length()<1return ;  
  6.     for(int i=0;i<str.length();i++){  
  7.         String _tmp = String.valueOf(str.charAt(i));  
  8.         int value = 0;  
  9.         if(map.containsKey(_tmp)){  
  10.             value = map.get(_tmp).intValue()+1;  
  11.             map.put(_tmp,value);  
  12.         }else{  
  13.             value = 1 ;  
  14.             map.put(_tmp,1);  
  15.         }  
  16.         if(value>maxCount){  
  17.             maxCount = value ;  
  18.             maxChar = _tmp ;  
  19.         }  
  20.     }  
  21.     System.out.println(maxChar);  
  22.     System.out.println(maxCount);         
  23.       
  24. }  

相关视频

    没有数据

相关阅读 .net面试题库(不断更新)及面试技巧IBM面试题试解(关于50条狗、50个人、病狗)dnf遗忘国度的古代金币怎么得dnf遗忘国度的古代金币怎么获得 dnf遗忘国度的古代金币有什么用百度打不开网页怎么办百度网盘隐藏空间在哪里百度app怎么清理内存手机百度怎么换头像

文章评论
发表评论

热门文章 Wordpress本地安装教程dx1.5如何设置二级域名

最新文章 hbuilder有哪些快捷键Wordpress本地安装教程 Wordpress本地安装教程expression web 4文档乱码解决方法dz 2.5“收藏本版”关闭小对话框无法关闭解在制作安装软件之前,您必须先将易语言存盘,

人气排行 如何使用multipart/form-data格式上传文件Photoshop PS图层混合模式详解(非常详细)ISAPI_Rewrite3使用教程网站里添加收藏和设为首页代码桌面快捷方式图标不见了C#获取执行程序所在的当前路径的方法详解(XMLHttpRequest)进行跨域请求方法如何用远程桌面连接进行传输文件