leetcode 357

Fisrt kill (leetcode 357)


357. Count Numbers with Unique Digits

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.
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])
Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.

题目翻译及思路

本题是在题目给出的数字范围内找到每一位数字(即个十百千万位等等)都不相同的数。
我的思路是一个类似递归的过程把一个数分为1-9,10-99,100-999类似的分区,分别找到对应分区的符合条件的数字,题目给的n可以确定有多少个区间,再把所有区间的数字个数全部加起来即可。再找每个区间的数我采用排列组合的方法,有n位数,则第一位可以有9种方法,因为有10个数(0-9),所以第二位也有9种,第三位也有8位以此类推。最后可得符合标准的数。

C语言代码

int jiecheng(int n) {
    if (n== 0)
        return 1;
    int a= 9;
    int temp= 9;
    while (--n) {
        a*= temp;
        temp--;
    }
    return a;
}
int countNumbersWithUniqueDigits(int n) {
    int x= n;
    int tot= 0;
    while(x>= 0) {
        tot+= jiecheng(x);
        x--;
    }
    int sum= tot;
    return sum;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值