【leetcode】1012. Numbers With Repeated Digits
题目如下:
Given a positive integer
N
, return the number of positive integers less than or equal toN
that have at least 1 repeated digit.Example 1:
Input: 20
Output: 1
Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.Example 2:
Input: 100
Output: 10
Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.Example 3:
Input: 1000
Output: 262Note:
1 <= N <= 10^9
解题思路:题目要求出至少有一个重复元素的数字的总数,我们可以先求没有重复元素的数字的总数,再用N减去这个总数即可。怎么求出没有重复元素的数字的个数呢?假设Input = 53254,首先求出位数小于Input时满足条件的数字总数
位数为1:一共有9种 (1,2,3.... 9)
位数为2:最高位不能为0,只能在1~9种任选一个,第二位在1~9中只有8种选择,但是也可以为0,所以也是9选1,总数为9*9,
位数为3:一共有 9 * 9 * 8
位数为4:很明显可以看出规律了,总数为9 * A(4-1,9) (A为数学中排列)
位数等于Input时的情况会麻烦一些。我的方法是首先求以5XXXX这种格式的总数,然后再求出53XXX这种格式的总数,直到最后求出总数。
5XXXX: 第二位只能为0,1或者2,剩余的位数中,只能在除了第一位和第二位之外的8个数字中挑选,总数为 3 * A(3,8)
53XXX: 第三位只能为0或者1,剩余的位数中,只能在除了第一,二,三位之外的7个数字中挑选,总数为 2 * A(2,7)
532XX之后的情况类似
代码如下:
class Solution(object):
def numDupDigitsAtMostN(self, N):
"""
:type N: int
:rtype: int
"""
def calcPerm(v1,v2):
v = 1
while v1 > 0:
v *= v2
v2 -= 1
v1 -= 1
return v
ln = list(str(N))
res = 1 if len(list(str(N))) == len(set(list(str(N)))) else 0 #判断N是否包含重复元素
dic_used = [int(ln[0])]
for i in range(1,len(ln)):
count = 0
for j in range(0,int(ln[i])):
if j not in dic_used:
count += 1
res += count * calcPerm(len(ln) - i - 1, 10 - i - 1)
if int(ln[i]) in dic_used:
break
dic_used.append(int(ln[i])) #
for i in range(1,len(ln)+1):
if i != len(ln):
res += (9 * calcPerm(i-1,9))
else:
count = int(ln[0]) - 1
res += (count * calcPerm(i - 1, 9))
#
return N - res
【leetcode】1012. Numbers With Repeated Digits的更多相关文章
- 解题报告-1012. Numbers With Repeated Digits
Given a positive integer N, return the number of positive integers less than or equal to N that have ...
- 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【leetcode】967. Numbers With Same Consecutive Differences
题目如下: Return all non-negative integers of length N such that the absolute difference between every t ...
- 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- 【leetcode】1291. Sequential Digits
题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
随机推荐
- sublime text 修改侧边栏字体大小
ctrl+shift_p 安装PackageResourceViewer,通过**PackageResourceViewer **这个插件来实现. 打开这个插件,选择Open Resource 输入T ...
- SolidWorks学习笔记7 镜像,阵列
镜像 将特征,面,实体相对于一个平面来复制.修改原来的特征,镜像特征随之改变 阵列 线性阵列 , 在左侧,先激活要阵列的特征,然后点击小柱 然后选择方向1和方向2,该方向的阵列距离和数量(一般使用边线 ...
- 【python基础学习】---解析多层json,解析xml
1.以豆瓣的API接口为例子,解析返回的json数据 https://api.douban.com/v2/book/1220502 { "rating":{ "max&q ...
- PTA(Basic Level)1042.字符统计
请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包含 1 个英文字母, ...
- 使用Themleaf 模板引擎手动生成html文件
1.为什么要写这一篇呢? 在做一个邮件发送功能的时候,需要发送html邮件,javaMail 发送html 的时候需要有已经生成的html正文,所以需要提前将要发送的内容生成,所以就需要模板引擎来动态 ...
- python函数 全局变量和局部变量
li1=[1,2,3,4,5] str1='abc' def func1(): li1=[7,8,9] str1='efg' print(str1) func1() print(li1)#输出的结果为 ...
- ZooKeeper常用命令行操作
ZooKeeper常用命令行操作 通过./zkCli.sh 打开zk的客户端进入命令行后台 ls/ls2 列出当前节点下的子节点 ls2还会列出当前节点的状态 [zk: localhost:2181( ...
- CodeForces 820B + 821C
(点击题目即可查看原题) 820B Mister B and Angle in Polygon 题意:在一个正n边形中,每个顶点按顺序记为1~n,正n边形中任意三点顶点组成一个角,∠x1x2x3,问 ...
- MVC 部署到服务器
1.Nuget程序包管理 —>程序包管理控制台,运行以下命令即可:Update-Package Microsoft.AspNet.WebApi -reinstall 2.dll文件的缺少,覆盖
- VS Code 配置碰到的问题
VS Code 呈现缩进参考线以及语法高亮改变 找到 首选项——>设置→搜索renderIntentGuides→将此选项改为true(默认为false),就可以了.