金山公司面试题:一个字符串中可能包含a~z中的多个字符,如有重复,如String data="aavzcadfdsfsdhshgWasdfasdf",求出现次数最多的那个字母及次数,如有多个重复的则都求出。
此题的解题思路如下:
引入TreeSet:通过集合快速找到所有出现过的字符串
引入ArrayList:为了快速排序,再通过StringBuffer生成排序后的字符串
通过String的indexOf方法和lastIndexOf方法来计算每个字符串出现的次数最大值
使用HashMap存储出现多的字符串和次数
代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
public class SortTest {
public static void main(String[] args) {
String input = "httpblogcsdnnetouyangpeng" ;
new SortTest().doString(input);
}
public void doString(String input) {
/**
* 第一步:
* 使用TreeSet快速找到所有出现的字符串
* 将输入的字符串按升序排列
*/
//将String转换为字符数组
char [] chars=input.toCharArray();
ArrayList<String> lists= new ArrayList<String>();
//TreeSet是一个有序集合,TreeSet中的元素将按照升序排列
//通过TreeSet的不重复性,快速找到所有出现的字符串
TreeSet<String> set= new TreeSet<String>();
for ( int i = 0 ; i < chars.length; i++) {
lists.add(String.valueOf(chars[i]));
set.add(String.valueOf(chars[i]));
}
//set= [a, b, c, d, e, g, h, l, n, o, p, s, t, u, y]
System.out.println( "set= " +set);
//排序
Collections.sort(lists);
//lists= [a, b, c, d, e, e, g, g, g, h, l, n, n, n, n, o, o, p, p, s, t, t, t, u, y]
System.out.println( "lists= " +lists);
//将排序好的字符数组转换为StringBuffer
StringBuffer sb= new StringBuffer();
for ( int i = 0 ; i < lists.size(); i++) {
sb.append(lists.get(i));
}
input=sb.toString();
//input= abcdeeggghlnnnnooppstttuy
System.out.println( "input= " +input);
/**
* 第二步: 找出出现相同的字符并记录出现多少次
*/
//最多重复出现多少次
int max= 0 ;
//重复出现的字符
String maxString= "" ;
/*//重复出现的字符列表
ArrayList<String> maxList=new ArrayList<String>();*/
//用来保存出现最多的字符串和次数
HashMap<String, Integer> hm= new HashMap<String, Integer>();
//将出现过的字符遍历
Iterator<String> its=set.iterator();
while (its.hasNext()) {
String os=its.next();
//字符出现在排序后input中的第一次位置
int begin=input.indexOf(os);
//字符出现在排序后input中的最后一次位置
int end=input.lastIndexOf(os);
//字符出现的次数
int value=end-begin+ 1 ;
if (value>=max) {
max=value;
maxString=os;
hm.put(maxString, max);
}
}
for (Map.Entry<String, Integer> enties: hm.entrySet()) {
if (enties.getValue()==max) {
System.out.print( "重复最多的字母是:" +enties.getKey());
System.out.println( "重复最多的次数是:" +enties.getValue());
}
}
}
}
|
运行结果如下:
?
1
2
3
|
set= [a, b, c, d, e, g, h, l, n, o, p, s, t, u, y]
lists= [a, b, c, d, e, e, g, g, g, h, l, n, n, n, n, o, o, p, p, s, t, t, t, u, y]
input= abcdeeggghlnnnnooppstttuy
|
重复最多的字母是:n重复最多的次数是:4
当有字符串重复的次数相同时,也可以将它们都打印出来。
如
?
1
2
3
4
|
public static void main(String[] args) {
String input = "abbcccddddeeeeeffffffaaaaabbb" ;
new SortTest().doString(input);
}
|
运行结果如下:
?
1
2
3
4
5
|
set= [a, b, c, d, e, f]
lists= [a, a, a, a, a, a, b, b, b, b, b, c, c, c, d, d, d, d, e, e, e, e, e, f, f, f, f, f, f]
input= aaaaaabbbbbcccddddeeeeeffffff
重复最多的字母是:f重复最多的次数是: 6
重复最多的字母是:a重复最多的次数是: 6
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/ouyang_peng/article/details/46526519
相关文章
猜你喜欢
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
PHP设计模式之中介者模式(Mediator Pattern)入门与应用案例详解
2025-05-29 63 -
2025-06-04 52
-
2025-05-29 35
-
2025-05-29 47
-
2025-05-25 66
热门评论