leetcode1012
# given number n, see whether n has repeated number
def has_repeated(n):
str_n = str(n)
return len(set(str_n)) != len(str_n) def permutation(n, k):
prod = 1
for i in range(k):
prod *= (n-i)
return prod # calculate number of non-repeated n-digit numbers
# note: the n-digit number can't start with 0
# i.e: n_digit_no_repeat(2) calculates the non-repeated
# numbers in range [10, 99] (inclusive)
def n_digit_no_repeat(n):
if n == 1:
return 9
else:
return 9 * permutation(9, n-1) class Solution(object):
def numDupDigitsAtMostN(self, N):
"""
:type N: int
:rtype: int
"""
N_str = str(N)
n_digit = len(N_str)
digits = list(map(int, N_str))
result = N - 1
prefix = 0
for i in range(1, n_digit):
result -= n_digit_no_repeat(i)
for i in range(n_digit):
# when we fix the most significant digit, it
# can't be zero
start = 0 if i else 1
for j in range(start, digits[i]):
if has_repeated(prefix * 10 + j):
continue
if i != n_digit-1:
result -= permutation(10-i-1, n_digit-1-i)
else:
# optmized from `result -= has_repeated(prefix*10+j)`
result -= 1
prefix = prefix*10 + digits[i]
return result + has_repeated(N)
以下分析过程是我自己的思路,和上面的代码思路不太一样,具体的实现过程稍后补充,先把我的分析过程记录下来。
1位(0~9)有0个
2位(10~99)有9个:11,22,33,44,55,66,77,88,99
3位(100~999)有252个
百位以1~9开头,因为对于100~199的情况和200~299的情况都是一样的,因此外层用9相乘。
以100~199为例:
1.首先要找的个位和十位一样的:00,,22,33,44,55,66,77,88,99,共10个。
2.找个位和百位一样的:01,,21,31,41,51,61,71,81,91,共10-1=9个。
3.找十位和百位一样的:10,,12,13,14,15,16,17,18,19,共10-1=9个。
但是2步中出现了数字11,3步中也出现了数字11,因此每一轮是10 + (10-1) + (10-1) = 28个,
因此外层9*28=252。
4位(1000~9999):有4464个
百位和千位单独考虑,是10~99:有9组包含重数字:11**,22**,33**,44**,55**,66**,77**,88**,99**。
这9组数字,因为前缀已经是重数,因此后面的**无论是多少都满足要求,这样就得到了9*100=900个。
接下来考虑10**~19**这10组,排除11**组之后,还剩9组。
分析如下:以10**为例:
1.首先找个位和十位一样的:00,,22,33,44,55,66,77,88,99,共10个。
2.找个位和百位0一样的:,,20,30,40,50,60,70,80,90,共10-1=9个。
3.找个位和千位1一样的:,,21,31,41,51,61,71,81,91,共10-1=9个。
4.找十位和百位0一样的:,,02,03,04,05,06,07,08,09,共10-2=8个。
5.找十位和千位1一样的:,,12,13,14,15,16,17,18,19,共10-2=8个。
可以看到有重复计算的数字,因此第2步的00在第1步已经出现过,因此第2步是9个;
第3步有1个重复数字,剩下9个。第4步有2个重复数字,剩下8个,第5步有2个重复数字,剩下8个。
根据上面的分析可以知道,每一组有10 + (10-1) + (10-1) + (10-2) + (10-2)=44个。
因此从1000~1999这1000个数字中,有9*44=396个。再加上11**的100个,这样就是496个。
那么同样的计算方式,可以知道2000~2999也是496,从1000~9999一共就是9*496=4464个。
1位:9*0=0
2位:9*1=9
3位:9*(10+(10-1)*1+(10-1)*1)=252
4位:9*(9*(10+(10-1)*2+(10-2)*2)+1*100)=4464
5位:9*(9*(8*(10+(10-1)*3+(10-3)*3)+2*100)+1*1000)=62784
6位:9*(9*(8*(7*(10+(10-1)*4+(10-4)*4)+3*100)+2*1000)+1*10000)=763920
7位:9*(9*(8*(7*(6*(10+(10-1)*5+(10-5)*5)+4*100)+3*1000)+2*10000)+1*100000)=8455680
8位:9*(9*(8*(7*(6*(5*(10+(10-1)*6+(10-6)*6)+5*100)+4*1000)+3*10000)+2*100000)+1*1000000)=88367040
9位:9*(9*(8*(7*(6*(5*(4*(10+(10-1)*7+(10-7)*7)+6*100)+5*1000)+4*10000)+3*100000)+2*1000000)+1*10000000)=896734080
10位:9*(9*(8*(7*(6*(5*(4*(3*(10+(10-1)*8+(10-8)*8)+7*100)+6*1000)+5*10000)+4*100000)+3*1000000)+2*10000000 )+1*100000000)=8996734080
10位以上,最内部的计算式子10+(10-1)*9+(10-9)*9=100,也就表示任意的数字都包含重复。
经过以上的分析,可以得到如下的递推公式:x表示数字的位数,例如x=3表示100~999之间的满足要求的数字个数(不含0~99之间的)
k=[9,8,7,6,5,4,3]
for x in range(3,11):
c = x - 4
he = (10+(10-1)*(x-2)+(10-(x-2))*(x-2))
p = 2
while c >= 0:
he = k[c] * he + (c+1) * pow(10,p)
c -= 1
p += 1
y = he * 9
print(y)
'''
252
4464
62784
763920
8455680
88367040
896734080
8996734080
'''
我尝试用这个思路来做一个完整的解决方案,如果能完成,我会代码补充上。如果做不出来,就算了吧,哈哈。
leetcode上面的hard题目,如果自己有思路就尝试着写一下,如果没有思路就学习别人的。
算法的能力的提升,是一个循序渐进的过程,急不得。
leetcode1012的更多相关文章
- [Swift]LeetCode1012. 至少有 1 位重复的数字 | Numbers With 1 Repeated Digit
Given a positive integer N, return the number of positive integers less than or equal to N that have ...
随机推荐
- ALGO-145_蓝桥杯_算法训练_4-1打印下述图形
记: 这里用到了printf("%*s%s%*s\n",n-i,"",arr,n-i,"");的写法, 其中%*s中的*代表该字符串s的个数 ...
- C++进阶--派生类的析构(虚析构函数,shared_ptr)
//############################################################################ /* 在多态虚基类中声明一个虚析构函数 * ...
- 【Https】Spring RestTemplete支持Https安全请求
实现步骤 Step1: 自定义ClientHttpRequestFactory package com.example.demo.https; import org.springframework.h ...
- Android开发之adb,$Sqlite篇
一. 操作系统: 1. linux操作系统: linux操作系统其实就是Linux内核,Linux内核[kernel]是整个操作系统的最底层,它负责整个硬件的驱动,以及提供各种系统所需的核心功能,包括 ...
- 客户端负载均衡Feign之二:Feign 功能介绍
一.Ribboon配置 在Spring cloud Feign中客户端负载均衡是通过Spring cloud Ribbon实现的,所以我们可以直接通过配置Ribbon客户端的方式来自定义各个服务客户端 ...
- 笔记函数 - Ring0 Sleep()
#define DELAY_ONE_MICROSECOND (-10) #define DELAY_ONE_MILLISEND (DELAY_ONE_MICROSECOND*1000) void Sl ...
- 几种常见NPE
NPE(Null Point Exception的简称) 1.Map下的NPE 直接上代码: public class User { private Integer id; private Strin ...
- 数据分箱:等频分箱,等距分箱,卡方分箱,计算WOE、IV
转载:https://zhuanlan.zhihu.com/p/38440477 转载:https://blog.csdn.net/starzhou/article/details/78930490 ...
- SAS 宏数据运算
74 /*计算两个日期之间间隔多少天:开始时间01DEC2005 结束日期:31JUL2018*/75 %LET N2='31JUL2018'D-'01DEC2005'D+1;76 %PUT & ...
- windows下安装、卸载mysql服务
将下载下来的mysql解压到指定目录下(如:d:\mysql)安装服务在命令行输入d:\mysql\bin\mysqld -installnet start mysql卸载服务在命令行输入net st ...