首页 > 其他 > 详细

357. Count Numbers with Unique Digits

时间:2016-07-08 13:26:19      阅读:126      评论:0      收藏:0      [点我收藏+]

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

给出一个非负整数n,计算出所有没有重复数字的x,其中0≤x<10n

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

设k为整数的位数,f(k)表示k位数中符合要求的数的个数:

f(1)=10;

f(2)=9*9;

f(3)=9*9*3;

……;

f(k)=9*9*...*(10-k+1);

ret=f(1)+f(2)+...+f(n);

 1 class Solution {
 2 public:
 3     int countNumbersWithUniqueDigits(int n) {
 4         int ret=1,s=9;
 5         for(int i=1;i<=n;i++){
 6             if(i==1){ret=ret+s;continue;}
 7             s=s*(10-i+1);
 8             ret=ret+s;
 9         }
10         return ret;
11     }
12 };

 

357. Count Numbers with Unique Digits

原文:http://www.cnblogs.com/Z-Sky/p/5652979.html

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