zoukankan      html  css  js  c++  java
  • 判断字符串中出现次数最多的字符和出现次数

    package my;
    
    import java.util.HashMap;
    
    public class FindStringMaxSolution {
        int findStrMax(String str){
            if(str.length() == 0 || str == null){
                return -1;
            }
            int max =0;
            HashMap<Character , Integer> map = new HashMap<>();
            for(int i= 0 ; i < str.length() ; i++){
                char c = str.charAt(i);
                if(map.containsKey(c)){
                    map.put(c ,map.get(c)+1);
                }else{
                    map.put(c,1);
                }
            }

    for(int j=0 ;j <str.length(); j++){ max = Math.max(max ,map.get(str.charAt(j))); } return max; } public static void main(String[] args){ String str ="3w3wrr3rk3jr"; int n = new FindStringMaxSolution().findStrMax(str); System.out.println(n); } }
    package my;
    
    import java.util.HashMap;
    
    public class FindStringMaxSolution {
        int findStrMax(String str){
            if(str.length() == 0 || str == null){
                return -1;
            }
            int max =0;
            char charmax = 0;
            HashMap<Character , Integer> map = new HashMap<>();
            for(int i= 0 ; i < str.length() ; i++){
                char c = str.charAt(i);
                if(map.containsKey(c)){
                    map.put(c ,map.get(c)+1);
                }else{
                    map.put(c,1);
                }
            }
            for(char key : map.keySet()){
                if(map.get(key) > max){
                    max = map.get(key);
                     charmax = key;
                }
            }
            System.out.println(charmax);
            return max;
        }
        public static void main(String[] args){
            String str ="3w3wrr3rk3jr";
            int n = new FindStringMaxSolution().findStrMax(str);
            System.out.println(n);
        }
    }
  • 相关阅读:
    手机各种JS语法,随时更新
    ionic上拉加载-下拉刷新
    JS获取浏览器信息及屏幕分辨率
    jQuery元素的显示、隐藏及动画
    jQuery原型
    表单序列化为对象
    html里的ajax数据传输
    面试技巧-互联网行业通吃
    jquery表单验证validate
    Javascript缓动动画原理
  • 原文地址:https://www.cnblogs.com/goodtest2018/p/13664423.html
Copyright © 2011-2022 走看看