力扣2094题解
记录:
2025.5.12
题目:
思路:
暴力遍历。
解题步骤:
1.统计数字出现次数:使用数组cnt来记录输入数组中每个数字的出现次数。
2.生成三位偶数:通过循环从100开始,每次递增2,生成所有三位偶数。
3.检查有效性:对于每个生成的三位偶数,分解其每一位数字,并统计每个数字的使用次数。如果任何一个数字的使用次数超过原数组中的次数,则跳过该数;否则将其加入结果列表。
4.返回结果:将结果列表转换为数组并返回。
代码:
class Solution {public int[] findEvenNumbers(int[] digits) {int[] cnt = new int[10];for (int d : digits) {cnt[d]++;}List<Integer> ans = new ArrayList<>();next:for (int i = 100; i < 1000; i += 2) { int[] c = new int[10];for (int x = i; x > 0; x /= 10) { int d = x % 10;if (++c[d] > cnt[d]) { continue next; }}ans.add(i);}return ans.stream().mapToInt(i -> i).toArray();}
}
复杂度:
O(N∗Log10(N))
O(1)