码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 357

时间:2016-08-03 10:22:06      阅读:148      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!