LCP 06.拿硬币

​​题目来源:

        leetcode题目,网址:LCP 06. 拿硬币 - 力扣(LeetCode)

解题思路:

       对于每堆硬币,若其数量为偶数,每次拿两枚,否则先拿一枚,再每次拿两枚。

解题代码:

class Solution {
    public int minCount(int[] coins) {
        int res=0;
        for(int coin:coins){
            if(coin%2!=0){
                res+=1;
            }
            res+=coin/2;
        }
        return res;
    }
}

总结:

        官方题解也是一样的解法,贪心使拿两枚的次数最多。


你可能感兴趣的:(#,Java,LeetCode,Java)