300分!给一个数组 求里面出现次数最多的元素和其次数 另:需要加上时间复杂度,否则只给5分。最优算法给100分

一头头 2011-09-18 07:27:33
要求很清楚了。给一个数组。然后求里面出现次数最多的数字,并且求这个数字在数组里面出现的次数。

另:需要给出算法的时间复杂度。

写出算法给5分,写出算法+时间复杂度给50分。顶一下给2分。最优算法给100分。

下面是测试用的数组,一共1000个数字。


int nums []= { 2,4,3,6,9,2,1,9,3,7,4,1,0,1,2,2,1,5,8,8,4,1,1,7,6,1,9,6,5,5,6,9,1,8,7,9,9,1,6,3,4,4,6,5,3,1,1,5,7,7,8,0,1,9,3,5,2,4,4,4,0,8,5,3,2,1,9,6,7,9,8,2,6,4,1,7,2,0,9,1,4,6,6,5,5,6,3,5,3,3,8,3,6,3,1,0,6,2,7,1,8,4,7,3,3,1,3,1,3,7,4,0,9,7,9,4,6,0,2,1,9,2,8,8,0,1,6,4,4,4,0,4,4,3,3,4,9,3,0,3,5,6,3,2,4,7,5,1,3,7,4,4,4,8,5,7,9,8,7,1,3,1,0,7,8,4,6,9,5,2,8,5,2,7,3,8,4,4,6,3,5,9,2,8,9,8,7,1,6,5,1,4,4,1,5,8,7,5,9,0,7,4,6,5,3,6,1,2,9,6,2,1,1,8,4,1,2,6,6,0,0,4,9,0,8,9,5,7,7,2,3,8,4,2,7,8,7,5,3,4,4,6,9,5,0,4,8,7,1,6,3,1,9,7,4,9,0,4,0,3,5,8,0,8,6,7,8,1,0,2,0,8,3,0,1,1,7,6,6,2,2,8,1,7,8,2,5,2,5,8,4,0,0,2,4,2,8,3,4,2,5,6,4,4,8,8,7,2,3,8,0,0,6,4,2,1,6,7,1,5,3,7,1,2,3,4,5,1,8,1,5,1,3,1,8,5,1,0,1,1,8,4,6,2,6,8,0,2,7,4,3,0,8,5,0,1,2,8,1,2,9,0,4,8,5,5,8,1,5,8,9,5,2,9,8,2,2,0,2,6,1,9,0,5,8,4,6,9,1,6,8,0,7,0,3,5,5,9,5,9,9,6,1,9,2,8,2,7,6,8,0,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7,1,6,3,8,4,5,4,1,2,9,0,5,7,1,7,1,2,9,3,8,1,2,7,5,3,8,7,1,7,3,0,8,2,9,6,8,6,0,9,9,5,4,3,8,1,2,2,2,2,3,9,8,5,5,5,5,2,9,8,8,1,3,0,1,1,9,3,7,7,7,2,2,3,6,3,7};
...全文
757 75 打赏 收藏 转发到动态 举报
写回复
用AI写文章
75 条回复
切换为时间正序
请发表友善的回复…
发表回复
一头头 2011-09-25
  • 打赏
  • 举报
回复
不好意思啊 各位

实在是人多分少 我只能开300分的帖子 所以这里给没有给分的或者是少给分的朋友说声对不起哇

真的不好意思

hellcomer 2011-09-20
  • 打赏
  • 举报
回复
map 最简洁,但时间复杂度并非最低,而且实用map相对的占用的空间复杂度也偏高,但简单易懂,使用有上限的数组时间复杂度最低,但是空间复杂度就相对高些,而排序再计算的方式,时间复杂度和空间复杂度都蛮高的。。。
feifeikub 2011-09-20
  • 打赏
  • 举报
回复
先顶一个,然后想想解决的办法。
zhoudianlong 2011-09-20
  • 打赏
  • 举报
回复
过来顶下。。学习学习。
chenliuyang 2011-09-20
  • 打赏
  • 举报
回复
[Quote=引用 68 楼 benac 的回复:]

