在一字符串中,出现次数最多的元素称为众数,试写出寻找众数的算法!谁能用正则写出来?

解决方案 »

  1.   

    针对于有序情况可以做的优化方法1、对于已检测出的最大计数Max,可以将查找步长增加至Max/2,这样凡是计数>=Max的,必将被检测到2次或以上。
    2、可以利用中位数,将原数组递归分段,来统计每个数重复了多少次,对于已经求出的Max,可以进行剪枝,如果数据大量重复,效率可大幅度提高,
    比如:如果存在某个元素占总元素的二分之一以上,仅需4次以内就可求得该数。
     private void button2_Click(object sender, EventArgs e) { int[] array = new int[] { 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 6, 6, 7, 8, 10, 11, 13, 13, 13, 13, 13, 13, 13, 13, 19, 20, 21, 24 }; int[] k; //有序情况找众数法  k = Zhongshu1(array); array = new int[] { 20, 21, 13, 24, 1, 13, 13, 13, 1, 2, 10, 11, 13, 13, 2, 2, 2, 3, 3, 3, 2, 6, 7, 2, 2, 2, 6, 8, 13, 13, 13, 2, 19 }; //无序情况找众数  k = ZhongshuNoSort(array); } //有序情况下的找众数  private int[] Zhongshu1(int[] source) { int currentValue = source[0]; int[] zhongshu = new int[source.Length]; int max = 1; int count = 0; int position = -1; for (int i = 0; i <= source.Length; i++) { if (i < source.Length && source[i] == currentValue) count++; else  { if (count > max) { //如果出现了更大的计数  max = count; position = 0; zhongshu[0] = source[i - 1]; } else if (count == max) zhongshu[++position] = source[i - 1]; if (i < source.Length) { currentValue = source[i]; count = 1; } } } //如果没有众数  if (max <= 1) return null; Array.Resize(ref zhongshu,position + 1); return zhongshu; } //无序情况找众数  private int[] ZhongshuNoSort(int[] source) { int count; bool flag = false; Dictionary<int, int> myDict = new Dictionary<int, int>(); for (int i = 0; i < source.Length; i++) { if (myDict.TryGetValue(source[i],out count)) { flag = true; myDict[source[i]]++; } else  myDict.Add(source[i], 1); } //如果没有众数  if(!flag) return null; int max = 0; int position = 0; int[] zhongshu = new int[source.Length]; //遍历hash表,复杂度<= O(n)  foreach (KeyValuePair<int, int> myKey in myDict) { if (myKey.Value > max) { max = myKey.Value; position = 0; zhongshu[0] = myKey.Key; } else if (myKey.Value == max) zhongshu[++position] = myKey.Key; } Array.Resize(ref zhongshu, position + 1); return zhongshu; }
      

  2.   

    java正则实现不了。用个程序循环一下即可。
      

  3.   

    这道题目不适合用正则,可以采用String.replaceAll方法来做//package com.ricky.www;/**
    在一字符串中,出现次数最多的元素称为众数,试写出寻找众数的算法!谁能用正则写出来?
    */import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    //import java.util.ArrayList;public class Test1{
    public static void main(String[] args){
    String content = "once angle come back to the heaven,one day";
    int num = getMax(content);
    System.out.println(num);
    } public static int getMax(String content){
    String str = null;
    String temp = null;
    int count = 0;
    int primary = content.length();
    while(primary != 0){
    primary = content.length();
    temp = content.substring(0,1);
    content = content.replaceAll(temp + "","");

    if(count < primary - content.length()){
    str = temp;
    count = primary - content.length();
    }
    primary = content.length();
    } System.out.println("出现次数最多的是 '" + str + "' 字符.");
    return count;
    }
    }
      

  4.   

    这道题目不适合用正则,可以采用String.replaceAll方法来做//package com.ricky.www;/**
    在一字符串中,出现次数最多的元素称为众数,试写出寻找众数的算法!谁能用正则写出来?
    */import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    //import java.util.ArrayList;public class Test1{
    public static void main(String[] args){
    String content = "once angle come back to the heaven,one day";
    int num = getMax(content);
    System.out.println(num);
    } public static int getMax(String content){
    String str = null;
    String temp = null;
    int count = 0;
    int primary = content.length();
    while(primary != 0){
    primary = content.length();
    temp = content.substring(0,1);
    content = content.replaceAll(temp + "","");

    if(count < primary - content.length()){
    str = temp;
    count = primary - content.length();
    }
    primary = content.length();
    } System.out.println("出现次数最多的是 '" + str + "' 字符.");
    return count;
    }
    }