找水王

这个问题是这样的:

  对于一个特别喜欢在论坛评论的人,他的评论的帖子数是最多的.

  好,下面先看代码

 1 import java.util.Random;
 2 
 3 public class Find {
 4     public static final String NUMBERCHAR = "12345959";
 5 
 6     public static void main(String[] args) {
 7         Random random = new Random();
 8         int a[] = new int[2000];
 9         for (int i = 0; i < a.length; i++) {
10             a[i] = random.nextInt(NUMBERCHAR.length());
11         }
12 
13         int sum = 0, count = 0, result = 0;
14         for (int i = 0; i < a.length; i++) {
15             for (int j = i; j < a.length - i; j++) {
16                 if (a[i] == a[j]) {
17                     count++;
18                 }
19             }
20             if (count > sum) {
21                 result = a[i];
22                 sum = count;
23             }
24             count = 0;
25         }
26         
27         System.out.println("水王最可能是:" + result);
28         System.out.println("出现的次数为:" + sum);
29     }
30 }

 

也可以先将重复的值去掉,然后重新进行排序

1         // 去掉重复
2         Set<Object> set = new HashSet<Object>();
3         for (Object o : a) {
4             set.add(o);
5         }
6 
7         //将得到的数组放到新的数组中
8         Object[] b = set.toArray();        

 

这是比较简单的方法,通过产生随机数储存到数组,来对数组中各元素进行比较统计,然后将统计出的结果进行输出.但是这样的时间复杂度是O(n^2),那么,有没有更加简单的方法来解决这个问题呢?

 

我们知道,在java.util 中包含我们最常用的集合类,最常用的就是List和Map.那么,在Map中,我们通过使用他的储存映射,可不可以解决这个问题呢?

  Map中提供HashMap来通过键的值储存数据排序,并可通过put函数进行插入或者统计;而在进行遍历时,可以通过增强for循环遍历,使用entrySet()进行遍历.那么,我们怎么来使用他们来解决我们上述问题呢?

请看代码:

 1 import java.util.Collection;
 2 import java.util.Collections;
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 import java.util.Random;
 6 
 7 public class Find_3 {
 8 
 9     public static final String NUMBERCHAR = "123456789";
10 
11     public static void main(String[] args) {
12 
13         Random random = new Random();
14         int a[] = new int[2000];
15         for (int i = 0; i < a.length; i++) {
16             int num = random.nextInt(NUMBERCHAR.length());
17             // String c = new DecimalFormat("000").format(num);//产生随机数,在前面补0,返回类型为String
18             // a[i]= Integer.parseInt(c);
19             a[i] = num;
20             // System.out.println(a[i]);
21 
22         }
23 
24         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
25         for (int i = 0; i < a.length; i++) {
26             if (map.containsKey(a[i])) {//通过值来统计数组的相同元素的个数
27                 int sum = map.get(a[i]);
28                 map.put(a[i], sum + 1);
29             } else {
30                 map.put(a[i], 1);
31             }
32         }
33 
34         Collection<Integer> count = map.values();
35         int mcount = Collections.max(count);
36         int number = 0;
37 
38         for (Map.Entry<Integer, Integer> entry : map.entrySet()) {//将最后的元素进行遍历取出
39 
40             if (mcount == entry.getValue()) {
41                 number = entry.getKey();
42             }
43         }
44         System.out.println("出现次数最多的数字为:" + number);
45         System.out.println("一共出现" + mcount + "次");
46 
47     }
48 }

好,这样就解决了一个问题.

 

截图:

 

 总结:

这又让我想起了我们以前做的一个关于一维数组的最大子数组的问题,当时我们也提到过时间复杂度的问题,那么,从这个问题中,我们是否也可以找到一个求解这种问题的方法呢?

 

 

 

  

posted @ 2019-06-01 13:50  啾啾丶丶  阅读(120)  评论(0编辑  收藏  举报