Java code
public static void main(String[] args) {
int i,j,h,max=0,num=0;
int nums []= {2,4,3,6,9,2,1,9,3,7,4,1,0,1,2,2,1,5,8,8,4,1,1,7,6,1,9,6,5,5,6,9,1,8,7,9,9,1,6,3,4,4,6,5,3……
[/Quote]
你抄袭我的不厚道
陈绍唐我爱你 2011-09-20
  • 打赏
  • 举报
回复
这个是老问题了。。
回想一下。。呵呵。。
aken315 2011-09-19
  • 打赏
  • 举报
回复
我也来学习一下。
禀赋苍天 2011-09-19
  • 打赏
  • 举报
回复
楼主描述的不够清楚,什么时间复杂度??
liyang1271989 2011-09-19
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
原来上面有很多兄弟都是这样想的。
另:用map的时间复杂度绝对不只O(N+M)
当然如果用空间换时间的办法,定义出最小数至最大数长度的数组,分别表示最每个数的次数是可以的。。。这样的复杂度差不多有O(2N+M)
  • 打赏
  • 举报
回复
算法懒的写了。
思路是先排序,再遍历一遍。
排序的时间复杂度最快为nlogn
则这个算法的时间复杂度为O(nlogn+n)
dengbojinig 2011-09-19
  • 打赏
  • 举报
回复
24楼的应该是时间复杂度最少的 学习 Up!!
Alexander 2011-09-19
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 qybao 的回复:]

如果是LZ举例的数字的话,投机取巧一下
Java code
int[] cnt = {0,0,0,0,0,0,0,0,0,0}; //分别记录数字0,1,2,3,4,5,6,7,8,9的次数
int[] max = {0,0}; //记录出现最多的数字以及出现次数,max[0]次数,max[1]数字
for (int n : num) { //遍历数组
cnt[n]++; //该数字……
[/Quote]
支持阿宝的方法。由于是无序数列,循环一次应该是必须的,所以O(n)应该是最小的时间复杂度了。

其实阿宝的方法不用拘泥于10个数的上限,因为频率最高的数在首次循环中就已经找到了:
[code=Java]
public static void main(String[] args) {
int nums[] = { 2, 4, 3, 6, 9, 2, 1, 9, 3, 7, 4, 1, 0, 1, 2, 2, 1, 5, 8,
8, 4, 1, 1, 7, 6, 1, 9, 6, 5, 5, 6, 9, 1, 8, 7, 9, 9, 1, 6, 3,
4, 4, 6, 5, 3, 1, 1, 5, 7, 7, 8, 0, 1, 9, 3, 5, 2, 4, 4, 4, 0,
8, 5, 3, 2, 1, 9, 6, 7, 9, 8, 2, 6, 4, 1, 7, 2, 0, 9, 1, 4, 6,
6, 5, 5, 6, 3, 5, 3, 3, 8, 3, 6, 3, 1, 0, 6, 2, 7, 1, 8, 4, 7,
3, 3, 1, 3, 1, 3, 7, 4, 0, 9, 7, 9, 4, 6, 0, 2, 1, 9, 2, 8, 8,
0, 1, 6, 4, 4, 4, 0, 4, 4, 3, 3, 4, 9, 3, 0, 3, 5, 6, 3, 2, 4,
7, 5, 1, 3, 7, 4, 4, 4, 8, 5, 7, 9, 8, 7, 1, 3, 1, 0, 7, 8, 4,
6, 9, 5, 2, 8, 5, 2, 7, 3, 8, 4, 4,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, 7, 6, 1, 9, 6, 3, 0, 4, 1, 8, 1, 3, 4, 5, 2, 0, 5, 5, 3,
8, 0, 9, 0, 1, 3, 4, 8, 1, 5, 0, 9, 6, 5, 0, 7, 2, 8, 2, 3, 2,
9, 9, 2, 4, 7, 0, 8, 6, 4, 0, 8, 6, 8, 3, 6, 6, 7, 5, 3, 0, 1,
3, 7, 1, 6, 3, 8, 4, 5, 4, 1, 2, 9, 0, 5, 7, 1, 7, 1, 2, 9, 3,
8, 1, 2, 7, 5, 3, 8, 7, 1, 7, 3, 0, 8, 2, 9, 6, 8, 6, 0, 9, 9,
5, 4, 3, 8, 1, 2, 2, 2, 2, 3, 9, 8, 5, 5, 5, 5, 2, 9, 8, 8, 1,
3, 0, 1, 1, 9, 3, 7, 7, 7, 2, 2, 3, 6, 3, 7 };
int count[] = new int[10000];
int max = 0;
Stack<Integer> maxIndexes = new Stack<Integer>();

for (int n : nums) {
if (++count[n] >= max) {
if (count[n] > max) {
maxIndexes.clear();
max = count[n];
}
maxIndexes.push(n);
}
}

// for (int i = 0; i < 10; i++)
// System.out.printf("%d出现的次数:%d\n", i, count[i]);

System.out.println("出现次数最多的数:");
while (!(maxIndexes.empty())) {
int n = maxIndexes.pop();
System.out.printf("%d 出现的次数: %d\n", n, count[n]);
}
}
[code]
softroad 2011-09-19
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 qybao 的回复:]

如果是LZ举例的数字的话,投机取巧一下
Java code
int[] cnt = {0,0,0,0,0,0,0,0,0,0}; //分别记录数字0,1,2,3,4,5,6,7,8,9的次数
int[] max = {0,0}; //记录出现最多的数字以及出现次数,max[0]次数,max[1]数字
for (int n : num) { //遍历数组
cnt[n]++; //该数字……
[/Quote]

不错不错。学习了。
wuhuipengwhp 2011-09-19
  • 打赏
  • 举报
回复
就是写一个程序判断一个数组中出现次数最多的那个元素。
方法一:

import java.util.*;
public class Test1{
public static void main(String args[]){
String[] s = {"a","b","a","a","b","c"};
HashMap< String, Integer> hm = new HashMap< String, Integer>();

for (int i = 0; i < s.length; i++) {
if (!hm.containsKey(s[i])) {
hm.put(s[i], 1);
} else {
hm.put(s[i], hm.get(s[i]).intValue()+1);
}
}

System.out.println(hm);
}
}
运行结果:

{a=3, c=1, b=2}

方法二:

import java.util.*;
public class FindMostEle {
private static LinkedHashMap<String, Integer> map;

public static LinkedHashMap<String, Integer> mostEle(String[] strArray){
map = new LinkedHashMap<String, Integer>();

String str = "";
int count = 0;
int result = 0;

for(int i=0; i<strArray.length; i++)
str += strArray[i];

for(int i=0; i<strArray.length; i++){
String temp = str.replaceAll(strArray[i], "");
count = (str.length() - temp.length())/strArray[i].length();

if (count > result){
map.clear();
map.put(strArray[i], count);
result = count;
}
else if(null == map.get(strArray[i]) && count == result)
map.put(strArray[i], count);
}
return map;
}

public static void main(String args[]){
String[] strArray = {"11", "11", "2", "2", "4", "5", "4"};

LinkedHashMap<String, Integer> result = mostEle(strArray);

ArrayList<Integer> c = new ArrayList<Integer>(result.values());
Set<String> s = result.keySet();

System.out.print("一共有"+ result.size() +"元素最多。它们分别是");
System.out.print(s);
System.out.println(",分别出现了"+ c.get(0) +"次。");

}
}
结果是:

一共有3元素最多。它们分别是[11, 2, 4],分别出现了2次。
方法三:
import java.util.*;
public class test {
public static void main(String[] args) {
int[] array = {1,2,2,3,4,4,5,6,7,7,8,9};
int iMaxCount=0;
//因为可能有多个元素的出现次数同时位居第一,
//所以结果应该是一个集合。用一个List来装载。
List< Integer> list=new ArrayList< Integer>();
for (int i=0;i< array.length;i++){
int iCount=0;
for (int j=0;j< array.length;j++){
if (array[i]==array[j])
iCount++;
}
if (iCount>iMaxCount){
list.clear();
list.add(array[i]);
iMaxCount=iCount;
}else if (iCount==iMaxCount){
if (list.indexOf(array[i])==-1)
list.add(array[i]);
}
}
System.out.println("出现次数最多的元素为"+list.toString());
}
}
运行结果:
出现次数最多的元素为[2, 4, 7]
盗圣 2011-09-19
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 huiwenjie168 的回复:]

Java code

int nums[] = {1,2,3,3,6,8,9,2,3,9,9,9,9,9};
Map<Integer, Integer> treeMap = new TreeMap<Integer, Integer>();
for(int i = 0; i < nums.length; i++) {
int tempNu……
[/Quote]这个是比较靠谱的算法,顶起!
ly_littlefish 2011-09-19
  • 打赏
  • 举报
回复
改正一个错误。
/*
* 返回一个数组,第一个元素放数字最多的一个那个数
* 第二个元素是出现的次数
*/
public int[] getMaxTimes(int[] myArr){
int[] retArr = new int[2];
int[] myNum = new int[10];
int maxTimes = 0;
int maxNum = 0;

for(int i = 0; i < myArr.length; ++i){
++myNum[myArr[i]];
}

//找到最大的数字和出现的次数
for(int j = 0; j < 10; ++j){
if(maxTimes < myNum[j]){
maxTimes = myNum[j];
maxNum = j;
}
}

retArr[0] = maxNum;
retArr[1] = maxTimes;

return retArr;
}
[Quote=引用 41 楼 fresh1985 的回复:]

如果数组中的数字仅仅是0-9的话,可以如下实现
Java code
/*
* 返回一个数组,第一个元素放数字最多的一个那个数
* 第二个元素是出现的次数
*/
public int[] getMaxTimes(int[] myArr){
int[] retArr = new int[2];
int[] myNum = ne……
[/Quote]
fengnenglu 2011-09-19
  • 打赏
  • 举报
回复
对整个数组遍历一遍,过程中统计出其中的每个数出现的次数,并记录出现最多的数以及次数,虽然时间复杂度较低,但开辟的空间比较大; 时间赋值度:O(num.length);

代码如下:
public static void main(String[] args)
{
int[] nums = {2, 4, 3, 6, 9, 2, 1, 9, 3, 7, 4, 1, 0, 1, 2, 2, 1, 5, 8, 8, 4, 1, 1, 7, 6, 1, 9, 6, 5, 5, 6, 9, 1, 8, 7, 9, 9, 1, 6, 3, 4, 4, 6, 5, 3, 1, 1, 5, 7, 7, 8, 0, 1, 9, 3, 5, 2, 4, 4, 4, 0, 8, 5, 3, 2, 1, 9, 6, 7, 9, 8, 2, 6, 4, 1, 7, 2, 0, 9, 1, 4, 6, 6, 5, 5, 6, 3, 5, 3, 3, 8, 3, 6, 3, 1, 0, 6, 2, 7, 1, 8, 4, 7, 3, 3,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, 2, 8, 1, 6, 0, 1, 2, 1, 9, 2, 8, 3, 2, 5, 8, 5, 9, 3, 2, 1, 6, 9, 7, 6, 5, 3, 0, 1, 4, 4, 2, 8, 4, 4, 2, 0, 9, 0, 7, 7, 7, 6, 4, 0, 6, 9, 2, 4, 7, 2, 3, 9, 9, 8, 3, 8, 3, 8, 1, 3, 6, 9, 8, 5, 8, 1, 4, 2, 0, 2, 9, 4, 2, 7, 6, 1, 9, 6, 3, 0, 4, 1, 8, 1, 3, 4, 5, 2, 0, 5, 5, 3, 8, 0, 9, 0, 1, 3, 4, 8, 1, 5, 0, 9, 6, 5, 0, 7, 2, 8, 2, 3, 2, 9, 9, 2, 4, 7, 0, 8, 6, 4, 0, 8, 6, 8, 3, 6, 6, 7, 5, 3, 0, 1, 3, 7, 1, 6, 3, 8, 4, 5, 4, 1, 2, 9, 0, 5, 7, 1, 7, 1, 2, 9, 3, 8, 1, 2, 7, 5, 3, 8, 7, 1, 7, 3, 0, 8, 2, 9, 6, 8, 6, 0, 9, 9, 5, 4, 3, 8, 1, 2, 2, 2, 2, 3, 9, 8, 5, 5, 5, 5, 2, 9, 8, 8, 1, 3, 0, 1, 1, 9, 3, 7, 7, 7, 2, 2, 3, 6, 3, 7};
int[] tellNum = new int[nums.length]; //统计num中的每个数字的个数
int num = 0; //出现最多的数字
int maxCount = 0; //记录最大的统计数。
for (int i = 0; i < nums.length; i++)
{
int tod = nums[i];
tellNum[tod] = tellNum[tod] + 1;
if(maxCount<tellNum[tod])
{
maxCount = tellNum[tod];
num = tod;
}
}
System.out.println("出现最多的数字是:"+num+"出现次数:"+maxCount);
}
ly_littlefish 2011-09-19
  • 打赏
  • 举报
回复
如果数组中的数字仅仅是0-9的话,可以如下实现
/*
* 返回一个数组,第一个元素放数字最多的一个那个数
* 第二个元素是出现的次数
*/
public int[] getMaxTimes(int[] myArr){
int[] retArr = new int[2];
int[] myNum = new int[10];
int maxTimes = 0;
int maxNum = 0;

for(int i = 0; i < myArr.length; ++i){
++myNum[myArr[i]];
}

//找到最大的数字和出现的次数
for(int j = 0; j < 9; ++j){
if(maxTimes < myNum[j]){
maxTimes = myNum[j];
maxNum = j;
}
}

retArr[0] = maxNum;
retArr[1] = maxTimes;

return retArr;
}
benac 2011-09-19
  • 打赏
  • 举报
回复
	public static void main(String[] args) {
int i,j,h,max=0,num=0;
int nums []= {2,4,3,6,9,2,1,9,3,7,4,1,0,1,2,2,1,5,8,8,4,1,1,7,6,1,9,6,5,5,6,9,1,8,7,9,9,1,6,3,4,4,6,5,3,1,1,5,7,7,8,0,1,9,3,5,2,4,4,4,0,8,5,3,2,1,9,6,7,9,8,2,6,4,1,7,2,0,9,1,4,6,6,5,5,6,3,5,3,3,8,3,6,3,1,0,6,2,7,1,8,4,7,3,3,1,3,1,3,7,4,0,9,7,9,4,6,0,2,1,9,2,8,8,0,1,6,4,4,4,0,4,4,3,3,4,9,3,0,3,5,6,3,2,4,7,5,1,3,7,4,4,4,8,5,7,9,8,7,1,3,1,0,7,8,4,6,9,5,2,8,5,2,7,3,8,4,4,6,3,5,9,2,8,9,8,7,1,6,5,1,4,4,1,5,8,7,5,9,0,7,4,6,5,3,6,1,2,9,6,2,1,1,8,4,1,2,6,6,0,0,4,9,0,8,9,5,7,7,2,3,8,4,2,7,8,7,5,3,4,4,6,9,5,0,4,8,7,1,6,3,1,9,7,4,9,0,4,0,3,5,8,0,8,6,7,8,1,0,2,0,8,3,0,1,1,7,6,6,2,2,8,1,7,8,2,5,2,5,8,4,0,0,2,4,2,8,3,4,2,5,6,4,4,8,8,7,2,3,8,0,0,6,4,2,1,6,7,1,5,3,7,1,2,3,4,5,1,8,1,5,1,3,1,8,5,1,0,1,1,8,4,6,2,6,8,0,2,7,4,3,0,8,5,0,1,2,8,1,2,9,0,4,8,5,5,8,1,5,8,9,5,2,9,8,2,2,0,2,6,1,9,0,5,8,4,6,9,1,6,8,0,7,0,3,5,5,9,5,9,9,6,1,9,2,8,2,7,6,8,0,8,7,1,4,2,2,7,6,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1,2,9,0,5,7,1,7,1,2,9,3,8,1,2,7,5,3,8,7,1,7,3,0,8,2,9,6,8,6,0,9,9,5,4,3,8,1,2,2,2,2,3,9,8,5,5,5,5,2,9,8,8,1,3,0,1,1,9,3,7,7,7,2,2,3,6,3,7};
int []count={0,0,0,0,0,0,0,0,0,0};//初始化数组记录每个数出现的次数,比如count[0]的值就是0出现的次数
//遍历nums[],记录每个数出现的次数
for(i=0;i<nums.length;i++)
{
h=nums[i];
count[h]=count[h]+1;

}
//获取count[]中最大的值,就是出现的次数,下标就是原来的数值
for(j=0;j<count.length;j++)
{
if(num<count[j])
{
num=count[j];
max=j;
}
}
System.out.println("最大数"+max);
System.out.println("出现次数"+num);
}
加载更多回复(53)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