Skip to content

Latest commit

 

History

History
56 lines (50 loc) · 945 Bytes

788.旋转数字.md

File metadata and controls

56 lines (50 loc) · 945 Bytes

788.旋转数字

/*
 * @lc app=leetcode.cn id=788 lang=typescript
 *
 * [788] 旋转数字
 */

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

解法 1: 模拟

function rotatedDigits(n: number): number {
  const map = new Map([
    [0, 0],
    [1, 1],
    [8, 8],
    [2, 5],
    [5, 2],
    [6, 9],
    [9, 6],
  ])
  const check = (num: number) => {
    let flag = false
    while (num) {
      const a = num % 10
      if (!map.has(a)) return false
      else if (map.get(a) !== a) flag = true
      num = Math.floor(num / 10)
    }
    return flag
  }
  let res = 0
  for (let i = 1; i <= n; i++) {
    if (check(i)) res++
  }
  return res
}

Case

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