在一字符串中,出现次数最多的元素称为众数,试写出寻找众数的算法!谁能用正则写出来?
解决方案 »
- socket文件传输问题
- Arrays.asList是如何做到返回固定长度的list的?
- 如何输出类似巴斯卡三角形
- 请您进来看看这段代码有什么问题,非常感谢。
- 今天的面试题,好难喔,我想没多少人会做吧!!!!
- 连老师也解决不了的问题!望高手来挑战一下!
- 请教jbuilder9中的一个简单操作
- 问个初级一点的问题:j2sdk1.4.0_01中2和1.4.0各代表什么意思。
- 请问JavaScript可以和Applet进行数据交互或函数调用吗?如果可以怎么进行呢?谢谢
- 关于文件解压以后对其进行删除操作的问题!
- 【线程】大家来看看如何加锁?
- URLConnection类中的getInputStream方法是如何返回一个InputStream 对象的?
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; }
在一字符串中,出现次数最多的元素称为众数,试写出寻找众数的算法!谁能用正则写出来?
*/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;
}
}
在一字符串中,出现次数最多的元素称为众数,试写出寻找众数的算法!谁能用正则写出来?
*/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;
}
}