金山公司面試題:一個字符串中可能包含a~z中的多個字符,如有重復(fù),如String data="aavzcadfdsfsdhshgWasdfasdf",求出現(xiàn)次數(shù)最多的那個字母及次數(shù),如有多個重復(fù)的則都求出。
此題的解題思路如下:
引入TreeSet:通過集合快速找到所有出現(xiàn)過的字符串
引入ArrayList:為了快速排序,再通過StringBuffer生成排序后的字符串
通過String的indexOf方法和lastIndexOf方法來計算每個字符串出現(xiàn)的次數(shù)最大值
使用HashMap存儲出現(xiàn)多的字符串和次數(shù)
代碼如下:
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快速找到所有出現(xiàn)的字符串 * 將輸入的字符串按升序排列 */ //將String轉(zhuǎn)換為字符數(shù)組 char [] chars=input.toCharArray(); ArrayList<String> lists= new ArrayList<String>(); //TreeSet是一個有序集合,TreeSet中的元素將按照升序排列 //通過TreeSet的不重復(fù)性,快速找到所有出現(xiàn)的字符串 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); //將排序好的字符數(shù)組轉(zhuǎn)換為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); /** * 第二步: 找出出現(xiàn)相同的字符并記錄出現(xiàn)多少次 */ //最多重復(fù)出現(xiàn)多少次 int max= 0 ; //重復(fù)出現(xiàn)的字符 String maxString= "" ; /*//重復(fù)出現(xiàn)的字符列表 ArrayList<String> maxList=new ArrayList<String>();*/ //用來保存出現(xiàn)最多的字符串和次數(shù) HashMap<String, Integer> hm= new HashMap<String, Integer>(); //將出現(xiàn)過的字符遍歷 Iterator<String> its=set.iterator(); while (its.hasNext()) { String os=its.next(); //字符出現(xiàn)在排序后input中的第一次位置 int begin=input.indexOf(os); //字符出現(xiàn)在排序后input中的最后一次位置 int end=input.lastIndexOf(os); //字符出現(xiàn)的次數(shù) 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( "重復(fù)最多的字母是:" +enties.getKey()); System.out.println( "重復(fù)最多的次數(shù)是:" +enties.getValue()); } } } } |
運行結(jié)果如下:
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 |
重復(fù)最多的字母是:n重復(fù)最多的次數(shù)是:4
當(dāng)有字符串重復(fù)的次數(shù)相同時,也可以將它們都打印出來。
如
1
2
3
4
|
public static void main(String[] args) { String input = "abbcccddddeeeeeffffffaaaaabbb" ; new SortTest().doString(input); } |
運行結(jié)果如下:
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ù)最多的字母是:f重復(fù)最多的次數(shù)是: 6 重復(fù)最多的字母是:a重復(fù)最多的次數(shù)是: 6 |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/ouyang_peng/article/details/46526519