首页 > 其他 > 详细

leetcode 357

时间:2016-08-03 10:19:31      阅读:156      评论:0      收藏:0      [点我收藏+]

求互不相同的数的个数(范围:0<=x<10^n)

易知当n=0是,就一个0

当n=10时0-9

其余情况:

首先最高位可以使1-9

接下来哪一位与最高位不同,但是多了一个0,也是9位

接下来与次高位和最高位不同位8位

。7

。6

。1

 1 class Solution {
 2 public:
 3     int countNumbersWithUniqueDigits(int n) {
 4       if(n==0)return 1;
 5       if(n==1)return 10;
 6       int k=9,num=n,nextk=9;
 7       n--;
 8       while(n--) {
 9         k*=nextk;
10         nextk--;
11       }
12       num--;
13         return k+countNumbersWithUniqueDigits(num);
14     }
15 };

 

leetcode 357

原文:http://www.cnblogs.com/thefirstfeeling/p/5731594.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!