# 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)

参考:https://leetcode.com/problems/numbers-with-repeated-digits/discuss/256866/Python-O(logN)-solution-with-clear-explanation

以下分析过程是我自己的思路,和上面的代码思路不太一样,具体的实现过程稍后补充,先把我的分析过程记录下来。

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

  1. [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 ...

随机推荐

  1. Spring mvc Json 的正确返回姿势

    我们经常都需要封装统一JSON格式 例如以下形式 { “data”:“输出的数据”, “code”:“响应代码”, “msg”:“响应信息” } /** * Created by linli on 2 ...

  2. Google 新实现的Protobuf RPC: grpc

    转自: http://www.dongliu.net/post/622450 Google 刚刚开源了grpc,  一个基于HTTP2 和 Protobuf 的RPC 实现. Protobuf 本身虽 ...

  3. mysql备份学习笔记及xtrabackup安装

    (参考书籍:<深入浅出MySQL>) 一.备份恢复策略 a)      确定要备份的表的存储引擎是事务型还是非事务型 b)      确定使用全备份还是增量备份 c)      定期做备份 ...

  4. hadoop fs、hadoop dfs与hdfs dfs的区别

    不多说,直接上干货! hadoop fs:    使用面最广,可以操作任何文件系统. hadoop dfs与hdfs dfs :   只能操作HDFS文件系统相关(包括与Local FS间的操作),前 ...

  5. P1616疯狂的采药

    传送 它不是可爱的01背包了!!!这个题中一种药可以采无限次!!! 它进化成了完全背包.完全背包中的内循环从m到v[i]改成了从v[i]到m 既然如此,代码如下: #include<iostre ...

  6. 忽略时间的小时分,展示的方法 data函数

    date(create_at) 列表: sql:

  7. 用sqoop将mysql的数据导入到hive表

    一.先将mysql一张表的数据用sqoop导入到hdfs 1.1.先在mysql中准备一张测试用的表 mysql> desc user_info; +-----------+---------- ...

  8. 学习笔记之TensorFlow

    TensorFlow https://www.tensorflow.org/ An open source machine learning framework for everyone Tensor ...

  9. [转][CentOS]修改IP后立即生效

    来自:http://bbs.51cto.com/thread-789908-1.html Linux系统里修改IP地址后该如何使之即刻生效,有两种方法可以解决: (1) sudo ifdown eth ...

  10. jQuery 事件的命名空间简单了解

    原文地址:http://www.jb51.net/article/43626.htm   用 jQuery 绑定和解绑事件监听器都是非常简单的,怎样精确地解绑其中一个监听器?我们需要了解一下事件的命名 ...