数据结构

1. Two Sum

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])){
                res[0]=map.get(target-nums[i]);
                res[1]=i;
                return res;
            }else{
                map.put(nums[i],i);
            }
        }
        return res;
    }
}

187. Repeated DNA Sequences

class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        Map<String,Integer> map = new HashMap<>();
        List<String> res = new ArrayList<>();
        for(int i=0;i+10<=s.length();i++){
            String str = s.substring(i,i+10);
            map.put(str,map.containsKey(str)?map.get(str)+1:1);
            if(map.get(str)==2) res.add(str);
        }
        return res;
    }
}

560. Subarray Sum Equals K

前缀和+hash

class Solution {
    public int subarraySum(int[] nums, int k) {
        int n = nums.length; 
        int res = 0;
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,1);
        for(int i=0,sum=0;i<n;i++){
            sum+=nums[i];
            if(map.containsKey(sum-k))  res+=map.get(sum-k);
            map.put(sum,map.containsKey(sum)?map.get(sum)+1:1);
        }      
        return res;        
    }
}

547. Friend Circles

并查集

class Solution {
    int[] p;
    public int find(int x){
        if(p[x]!=x) p[x]=find(p[x]);
        return p[x];
    }
    public int findCircleNum(int[][] M) {       
        int n = M.length;
        int res = n;
        p = new int[n];
        for(int i=0;i<n;i++){
            p[i]=i;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<i;j++){
                if(M[i][j]==0)  continue;
                if(find(i)!=find(j)){
                    p[find(i)]=find(j);
                    res--;
                }
            }
        }
        return res;
    }
}

684. Redundant Connection

class Solution {
    int[] p;
    public int find(int x){
        if(p[x]!=x)  p[x]=find(p[x]);
        return p[x];
    }
    public int[] findRedundantConnection(int[][] edges) {
        int n = edges.length;
        p = new int[n+1];
        for(int i=0;i<=n;i++){
            p[i]=i;
        }
        for(int i=0;i<n;i++){
            if(find(edges[i][0])==find(edges[i][1])){
                return new int[]{edges[i][0],edges[i][1]};
            }else{
                p[find(edges[i][0])]=find(edges[i][1]); 
            }
        }
        return new int[]{-1,-1};        
    }
}

692. Top K Frequent Words

注意:第一顺位是数量,第二顺位是字典序的实现方式。

class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        List<String> res = new ArrayList<>();
        Map<String,Integer> map = new HashMap<>();
        for(int i=0;i<words.length;i++){
            if(map.containsKey(words[i])){
                map.put(words[i],map.get(words[i])+1);
            }else{
                map.put(words[i],1);
            }
        }
        PriorityQueue<String> pq =new PriorityQueue<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(map.get(o1)==map.get(o2)){
                    return o2.compareTo(o1);//字典序逆序
                } else{
                    return map.get(o1)-map.get(o2);//小根堆,最小元素最先poll
                }
            }});

        for(String str : map.keySet()){
            pq.add(str);
            if(pq.size()>k)  pq.poll();
        }
        while(!pq.isEmpty()){//必须用poll的弹出顺序
            res.add(pq.poll());//字典序逆序,前K个元素 从小到大 排列
        }
        Collections.reverse(res);//反转后 字典序顺序且从大到小排列
        return res;
    }
}

295. Find Median from Data Stream

class MedianFinder {
    PriorityQueue<Integer> up = new PriorityQueue<>();//最小堆
    PriorityQueue<Integer> down = new PriorityQueue<>(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    });//最大堆
    
    /** initialize your data structure here. */
    public MedianFinder() {
        
    }
    
    public void addNum(int num) {
        if(down.isEmpty() || num >= down.peek()){
            up.add(num);
        }else{
            down.add(num);
            up.add(down.poll());
        }
        if(up.size()>down.size()+1){
            down.add(up.poll());
        }
    }
    
    public double findMedian() {
        if((up.size()+down.size())%2==1)    return up.peek();
        else return (up.peek()+down.peek())/2.0;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值