Thanks! It's an excellent explanation. I derive again by myself, and code it as follows.

class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
int sum = 1, perm = 9, i = 1;
while (i <= n && i <= 10){
sum += perm;
perm *= (11-++i);
}
return sum;
}
};

Can someone explain this solution more in detail? I'm having trouble what the used boolean array is for and why we set it to true and then false. I'm also having trouble understanding what exactly search is doing. I've tried to hand simulate the algorithm, but it's very difficult for me to see what the algorithm is doing. I would greatly appreciate any help! @lzb700m