zoukankan      html  css  js  c++  java
  • 这行字符串中出现频率最高的字符

    1. 给定一行字符串,求出这行字符串中出现频率最高的字符,字符串中含有标点符号,字符不区分大小写。如果出现频率相同时,输出先出现在字符串中的字符。

     1 import java.util.HashMap;
     2 import java.util.Map;
     3 import java.util.Map.Entry;
     4 import java.util.Scanner;
     5 public class Frequency {
     6 
     7     public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9         Scanner sc = new Scanner(System.in);
    10         String input = sc.nextLine();
    11         String strs = input.replaceAll(" ","").toLowerCase();
    12         Map<Character, Integer> map = new HashMap<Character, Integer>();
    13         Character CharStr = null;
    14         Integer CountmaxLength = 0;
    15         for (Character temp : strs.toCharArray()) {
    16             if (map.containsKey(temp)) { 
    17                 map.put(temp, map.get(temp) + 1);
    18             } else {
    19                 map.put(temp, 1);
    20             }
    21         }
    22         for (Entry<Character, Integer> entry : map.entrySet()) {
    23             if (entry.getValue() > CountmaxLength) {
    24                 CharStr = entry.getKey();
    25                 CountmaxLength = entry.getValue();
    26             }    
    27         }
    28         
    29         System.out.println(Character.toUpperCase(CharStr)+CountmaxLength.toString());
    30     }
    31 
    32 }
  • 相关阅读:
    java面向接口编程之适配器模式
    java面向接口编程之制定标准和简单工厂模式
    java接口的使用
    java接口(interface)
    java抽象类的体现-模板模式
    java 抽象类
    java final 关键字
    Singleton模式(单例模式) 饿汉式和懒汉式
    java构造方法的私有化
    java代码块
  • 原文地址:https://www.cnblogs.com/wangyufeiaichiyu/p/11238315.html
Copyright © 2011-2022 走看看