leetcode 357. Count Numbers with Unique Digits 统计独特编码数组的数量 + 一个很简单的排列组合问题
2017-10-08 10:06
791 查看
Given a non-negative integer n, count all numbers with unique digits, x, where 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])
这道题主要是考察所有的digit的唯一性,最简单的就是遍历,但是肯定会超时,仔细想一下,这是一个排列组合问题。
这道题让我们找一个范围内的各位上不相同的数字,比如123就是各位不相同的数字,而11,121,222就不是这样的数字。那么我们根据提示中的最后一条可以知道,一位数的满足要求的数字是10个(0到9),二位数的满足题意的是81个,[10 - 99]这90个数字中去掉[11,22,33,44,55,66,77,88,99]这9个数字,还剩81个。通项公式为f(k) = 9 * 9 * 8 * … (9 - k + 2),那么我们就可以根据n的大小,把[1, n]区间位数通过通项公式算出来累加起来即可,参见代码如下:
代码如下:
下面是C++的做法
其实这是一个排列组合问题
代码如下:
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])
这道题主要是考察所有的digit的唯一性,最简单的就是遍历,但是肯定会超时,仔细想一下,这是一个排列组合问题。
这道题让我们找一个范围内的各位上不相同的数字,比如123就是各位不相同的数字,而11,121,222就不是这样的数字。那么我们根据提示中的最后一条可以知道,一位数的满足要求的数字是10个(0到9),二位数的满足题意的是81个,[10 - 99]这90个数字中去掉[11,22,33,44,55,66,77,88,99]这9个数字,还剩81个。通项公式为f(k) = 9 * 9 * 8 * … (9 - k + 2),那么我们就可以根据n的大小,把[1, n]区间位数通过通项公式算出来累加起来即可,参见代码如下:
代码如下:
import java.util.HashSet; import java.util.Set; /* * 仔细想一下这是一个简单的排列组合问题 * */ class Solution { public int countNumbersWithUniqueDigits(int n) { if(n<=1) return (int)Math.pow(10, n); int count=10,val=9; for(int i=2;i<=n;i++) { val=val*(9+2-i); count=count+val; } return count; } /* * 直接查询肯定是最直接的想法,但是肯定会超时 * */ public int countNumbersWithUniqueDigitsByLoop(int n) { if(n<=1) return (int)Math.pow(10, n); int count=0; for(long i=0;i<Math.pow(10, n);i++) { char[] array=(""+i).toCharArray(); Set<Character> set=new HashSet<>(); for(int j=0;j<array.length;j++) set.add(array[j]); if(set.size()==(""+i).length()) count++; } return count; } }
下面是C++的做法
其实这是一个排列组合问题
代码如下:
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> using namespace std; class Solution { public: int countNumbersWithUniqueDigits(int n) { if (n == 0) return 1; int count = 10 , tmp = 9; for (int i = 2; i <= n; i++) { tmp *= (11 - i); count += tmp; } return count; } };
相关文章推荐
- 排列组合-打印出一个数组的元素的所有排列方式-算法
- 求一个长度为n的数组中长度为m的所有排列组合。
- Leetcode526. 回溯法之应用(一):统计数组排列数目
- LeetCode排列组合问题合集
- 面试题:一个整型数组中各元素排列组合得到的最大的一个数 ,比如,1,3,9,11,23,233,911 要得到:9,911,3,233,23,11,1
- JS把数组中相同元素组合成一个新的数组问题
- java数组排列组合问题汇总
- 给定一个数组,求出数组元素的排列和组合——Java实现
- 关于Java数组越界的一个诡异问题【leetcode204】
- 数组排列组合问题——BACKTRACKING
- 对面试题“输入n,求一个nXn矩阵,规定矩阵沿45度递增,形成一个zigzag数组(JPEG编码里取像素数据的排列顺序),请问如何用C++实现?”的理解
- 多个数组间元素排列组合问题求解(Java实现)
- C语言如何打印一个数组排列组合?
- 【leetcode 排列组合问题】Next Permutation | Permutations | Permutations II | Permutation Sequence
- Java 算法设计 多数组排列组合问题
- 求一个数组,n个元素的不同组合(排列组合问题)
- JS把数组中相同元素组合成一个新的数组问题
- 打印给定一个数组序列的所有的排列的类n皇后问题
- 给定一个数组,按序排列,从数组找出若干个数,使得这若干个数字的和与M最为接近,(背包问题)
- Java利用递归算法统计1-6的数组排列组合数