LeetCode —— 【1046. 最后一块石头的重量】

主要是使用Comparator 和 PriorityQueue

可以参考我的链接:(PriorityQueue的排序)https://blog.csdn.net/ChaoFeiLi/article/details/100137395

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

//Comparator<test> OrderIsdn =  new Comparator<test>()
class Solution {
    Comparator<Integer> comparator = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    };
    Queue<Integer> integerQueue = new PriorityQueue<Integer>(comparator);

    public int lastStoneWeight(int[] stones) {
        if (stones.length == 0)
            return 0;
        if (stones.length == 1)
            return stones[0];

        for (int stone : stones) {
            integerQueue.add(stone);
        }
        while (integerQueue.size() > 1) {
            int temp1 = integerQueue.poll();
            int temp2 = integerQueue.poll();
            int diff = Math.abs(temp1 - temp2);
            if (diff > 0)
                integerQueue.add(diff);
        }

        if (integerQueue.isEmpty())
            return 0;
        else return integerQueue.peek();
    }

    public static void main(String args[]) {

        int[] ints = new int[]{1, 2, 3, 4, 5, 6, 7};
        Solution solution = new Solution();
        System.out.println(solution.lastStoneWeight(ints));

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值