[java] 找出字符串中出现最多的字符和出现的次数
生活随笔
收集整理的這篇文章主要介紹了
[java] 找出字符串中出现最多的字符和出现的次数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
逛園子看到一童鞋做的華為上機(jī)題目,寫來(lái)好長(zhǎng)的代碼,懶得看,感覺不可能這么難,于是動(dòng)手敲了下。
?
import java.util.Scanner;public class StringTest {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner scanner = new Scanner(System.in);String A = scanner.nextLine();//find the char which showS the mostint[] charTable=new int[123];//A-65 Z-90//a-97 z-122int maxIndex=0;for(char c:A.toCharArray()){ charTable[c]=1+charTable[c]; if(charTable[c]>charTable[maxIndex])maxIndex=c;}System.out.println("the char is: "+(char)maxIndex+" - Times:"+charTable[maxIndex]); }}?
總結(jié)
以上是生活随笔為你收集整理的[java] 找出字符串中出现最多的字符和出现的次数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用python做采集时相对路径转换成绝对
- 下一篇: 北京爱情故事-经典语录