[leetCode刷题记录-2094] 找出 3 位偶数

2094. 找出 3 位偶数

给你一个整数数组 digits ,其中每个元素是一个数字(0 - 9)。数组中可能存在重复元素。

你需要找出 所有 满足下述条件且 互不相同 的整数:

  • 该整数由 digits 中的三个元素按 任意 顺序 依次连接 组成。
  • 该整数不含 前导零
  • 该整数是一个 偶数

例如,给定的 digits 是 [1, 2, 3] ,整数 132 和 312 满足上面列出的全部条件。

将找出的所有互不相同的整数按 递增顺序 排列,并以数组形式返回

示例 1:

输入:digits = [2,1,3,0]
输出:[102,120,130,132,210,230,302,310,312,320]
解释:
所有满足题目条件的整数都在输出数组中列出。 
注意,答案数组中不含有 奇数 或带 前导零 的整数。

示例 2:

输入:digits = [2,2,8,8,2]
输出:[222,228,282,288,822,828,882]
解释:
同样的数字(0 - 9)在构造整数时可以重复多次,重复次数最多与其在 digits 中出现的次数一样。 
在这个例子中,数字 8 在构造 288、828 和 882 时都重复了两次。 

示例 3:

输入:digits = [3,7,5]
输出:[]
解释:
使用给定的 digits 无法构造偶数。

提示:

  • 3 <= digits.length <= 100
  • 0 <= digits[i] <= 9

第一次解题:暴力枚举,性能为O(n^3)

class Solution {
    /**
        暴力枚举
        复杂度O(N^3)
    */
    public int[] findEvenNumbers(int[] digits) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < digits.length; i++) {
            int[] indexs = new int[2];
            int first, second, third;
            if (digits[i] % 2 == 0) {
                third = digits[i];
                indexs[0] = i;
                for (int j = 0; j < digits.length; j++) {
                    if (indexs[0] == j) {
                        continue;
                    }
                    second = digits[j];
                    indexs[1] = j;
                    for (int k = 0; k < digits.length; k++) {
                        if (digits[k] == 0 || indexs[0] == k || indexs[1] == k) {
                            continue;
                        }
                        first = digits[k];
                        result.add(Integer.valueOf("" + first + second + third));
                    }
                }
            }
        }
        return result.stream().distinct().sorted().mapToInt(Integer::intValue).toArray();
    }
}

The range of possible answers includes all even numbers between 100 and 999 inclusive. Could you check each possible answer to see if it could be formed from the digits in the array?

看leetCode的官方题解,可以反向思考,从【100,999】遍历,保留下digits能够支持组装的数字。

class Solution {
    public int[] findEvenNumbers(int[] digits) {
        List<Integer> res = new ArrayList<>();
        Map<Integer, Integer> freq = new HashMap<>();
        // 统计数字数组中各数字的出现次数
        for (int d : digits) {
            freq.put(d, freq.getOrDefault(d, 0) + 1);
        }
        // 枚举所有三位偶数
        for (int i = 100; i < 1000; i += 2) {
            Map<Integer, Integer> freq1 = new HashMap<>();
            int num = i;
            // 统计当前偶数的每一位数字的出现次数
            while (num > 0) {
                int d = num % 10;
                freq1.put(d, freq1.getOrDefault(d, 0) + 1);
                num /= 10;
            }
            // 检查是否满足条件
            boolean isValid = true;
            for (Map.Entry<Integer, Integer> entry : freq1.entrySet()) {
                if (freq.getOrDefault(entry.getKey(), 0) < entry.getValue()) {
                    isValid = false;
                    break;
                }
            }
            if (isValid) {
                res.add(i);
            }
        }
        // 转换为数组
        int[] result = new int[res.size()];
        for (int j = 0; j < res.size(); j++) {
            result[j] = res.get(j);
        }
        return result;
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/finding-3-digit-even-numbers/solutions/1140756/zhao-chu-3-wei-ou-shu-by-leetcode-soluti-hptf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

复杂度分析

时间复杂度:O(k⋅10 ^k),其中 k 为目标偶数的位数。即为枚举所有给定位数偶数的时间复杂度。

空间复杂度:O(1),输出数组不计入空间复杂度。

补充一个评论区看到的题解,这个更简洁,而且时间复杂度只有O(n)。

对比官方题解的hashMap存储数字出现的次数,这个解法采用数组来存储,因为题目设定digits里只会出现0~9的数字,所以可以下标记录digits里出现的数字,用数组的值来存储出现的次数,非常巧妙

class Solution {
    public int[] findEvenNumbers(int[] digits) {
        int[] arr = new int[10];
        for (int digit : digits) {
            arr[digit]++;
        }

        List<Integer> list = new ArrayList<>();
        for (int i = 100; i < 1000; i++) {
            if ((i & 1) != 0){
                continue;
            }
            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100;
            arr[a]--;
            arr[b]--;
            arr[c]--;

            if (arr[a] >= 0 && arr[b] >= 0 && arr[c] >= 0){
                list.add(i);
            }

            arr[a]++;
            arr[b]++;
            arr[c]++;
        }

        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

### PyCharm 打开文件显示全的解决方案 当遇到PyCharm打开文件显示全的情况时,可以尝试以下几种方法来解决问题。 #### 方法一:清理缓存并重启IDE 有时IDE内部缓存可能导致文件加载异常。通过清除缓存再启动程序能够有效改善此状况。具体操作路径为`File -> Invalidate Caches / Restart...`,之后按照提示完成相应动作即可[^1]。 #### 方法二:调整编辑器字体设置 如果是因为字体原因造成的内容显示问题,则可以通过修改编辑区内的文字样式来进行修复。进入`Settings/Preferences | Editor | Font`选项卡内更改合适的字号大小以及启用抗锯齿功能等参数配置[^2]。 #### 方法三:检查项目结构配置 对于某些特定场景下的源码视图缺失现象,可能是由于当前工作空间未能正确识别全部模块所引起。此时应该核查Project Structure的Content Roots设定项是否涵盖了整个工程根目录;必要时可手动添加遗漏部分,并保存变更生效[^3]。 ```python # 示例代码用于展示如何获取当前项目的根路径,在实际应用中可根据需求调用该函数辅助排查问题 import os def get_project_root(): current_file = os.path.abspath(__file__) project_dir = os.path.dirname(current_file) while not os.path.exists(os.path.join(project_dir, '.idea')): parent_dir = os.path.dirname(project_dir) if parent_dir == project_dir: break project_dir = parent_dir return project_dir print(f"Current Project Root Directory is {get_project_root()}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sdfssdfa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值