集合的面试应用

来源:互联网 发布:啥软件可以看禁播动漫 编辑:程序博客网 时间:2024/04/30 12:38
书写一段业务逻辑代码,完成如下的业务需求
----------------------------------------------
随便给一组数列如下[7,4,2,7,4,3,2,9,4,1,2,3]
要求按照如下规律输出数列的特征:
按照某个数字出现的次数的多少依次输出该数列中出现的数字,
如果有两个或者多个数字的出现次数相等,则,按照该数字的大小进行排列。

例如上面的数列的结果为:4,2,7,3,9,1


这道面试题涉及到集合的使用 HashMap ArrayList Hashset 都涉及到了 这里提供两种思路。给大家参考。

思路1:

首先把数组排序,这样数组中左右邻居要么相等,要么异样 如:[7,4,2,7,4,3,2,9,4,1,2,3]排序后为: 1,2, 2,2,3,3,4,4,4,7,7,9
然后遍历数组数组将元素出现次数和他本身保存在一个对象中 如 1 ,1出现1次    2出现3次 保存在所以想到创建Inter类 。下面的问题是遍历数组,得到元素出现的次数了,这里代码比较复杂:
1.创建Inter类(出现元素,出现元素的次数)实现Comparable接口 (排序需要实现)。
因排序按照某个数字出现的次数的多少依次输出该数列中出现的数字,
如果有两个或者多个数字的出现次数相等,则,按照该数字的大小进行排列。因此关键代码为:
@Overridepublic int compareTo(Object o) {Inter i=(Inter)o;if(i.times>this.times){return 1;}else if(i.times==this.times){return i.inter-this.inter;}else{return -1;}}

完整代码为:(get set 其实可以不要但是还是规范点吧 呵呵~)
public class Inter implements Comparable{private int inter;private int times;public int getInter() {return inter;}public void setInter(int inter) {this.inter = inter;}public int getTimes() {return times;}public void setTimes(int times) {this.times = times;}public Inter(int inter, int times) {this.inter = inter;this.times = times;}@Overridepublic String toString() {return this.inter+""+"  ";}public Inter() {}@Overridepublic int compareTo(Object o) {Inter i=(Inter)o;if(i.times>this.times){return 1;}else if(i.times==this.times){return i.inter-this.inter;}else{return -1;}}}
2.遍历数组 得到该元素出现的次数和本身代码附上:
public class TestInter {public static void main(String[] args) {int []a=new int []{7,4,2,7,4,3,2,9,4,1,2,3};ArrayList arr=new ArrayList();Arrays.sort(a);//将原始数组排序 从小到大int i=0;boolean flag=true;//创建循环标志int time=1;A:while(i<a.length){//java中创建标签符号while(flag){if(i==a.length-1){arr.add(new Inter(a[i],time));break;}if(a[i]==a[i+1]){i++;time++;continue;}else{arr.add(new Inter(a[i],time));time=1;i++;flag=false;if(i==a.length-1){arr.add(new Inter(a[i],1));break A;//此BREAK 可以结束标签所在的while循环}}}flag=true;}Collections.sort(arr);//实现Inter类中的排序方法! System.out.println(arr.toString());}

思路2:

上面的思路可能大多人接受不了,因为此面试题更注重人才JAVA类中集合的使用 所以有了以下算法。
1.遍历数组将数组中的元素放到HashSet这个集合里。注意HashSet 保存不重复元素。
2.将HashSet里的元素作为TreeMap的健(Key)。将遍历数组后将HashSet 某个Key在数组中出现的次数放在 TreeMap中。
3.根据TreeMap中value 和Key 将TreeMap元素排序。
4.将TreeMap 保存的对象放到ArrayList中 通过collections.sort()方法排序。
5.18行创建匿名内部类 用来实现ArrayList排序功能~即按照出现次数 ,若出现次数相同按照实际值排序
public static void mysort(){int []a=new int []{7,4,2,7,4,3,2,9,4,1,2,3};Set<Integer> ss=new HashSet<Integer>();for(int p:a){ss.add(p);}Map<Integer,Integer> map=new TreeMap<Integer,Integer>();for(int p:ss){int time=0;for(int s:a){if(p==s){time++;}}map.put(p, time);}ArrayList<Entry<Integer,Integer>> myList=new ArrayList<Entry<Integer,Integer>>(map.entrySet());Collections.sort(myList,new Comparator<Map.Entry<Integer,Integer>>(){//@Overridepublic int compare(Map.Entry<Integer, Integer> o1,Entry<Integer, Integer> o2) {if(o1.getValue()>o2.getValue()){return -1;}else if(o1.getValue().equals(o2.getValue())){return o2.getKey()-o1.getKey();}else{return 1;}}});for(Entry<Integer, Integer> p:myList){System.out.print(p.getKey()+" ");}}







原创粉丝点击