Leetcode 3483. Unique 3-Digit Even Numbers

  • [Leetcode 3483. Unique 3-Digit Even Numbers](#Leetcode 3483. Unique 3-Digit Even Numbers)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)

1. 解题思路

这一题其实是一个easy的题目,因为限制条件有限,最暴力的方法就是直接遍历一下100到999的全部数字,看看那些数字可以被构成即可。

我们这里稍微做的复杂了一点,用了迭代的思路,分别考察每一位上的可取数字,看起来更好看了一些而已......

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def totalNumbers(self, digits: List[int]) -> int:
        cnt = Counter(digits)
        
        def dp(idx):
            if idx == 2:
                return len([i for i in range(0, 10, 2) if cnt[i] != 0])
            ans = 0
            st = 0 if idx == 1 else 1
            for i in range(st, 10):
                if cnt[i] > 0:
                    cnt[i] -= 1
                    ans += dp(idx+1)
                    cnt[i] += 1
            return ans
        
        return dp(0)

提交代码评测得到:耗时19ms,占用内存18.2MB。

相关推荐
Espresso Macchiato11 天前
Leetcode 3782. Last Remaining Integer After Alternating Deletion Operations
迭代·leetcode hard·leetcode双周赛172·leetcode 3782
Tisfy11 天前
LeetCode 1351.统计有序矩阵中的负数:O(m+n)时间复杂度——抽象题解
算法·leetcode·矩阵·题解·遍历
Tisfy13 天前
LeetCode 2483.商店的最少代价:两次遍历 -> 一次遍历
算法·leetcode·题解·遍历
Tisfy18 天前
LeetCode 955.删列造序 II:模拟(O(mn)) + 提前退出
算法·leetcode·字符串·题解·遍历
DanyHope20 天前
LeetCode 206. 反转链表:迭代 + 递归双解法全解析
算法·leetcode·链表·递归·迭代
2401_841495641 个月前
【LeetCode刷题】合并区间
数据结构·python·算法·leetcode·合并·遍历·排序
winfredzhang2 个月前
用 Python + wxPython 打造一个万能网页图片一键下载神器(附完整源码 + 详细解析)
python·微信·pdf·遍历·图片·网页·保存
Q741_1472 个月前
C++ 面试高频考点 链表 迭代 递归 力扣 25. K 个一组翻转链表 每日一题 题解
c++·算法·链表·面试·递归·迭代
闪电麦坤955 个月前
数据结构:用链式队列实现层序遍历 (Level-order Traversal)
数据结构··遍历
Tisfy5 个月前
LeetCode 2411.按位或最大的最小子数组长度:一次倒序遍历
数据结构·算法·leetcode·题解·位运算·遍历