【leetcode】357. Count Numbers with Unique Digits【M】【72】

Android中的Service:Binder,Messenger,AIDL(2) 前言前面一篇博文介绍了关于Service的一些基本知识,包括service是什么,怎么创建一个service,创建了一个service之后如何启动它等等。在这一篇博文里有一些需要前一篇铺垫的东西,建议没有看过前一篇博文的同学先去看一下前一篇: Android中的Service:默默的奉献者 (1) 。但是在前一篇博文中也有一些遗漏的东西——主要是关于bindService()这一块的具体细节。由于这 阅读详情

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

Hint:

  1. A direct way is to use the backtracking approach.
  2. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10n.
  3. This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
  4. Let f(k) = count of numbers with unique digits with length equals k.
  5. f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].

Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.


Subscribe to see which companies asked this question





class Solution(object):
    def countNumbersWithUniqueDigits(self, n):
        if n == 0:
            return 1
            
        res = [10,81]
        
        for i in xrange(3,min(10,n)+1):
            j = 3
            t = 81
            
            while j <= i:
                t *= (9-j+2)
                j += 1
            res += t,
        #print res
        return sum(res[:min(n,10)])  


一文速览:大模型语料构建与基础训练全流程实战指南 因为模型只有看到足够丰富、高质量的数据,才能真正学到“真本事”,展现出更强大的泛化能力,也就是能够在全新的场景和任务下,做出准确的推理和判断。我们日常做饭的时候,通常会先把买回来的菜仔细清洗干净,把坏掉的叶子、泥土、杂质等去除掉。同理,训练大模型的数据就像我们的食材,如果不干净,就会影响模型的学习效果,甚至训练出逻辑紊乱的模型。在大模型的世界里,有一句名言:“Garbage in, Garbage out”,翻译过来就是“垃圾输入,垃圾输出”,说白了,就是给模型喂的是什么数据,它学到的就是什么知识。 阅读详情

相关推荐

【Cadence】 ADS Dynamic Link使用教程

在Cadence617上使用ADS Dynamic Link

乾巽的博客 6080

357. Count Numbers with Unique Digits 完全不重复的数

Given anon-negativeinteger n, count all numbers with unique digits, x, where 0 ≤ x < . Example: Input: 2 Output: 91 Explanation: The answer should be the total numbers in the range of 0 ≤ x &...

LLL_foever的博客 325

BIM模型轻量化避坑指南:Revit导出GLTF常见问题及解决方案

本文深入探讨了BIM模型轻量化过程中,从Revit导出GLTF格式时遇到的常见问题与解决方案。针对文件体积膨胀、材质丢失、属性信息缺失等核心痛点,提供了从数据预处理、Draco几何压缩、纹理优化到Web端性能调优的全流程实战指南,帮助工程师高效实现BIM模型在WebGL环境中的流畅展示与交互。

wdx012345的博客 171

357. Count Numbers with Unique Digits(唯一数的个数——dp+math)

给定一个非负整数n,计算所有具有唯一数字x的数字,其中0≤x <10 n。 例: 输入:2 输出:91 说明:答案应该是0≤x <100范围内的总数, 不包括 11,22,33,44,55,66,77,88,99 根据dp来说,n为1:10个,为2 :9*9(因为0~9还有九个数字可以用);为3时,在为2的基础上还可以选择8个数字,往后乘的数依次递减,直到为1,那时候说明n的值已经大于了9,说明数的范围到了10000 0000 00 这个时候已经不可能再有唯一数了,因为总共0~9就十个数。 所以

AAAtang 506

别再死记硬背MAML原理了!用PyTorch手撸一个Omniglot小样本分类器(附完整代码)

本文详细介绍了如何使用PyTorch实现MAML(模型无关的元学习)算法,构建一个Omniglot小样本分类器。通过实战代码演示,帮助读者理解MAML的双层优化原理,并提供了完整的训练循环、网络架构设计及调试技巧,适合想要掌握元学习技术的开发者。

weixin_29191669的博客 131

Proxmox(pve)集群(4)存储(1)配置分布式存储ceph

它使用一种称为CRUSH(Controlled Replication Under Scalable Hashing)算法的数据分布算法来将数据均匀地分布在多个存储节点上,并确保数据的冗余和容错。它负责管理数据的分布、冗余和自动修复。PVE支持本地存储、LVM、NFS、iSCSI、CephFS、RBD、ZFS等存储方式,我这里使用两种存储方式,一种是通过ceph实现分布式存储,一种是通过truenas实现集中式存储。自我管理和自动恢复:Ceph具有自我管理的特性,可以动态地调整和优化存储集群的配置。

Monster✺◟(∗❛ัᴗ❛ั∗)◞✺的博客 2628

LeetCode:Count Numbers with Unique Digits

Count Numbers with Unique Digits Total Accepted:2092Total Submissions:4996Difficulty:Medium Given anon-negativeinteger n, count all numbers with unique digits, x, wher

walker lee 2234

深入剖析 Wenet 下 AISHELL-1 的 run.sh 脚本:从数据准备到模型训练

