java吧 关注:1,242,271贴子:12,713,832
  • 11回复贴,共1

哪个大神抽个空做下这题

只看楼主收藏回复

一个字符串中可能包含a-z中的多个字符,如有重复,如String data=“arteyzuwdgfdgY” ,求出现次数最多的那个字母及出现次数,如有多个重复则都求出。
谁能把源代码写出来看看


1楼2016-09-26 11:52回复
    有福利吗


    3楼2016-09-26 11:53
    回复
      下载贴吧客户端发语音!


      来自iPhone客户端4楼2016-09-26 11:54
      回复
        小白求教


        5楼2016-09-26 11:57
        回复
          百度


          来自Android客户端6楼2016-09-26 12:14
          回复


            来自Android客户端7楼2016-09-26 12:15
            回复
              有人能给我写吗


              8楼2016-09-26 15:55
              回复
                高级点的就用分词器加lucene统计


                IP属地:广东来自Android客户端10楼2016-09-27 15:22
                回复
                  public static void main(String[] args) {
                  test("qwwtttfffggg");
                  }
                  public static void test(String str) {
                  int max_length = 0;
                  int temp_length = 0;
                  List<String> storeword = new ArrayList<String>();
                  List<String> storelength = new ArrayList<String>();
                  while (str.length() > 0) {//统计得到各个字符分别出现的次数
                  int length = str.length();
                  String first = str.substring(0, 1);
                  str = str.replaceAll(first, "");
                  if (max_length <= length - str.length()) {
                  temp_length = length - str.length();
                  if(temp_length>=max_length){
                  max_length = temp_length;
                  storeword.add(first);
                  storelength.add(max_length+"");
                  }
                  }
                  }
                  List<String> count = new ArrayList<String>();
                  for(int i=0;i<storelength.size();i++){
                  temp_length = Integer.valueOf(storelength.get(i));
                  if(temp_length>max_length){
                  max_length = temp_length; //得到最多次数
                  }
                  }
                  for(int k = 0; k<storelength.size();k++){
                  if(max_length==Integer.valueOf(storelength.get(k))){
                  count.add(k+"");
                  }
                  }
                  System.out.println("出现次数最多的字符为:");
                  for(int l = 0;l<count.size();l++){
                  System.out.print(storeword.get(Integer.valueOf(count.get(l)))+","); //得到出现次数对多的字符
                  }
                  System.out.println();
                  System.out.println("出现的次数为"+max_length+"次");
                  }


                  11楼2016-09-27 17:39
                  收起回复
                    再稍微修改了下
                    public static void main(String[] args) {
                    while(true){
                    System.out.println("请输入字符:");
                    Scanner scan = new Scanner(System.in);
                    String date = scan.nextLine();
                    test(date);
                    }
                    }
                    public static void test(String str) {
                    int max_length = 0;
                    int temp_length = 0;
                    List<String> storeword = new ArrayList<String>();
                    List<String> storelength = new ArrayList<String>();
                    while (str.length() > 0) {//统计得到各个字符分别出现的次数
                    int length = str.length();
                    String first = str.substring(0, 1);
                    str = str.replaceAll(first, "");
                    if (max_length <= length - str.length()) {
                    temp_length = length - str.length();
                    if(temp_length>=max_length){
                    max_length = temp_length;
                    storeword.add(first);
                    storelength.add(max_length+"");
                    }
                    }
                    }
                    List<String> count = new ArrayList<String>();
                    for(int i=0;i<storelength.size();i++){
                    temp_length = Integer.valueOf(storelength.get(i));
                    if(temp_length>max_length){
                    max_length = temp_length; //得到最多次数
                    }
                    }
                    for(int k = 0; k<storelength.size();k++){
                    if(max_length==Integer.valueOf(storelength.get(k))){
                    count.add(k+"");
                    }
                    }
                    System.out.println("出现次数最多的字符为:");
                    for(int l = 0;l<count.size();l++){
                    System.out.print(storeword.get(Integer.valueOf(count.get(l)))+","); //得到出现次数对多的字符
                    }
                    System.out.println();
                    System.out.println("出现的次数为"+max_length+"次");
                    }


                    12楼2016-09-27 17:45
                    回复