Skip to content

Latest commit

 

History

History
41 lines (34 loc) · 658 Bytes

263.丑数.md

File metadata and controls

41 lines (34 loc) · 658 Bytes

263.丑数

/*
 * @lc app=leetcode.cn id=263 lang=typescript
 *
 * [263] 丑数
 */

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

解法 1: 整除 2,3,5

function isUgly(n: number): boolean {
  if (n <= 0) return false

  for (const num of [2, 3, 5]) {
    while (n % num === 0) {
      n = n / num
    }
  }
  return n === 1
}

Case

test.each([
  { input: { n: 6 }, output: true },
  { input: { n: 8 }, output: true },
  { input: { n: 14 }, output: false },
  { input: { n: 1 }, output: true },
])(`input: n = $input.n`, ({ input: { n }, output }) => {
  expect(isUgly(n)).toBe(output)
})