run.sh脚本用于处理 AISHELL-1 数据集的整个训练和测试流程。我们将逐步分析脚本中的每个部分,包括环境变量、参数设置、各个阶段的执行等。stage: 设置脚本的起始阶段(如数据准备)。stop_stage: 设置脚本的结束阶段。通过对 Wenet 下 AISHELL-1 的run.sh脚本的详细剖析,我们希望你能更好地理解每个步骤的功能及其背后的原理。此脚本不仅为模型训练提供了便利,也为用户自定义和扩展训练流程提供了基础。希望本教程能帮助你顺利完成语音识别项目!

帅小柏的博客 1634

LeetCode.357 Count Numbers with Unique Digits (统计数字不同的总数)

1.题目 Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. 2. 样例 Example: Input: 2 Output: 91 Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,4

侠客小虎的博客 268

Python办公自动化 2.1开发环境搭建:PyCharm社区版配置Anaconda开发环境

本篇文章主要讲述了如何在windows主机上配置 PyCharm + Anaconda开发环境、以及了如何配置 PyQt界面开发环境,安装过程超级详细,初学者可以照着一步一步来!

超级大洋葱的博客 2585

0 基础学前端:100 天拿 offer 实战课(第 16 天)—— JS 数组高级方法:数据处理效率翻倍,代码更简洁

方法名核心作用回调函数返回值返回结果适用场景map数据转换转换后的新元素新数组(长度与原数组一致)格式转换、新增字段、生成 DOM 字符串filter数据筛选true(保留)/false(过滤)新数组(符合条件的元素)筛选符合条件的元素(如未完成计划、今天的评论)reduce数据汇总新的累加值任意类型(数字、对象等)求和、计数、分组、多数据汇总find查找元素true(匹配)/false(不匹配)第一个匹配的元素 /undefined。

苏打水.X的博客 135

357. Count Numbers with Unique Digits 题解

357. Count Numbers with Unique Digits 题解 题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n. Example: Given n = 2, return 91. (The answer

yijingzhi的博客 426

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode 357. Count Numbers with Unique Digits(计算无重复数字)

原题网址:https://leetcode.com/problems/count-numbers-with-unique-digits/ Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n. Example: Given n = 2, return 91. (

James Pan 1542

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Leetcode-357- Count Numbers with Unique Digits C#

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x [11,22,33

悟 493

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode | 357. Count Numbers with Unique Digits 数论

Given a non-negative integer n, count all numbers withunique digits, x, where 0 ≤ x &lt; 10n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of0 ≤ x &lt; 100, excl...

u012737193的博客 201

leetcode 357. Count Numbers with Unique Digits

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excludin

Marshall的专栏 744

357. Count Numbers with Unique Digits

357. Count Numbers with Unique Digits 题目链接:https://leetcode.com/problems... 和安卓解锁那道题很想,那道题步数是m到n,https://segmentfault.com/a/11...这道题是1到n,表示数字的位数。backtracking,每次要记录走到的位数作为...

weixin_33918114的博客 102

leetcode 357

Count Numbers with Unique Digits 先来看看题目: 这道题从原理上来讲并不是很难,只有Medium难度,实则就是求所有的非负n位整数中,所有位上的数字均不相同的数有多少种。 粗暴方法 首先想到的是用排列组合的方式直接粗暴求解,分别考虑数字为1位数…n位数的情况,这样就会使用一层循环;其次就要考虑当为m位数时(1<=m<=n),各个位上可能存在的情况,第一...

噜啦啦~~的博客 299

leetcode-357. Count Numbers with Unique Digits

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x 题目的意思就是每一位上

caoyan_12727的博客 452

【Leetcode】357. Count Numbers with Unique Digits

题目描述: Given anon-negativeinteger n, count all numbers with unique digits, x, where 0 ≤ x < 10n. 解题分析: 题目要就就是找出0≤ x < 10n中各位数字都不相同的数的个数。要接触这道题只需要理解: 1.设f(n)表示n为数字中各位都不相同的个数,则有countNumb...

weixin_30695195的博客 164

leetcode_middle_13_357. Count Numbers with Unique Digits

题意: 给定一个整数,返回0到10的n次方中每个位都不等的数的个数。 分析: 即我们要找出存在相等位的数的个数。我们发现这样的数1位数有10个。2位数是有9*9个。按照排列组合的思想,3位数是9*9个,4位数是9*9*8个,直到10位数及以上是0,因为0-9九个数字必定有重复。 public class Solution { public int countNumbersWit

pusude的博客 269

LeetCode--Power of Three、Integer Break、Count Numbers with Unique Digits、Valid Perfect Square

326.Power of Three Given an integer, write a function to determine if it is a power of three. 给定一个整数,写一个函数来判断它是否是 3的幂次方。 class Solution(object): def isPowerOfThree(self, n): """ ...

qq_27318693的博客 215

【Leetcode】Count Numbers with Unique Digits

题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/题目: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.Example: Given n = 2, return 91. (Th

业精于勤,荒于嬉 1015

【LEETCODE】357- Count Numbers with Unique Digits [Python]

【LEETCODE】357- Count Numbers with Unique Digits [Python]

aliceyangxi1987的博客 1318

Count Numbers with Unique Digits

题目 Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x [11,2

gexlei的博客 274
上一篇: 【leetcode】155. Min Stack【E】【98】
下一篇: 【leetcode】368. Largest Divisible Subset【M】【65】
Vicky_Vicky_990121
博客等级 码龄14年 23粉丝 187原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值