Topic description
Ugly numbers are positive integers that contain only prime factors 2
, 3
and 5
.
Given an integer n
, please judge whether n
is an ugly number . If yes, return true
; otherwise, return false
.
Example 1:
输入:n = 6
输出:true
解释:6 = 2 × 3
Example 2:
输入:n = 1
输出:true
解释:1 没有质因数,因此它的全部质因数是 {2, 3, 5} 的空集。习惯上将其视作第一个丑数。
Example 3:
输入:n = 14
输出:false
解释:14 不是丑数,因为它包含了另外一个质因数 7 。
Likou original title address: https://leetcode.cn/problems/ugly-number
idea solution
The problem is not difficult, the core is how to decompose a number 分解
, such as: 6 is decomposed into 2 *
3, 12 is decomposed into 3 *
4. Continue to decompose according to 2 or 3 or 5 until the decomposition can't come out (not divisible by 2 3 5). The abstraction is: an operation is executed until a certain condition is met, and it will not stop.
So we will use 递归
, or while循环
recursive writing
Before writing the code, we can use the enumeration method to give a few more examples in order to better understand the idea. like:
/**
* 0不是丑数
* 1是丑数
* 2是丑数
* 3是丑数
* 4是丑数 = 2 * 2
* 5是丑数
* 6是丑数 = 2 * 3
* 7不是丑数,没法被 2 3 5整除
* 8是丑数 = 2 * 4
* 9是丑数 = 3 * 3
* 10是丑数 = 2 * 5 或 5 * 2
* 11不是丑数,没法被 2 3 5整除
* */
Therefore, in order to make the code more readable, we can write more if judgments, as follows:
var isUgly = function (n) {
if (n == 0) { // 0 不是丑数
return false
} else if (n == 1 | n == 2 | n == 3 | n == 4 | n == 5) { // 1 是第一个丑数
return true // 2 3 4 5 也都是丑数
} else { // 从6开始做递归
if (n % 2 == 0) { // 能被2整除,继续分解(递归)
n = n / 2
return isUgly(n)
} else if (n % 3 == 0) { // 能被3整除,继续分解(递归)
n = n / 3
return isUgly(n)
} else if (n % 5 == 0) { // 能被5整除,继续分解(递归)
n = n / 5
return isUgly(n)
} else {
// 若是没法被2 3 5分解整除的数,就一定不是丑数,比如7 11 13等...
return false
}
}
}
The recursive writing method submits the result graph
Indeed, the performance of recursion does not seem to be very good, but it can be used as our 保命
solution, after all, during the interview, often the mental state is not very good, the first is to be able to solve the algorithm problems, and then to optimize solution
while loop writing
The idea is similar, that is, put the decomposition of the number in the while loop, the following code
var isUgly = function (n) {
if (n === 0) { // 0 不是丑数
return false
} else if (n === 1 | n === 2 | n === 3 | n === 4 | n === 5) { // 1 是第一个丑数
return true // 2 3 4 5 也都是丑数
} else { // 从6开始做while循环分解
while (n % 2 === 0) {
n = n / 2
}
while (n % 3 === 0) {
n = n / 3
}
while (n % 5 === 0) {
n = n / 5
}
// 当一个大于等于6的数字被2 3 5不停的分解,最终的结果只有两个,要么是1,即分解到头了
// 要么不是1,说明没法分解了
// 如:10 / 2 == 5 5 / 5 == 1
// 如:17 被 2 3 5 没法分解,就不是1,故不是丑数
if (n === 1) {
return true
} else {
return false
}
}
}
The while loop writing method submits the result graph
Comparing the above two figures, it is found that the performance of recursion is indeed worse.
Summarize
In the future, when you need to decompose a larger number into small numbers, you can consider using 递归
, or use while循环
Although 递归
is occasionally used at work, while循环
is rarely used
There are ugly numbers in the numbers, and there are ugly ducklings in the duck group. In actual life, due to genes, blood, inheritance, and family, the ugly duckling cannot become a white swan, but the ugly duckling also has to work hard, at least become one. Not so bad ugly duckling ^_^
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。