Leetcode_1046_最后一块石头的重量_水题

12/30

最近要期末复习并且还要写数据结构的大作业,实在是没有什么时间写题,前两天的题都是hard,所以就鸽了,有空一定补。
用优先队列模拟一下就好了
等考完试一定把正则学了
((a, b) -> b - a)太优雅了QAQ
class Solution {
    
    
    public int lastStoneWeight(int[] stones) {
    
    
        Comparator<Integer>cmp = new Comparator<Integer>() {
    
    
            @Override
            public int compare(Integer o1, Integer o2) {
    
    
                return o2-o1;
            }
        };
        PriorityQueue<Integer>pq = new PriorityQueue<Integer>(cmp);
        for(int i:stones){
    
    
            pq.offer(i);
        }
        while(pq.size()>1){
    
    
            int p=pq.poll();
            int q=pq.poll();
            if(p!=q){
    
    
                pq.offer(Math.abs(p-q));
            }
        }
        return pq.size()==0?0:pq.poll();
    }
}

猜你喜欢

转载自blog.csdn.net/HDUCheater/article/details/111982345