題目:找出字符串中出現(xiàn)次數(shù)最多的字符和次數(shù)雾袱。
例如:“abbcdaaca”中出現(xiàn)次數(shù)最多的字符就是a械巡,出現(xiàn)次數(shù)是4
思路
由于需要找出字符和次數(shù)刹淌,這里可以利用Map鍵值對(duì)的形式來(lái)存儲(chǔ)饶氏,先通過(guò)遍歷字符串并比較字符,如果Map中已經(jīng)存在該字符就修改相對(duì)應(yīng)的value值芦鳍,也就是每次發(fā)現(xiàn)相同的字符就value++嚷往,這樣就得出了字符串中所有字符以及相對(duì)應(yīng)出現(xiàn)的次數(shù)的鍵值對(duì)對(duì)象。最后通過(guò)遍歷Map和比較找出出現(xiàn)次數(shù)最多的字符即可柠衅。
HashMap<Character,Integer> map = new HashMap<Character,Integer> ();
//獲取到字符串的字符和出現(xiàn)的次數(shù)的鍵值對(duì)對(duì)象
for(int i=0;i<str.length();i++)
{
char temp = str.charAt(i);
if(map.containsKey(temp))
{
map.put(temp,map.get(temp)+1);//如果map中已經(jīng)存在就重新改變對(duì)應(yīng)的value值+1
}
else
{
map.put(temp,1);
}
}
獲取到Map對(duì)象通過(guò)遍歷比較找出出現(xiàn)次數(shù)最對(duì)的鍵值對(duì)
int count = 0;
Character temp = null;
for (Map.Entry<Character,Integer> entry:map.entrySet()
) {
if(entry.getValue()>count)
{
count = entry.getValue();
temp = entry.getKey();
}
}
System.out.println("出現(xiàn)最多次的字符是:"+temp);
System.out.println("出現(xiàn)次數(shù):"+count);
有個(gè)問(wèn)題要解決
如果字符串是“aaaabbbbcc”皮仁,出現(xiàn)次數(shù)相同并且都是最多的那怎么辦呢?
這里我覺(jué)得可以再創(chuàng)建一個(gè)teamMap來(lái)存放出現(xiàn)次數(shù)最多有多個(gè)的情況菲宴,然后每次返回結(jié)果前就去看teamMap是否存在兩個(gè)或多于兩個(gè)的情況贷祈,存在的話(huà)就都打印出來(lái)。
int count = 0;
Character temp = null;
//存放臨時(shí)結(jié)果的map
HashMap<Character,Integer> tempMap = new HashMap<Character, Integer>();
for (Map.Entry<Character,Integer> entry:map.entrySet()
) {
if(entry.getValue()>count&&tempMap.isEmpty())
{
count = entry.getValue();
temp = entry.getKey();
tempMap.put(temp,count);
}
else if (entry.getValue()>count&&!tempMap.isEmpty())
{
tempMap.clear();//存在次數(shù)更多的字符喝峦,直接清空之前的元素
count = entry.getValue();
temp = entry.getKey();
tempMap.put(entry.getKey(),entry.getValue());
}
else if(entry.getValue()==count)
{
tempMap.put(entry.getKey(),entry.getValue());
}
}
if(tempMap.size()>1)
{
System.out.println("出現(xiàn)最多次數(shù)的字符有多個(gè)法势誊,分別是:");
for (Map.Entry<Character,Integer>entry:tempMap.entrySet()
) {
System.out.println(entry.getKey());
}
}
else
{
System.out.println("出現(xiàn)最多次的字符是:"+temp);
}
完整代碼
public static void getMaxTimeChar(String str)
{
HashMap<Character,Integer> map = new HashMap<Character,Integer> ();
//獲取到字符串的字符和出現(xiàn)的次數(shù)的鍵值對(duì)對(duì)象
for(int i=0;i<str.length();i++)
{
char temp = str.charAt(i);
if(map.containsKey(temp))
{
map.put(temp,map.get(temp)+1);//如果map中已經(jīng)存在就重新改變對(duì)應(yīng)的value值+1
}
else
{
map.put(temp,1);
}
}
int count = 0;
Character temp = null;
HashMap<Character,Integer> tempMap = new HashMap<Character, Integer>();
for (Map.Entry<Character,Integer> entry:map.entrySet()
) {
/* System.out.println(entry.getKey());
System.out.println(entry.getValue());*/
if(entry.getValue()>count&&tempMap.isEmpty())
{
count = entry.getValue();
temp = entry.getKey();
tempMap.put(temp,count);
}
else if (entry.getValue()>count&&!tempMap.isEmpty())
{
tempMap.clear();
count = entry.getValue();
temp = entry.getKey();
tempMap.put(entry.getKey(),entry.getValue());
}
else if(entry.getValue()==count)
{
tempMap.put(entry.getKey(),entry.getValue());
}
}
if(tempMap.size()>1)
{
System.out.println("出現(xiàn)最多次數(shù)的字符有多個(gè),分別是:");
for (Map.Entry<Character,Integer>entry:tempMap.entrySet()
) {
System.out.println(entry.getKey());
}
System.out.println("出現(xiàn)次數(shù)是:"+count);
}
else
{
System.out.println("出現(xiàn)最多次的字符是:"+temp+">>>>>>>>出現(xiàn)次數(shù):"+count);
}
}
引申:如果要找出第二多字符怎么實(shí)現(xiàn)呢谣蠢?
無(wú)外乎就是在遍歷的時(shí)候再多進(jìn)行一次比較粟耻,可以分別創(chuàng)建最大和第二的兩個(gè)變量來(lái)存儲(chǔ),然后在比較的時(shí)候再增加一層else if.
int maxCount = 0
Character maxChar= null;
int secCount = 0;
Character secChar= null;
if(entry.getValue>maxCount)
{
maxCount = entry.getValue();
maxChar = entry.getKey();
}else if(entry.getValue>secCount)
{
secCount = entry.getValue();
secChar = entry.getKey();
}
上次面試的時(shí)候被問(wèn)到了眉踱,所以在這里做個(gè)記錄挤忙。加油。