leetcode 822. Card Flipping Game | 822. 翻转卡片游戏(Java)

x33g5p2x  于2022-02-07 转载在 Java  
字(0.5k)|赞(0)|评价(0)|浏览(240)

题目

https://leetcode.com/problems/card-flipping-game/

题解

这个题描述挺复杂,总结起来就一句话:

正反面相同的肯定不行,所以找除了正反面相同的以外,其余的数字中最小的那个。

class Solution {
    public int flipgame(int[] fronts, int[] backs) {
        Set<Integer> invalid = new HashSet<>();
        for (int i = 0; i < fronts.length; i++) {
            if (fronts[i] == backs[i]) invalid.add(fronts[i]);
        }
        int result = Integer.MAX_VALUE;
        for (int i = 0; i < fronts.length; i++) {
            if (!invalid.contains(fronts[i])) result = Math.min(result, fronts[i]);
            if (!invalid.contains(backs[i])) result = Math.min(result, backs[i]);
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }
}

相关文章