Skip to content

Latest commit

 

History

History
42 lines (36 loc) · 773 Bytes

357.计算各个位数不同的数字个数.md

File metadata and controls

42 lines (36 loc) · 773 Bytes

357.计算各个位数不同的数字个数

/*
 * @lc app=leetcode.cn id=357 lang=typescript
 *
 * [357] 计算各个位数不同的数字个数
 */

// @lc code=start
function countNumbersWithUniqueDigits(n: number): number {}
// @lc code=end

解法 1: 排列组合

function countNumbersWithUniqueDigits(n: number): number {
  let sum = 0
  for (let i = 1; i <= n; i++) {
    let tmp = 1,
      base = 9
    for (let j = 0; j < i; j++) {
      tmp *= base
      if (j > 0) base--
    }
    sum += tmp
  }
  return sum + 1
}

Case

test.each([
  { input: { n: 2 }, output: 91 },
  { input: { n: 0 }, output: 1 },
])('input: n = $input.n', ({ input: { n }, output }) => {
  expect(countNumbersWithUniqueDigits(n)).toEqual(output)
})