集合的面试应用
来源:互联网 发布:啥软件可以看禁播动漫 编辑:程序博客网 时间:2024/04/30 12:38
书写一段业务逻辑代码,完成如下的业务需求
----------------------------------------------
随便给一组数列如下[7,4,2,7,4,3,2,9,4,1,2,3]
要求按照如下规律输出数列的特征:
按照某个数字出现的次数的多少依次输出该数列中出现的数字,
如果有两个或者多个数字的出现次数相等,则,按照该数字的大小进行排列。
----------------------------------------------
随便给一组数列如下[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()+" ");}}
- 集合的面试应用
- 我的面试提集合
- 关于Java集合的面试
- Python的应用集合
- 集合类的应用
- 集合Properties的应用
- ArrayList 集合的应用
- Properties集合的应用
- python集合的应用
- 面试集合
- 面试集合
- 集合面试
- 集合的应用----集合覆盖分析
- 面试中问到的集合方面的问题
- 英语面试常见的问题集合
- HashMap--面试必问的集合类
- 面试经常问到的集合类
- 有关Google面试的资料集合
- JavaScript(07): 实例2---网页广告漂浮效果(面向对象版)
- Html5系列之Canvas
- linux进程内存分布
- Eclipse关联java源代码
- 关于ios实现键盘隐藏的方法小节
- 集合的面试应用
- ExtJS 4 组件扩展(一)
- 常用的android指令
- Android游戏框架AndEngine使用入门
- Android 相对布局
- iPhone之手动切换View
- 我看数据安全,服务用户,符合常理
- hdu 3792 Twin Prime Conjecture n之内的孪生素数个数
- 云计算的两种形式