题目及示例
triplets中的数组一一与target中对比,如果有大于target的数,则不能加入运算;如果有等于target的数,则可以确定在target中的位置。
class Solution { public boolean mergeTriplets(int[][] triplets, int[] target) { boolean[] confirmed = new boolean[3]; for (int[] triplet : triplets) { boolean[] con = new boolean[3]; boolean canAdd = true; for (int i = 0; i < triplet.length; i++) { if (triplet[i] > target[i]) { canAdd = false; break; } else if (triplet[i] == target[i]) { con[i] = true; } } if (canAdd) { for (int i = 0; i < con.length; i++) { if (con[i]) { confirmed[i] = true; } } } } return confirmed[0] && confirmed[1] && confirmed[2]; } }