LeetCode 347. 前K个高频元素 692.前K个高频单词

前K个高频元素:
在这里插入图片描述
分析:
这题要求从所给数组中筛选出出现频率前k高的元素,因此需要统计每个数出现的次数,使用HashMap记录数据和它们出现的频次,读到的数如果在Map中不存在,则添加到Map中并记录频次为1,一旦第二次读到,就把计数加一。

HashMap<Integer, Integer> map=new HashMap<>();
        for(int i=0;i<nums.length;i++){
        	if(map.containsKey(nums[i])){
        		int num=map.get(nums[i]);
        		num++;
        		map.replace(nums[i], num);
        	}else{
        	  map.put(nums[i], 1);
        	}
        }

因为Map不支持排序,我们需要把数据取出到集合中,再进行排序,而同时考虑到数值和其出现的频次是不能分开的,于是我设计了一个内部类来记录这些信息,它支持排序,实现了Comparable接口。

class Entry implements Comparable<Entry>{
		public int num;
		public int count;
		public Entry(int num, int count) {
			this.num = num;
			this.count = count;
		}		
		public int compareTo(Entry o) {			
			return this.count-o.count;
		}		
	}

把Map中的数据迭代取出到数组,数组中的元素是Entry,最后进行排序。

Iterator<Integer> iterator=map.keySet().iterator();
        ArrayList<Entry> arrayList=new ArrayList<>();
        while(iterator.hasNext()){
        	int key=iterator.next();
        	int val=map.get(key);
        	arrayList.add(new Entry(key, val));
        }
        Collections.sort(arrayList);

记录出现频次前k的元素并返回。

ArrayList<Integer> arrayList2=new ArrayList<>();
        for(int i=0;i<k;i++){
        	arrayList2.add(arrayList.get(arrayList.size()-1-i).num);
        }

完整代码(java):

class Solution {
	class Entry implements Comparable<Entry>{
		public int num;
		public int count;
		public Entry(int num, int count) {
			this.num = num;
			this.count = count;
		}
		
		public int compareTo(Entry o) {			
			return this.count-o.count;
		}		
	}
    public List<Integer> topKFrequent(int[] nums, int k) {
        HashMap<Integer, Integer> map=new HashMap<>();
        for(int i=0;i<nums.length;i++){
        	if(map.containsKey(nums[i])){
        		int num=map.get(nums[i]);
        		num++;
        		map.replace(nums[i], num);
        	}else{
        	  map.put(nums[i], 1);
        	}
        }
        Iterator<Integer> iterator=map.keySet().iterator();
        ArrayList<Entry> arrayList=new ArrayList<>();
        while(iterator.hasNext()){
        	int key=iterator.next();
        	int val=map.get(key);
        	arrayList.add(new Entry(key, val));
        }
        Collections.sort(arrayList);
        ArrayList<Integer> arrayList2=new ArrayList<>();
        for(int i=0;i<k;i++){
        	arrayList2.add(arrayList.get(arrayList.size()-1-i).num);
        }
        return arrayList2;
    }
}

前K个高频单词:
在这里插入图片描述
分析:
和上一题的解法类似,只是这题需要多考虑一些地方,比如需要在比较的时候加上首字母比较,首字母相同还需要比较下一个字母,当字母都相同时还要再区分一下长度,比如a和aaa。
完整代码:

class Solution {
	ArrayList<Entry> arrayList=new ArrayList<>();
	class Entry implements Comparable<Entry>{
		public String name;
		public int number;
		public Entry(String name,int num){
			this.name=name;
			this.number=num;
		}		
		public int compareTo(Entry o) {	
			if(this.number==o.number){
				int aL=this.name.length();
				int oL=o.name.length();
				for(int i=0;i<(aL>oL?o.name.length():this.name.length());i++){
					if(o.name.charAt(i)==this.name.charAt(i)){
						continue;
					}else{
						return o.name.charAt(i)-this.name.charAt(i);
					}
				}
				return o.name.length()-this.name.length();
			}else{
			    return this.number-o.number;
			}
		}		
	}
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String, Integer> map=new HashMap<>();
        for(int i=0;i<words.length;i++){
        	if(map.containsKey(words[i])){
        		int num=map.get(words[i]);
        		num++;
        		map.replace(words[i], num);        		
        	}else{
        		map.put(words[i],1);
        	}
        }        
        Iterator<String> iterator=map.keySet().iterator();
        while(iterator.hasNext()){
        	String key=iterator.next();
        	int num=map.get(key);
        	arrayList.add(new Entry(key, num));
        }
        Collections.sort(arrayList);
        ArrayList<String> arrayList1=new ArrayList<>();
        for(int i=0;i<k;i++){
        	arrayList1.add(arrayList.get(arrayList.size()-1-i).name);
        }
        return arrayList1;
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值