题目如下:

Given a positive integer N, return the number of positive integers less than or equal to N 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: 262

Note:

  1. 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的更多相关文章

  1. 解题报告-1012. Numbers With Repeated Digits

    Given a positive integer N, return the number of positive integers less than or equal to N that have ...

  2. 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  3. 【leetcode】967. Numbers With Same Consecutive Differences

    题目如下: Return all non-negative integers of length N such that the absolute difference between every t ...

  4. 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【leetcode】1012. Complement of Base 10 Integer

    题目如下: Every non-negative integer N has a binary representation.  For example, 5 can be represented a ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  8. 【leetcode】1291. Sequential Digits

    题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...

  9. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

随机推荐

  1. K8s之Projected Volume

    四种:Secret .ConfigMap.Downward API.ServiceAccountToken 1.Secret Secret:帮你把Pod想要访问的加密数据,存放到Etcd中,然后,通过 ...

  2. PTA(Basic Level)1027.打印沙漏

    本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓"沙漏形状",是指每行 ...

  3. [转帖]glib gslibc libc 的关系与区别

    https://blog.csdn.net/Com_ma/article/details/78692092 [glibc 和 libc] glibc 和 libc 都是 Linux 下的 C 函数库. ...

  4. 统计sql server 2012表的行数

    --功能:统计sql server 2012表的行数 SELECT a.name, a.object_id, b.rows, b.index_id FROM sys.tables AS a INNER ...

  5. nginx反向代理_负载均衡

    注意ip地址为: 虚拟机ip设置 TYPE="Ethernet"BOOTPROTO="static"NAME="enp0s3"DEVICE= ...

  6. P2670 【扫雷游戏】

    题面哦~~ lalala~~~ 这题数据并不大,最大不过100*100,所以果断穷举 其实本来我是想边读边做的,但读入是个问题. 主要思路呢,就是读完之后穷举一边,只要是炸弹,就把周围一圈8个加一遍 ...

  7. 微信小程序,预览在开发工具上显示正常,手机预览二维码报request->fail错误,打开手机的调试功能又正常。

    这里错误很明显是属于网址错误,开发工具和手机调试都能走request->success: 唯独常规模式下无法显示. 最开始调试过很多方法,没找出原因.最后到小程序开发设置才发现,自己未配置服务器 ...

  8. python_线程读写操作<一>

    线程读写操作 import threading,random,queue q = queue.Queue() alist=[] def shengchan(): for i in range(10): ...

  9. JMS消息通信服务

    什么是Java消息服务 Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持JAVA应用程序开发.在J2EE中 ...

  10. 2种方法实现java对象的深拷贝

    2种方法实现java对象的深拷贝 2017年12月03日 22:23:07 iCoding91 阅读数 4420更多 分类专栏: java   版权声明:本文为博主原创文章,遵循CC 4.0 BY-S ...