leet-code/src/main/java/leetcode/editor/cn/UglyNumber.java
2021-04-29 23:21:52 +08:00

78 lines
1.4 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个整数 n ,请你判断 n 是否为 丑数 。如果是,返回 true ;否则,返回 false 。
//
// 丑数 就是只包含质因数 2、3 和/或 5 的正整数。
//
//
//
// 示例 1
//
//
//输入n = 6
//输出true
//解释6 = 2 × 3
//
// 示例 2
//
//
//输入n = 8
//输出true
//解释8 = 2 × 2 × 2
//
//
// 示例 3
//
//
//输入n = 14
//输出false
//解释14 不是丑数因为它包含了另外一个质因数 7 。
//
//
// 示例 4
//
//
//输入n = 1
//输出true
//解释1 通常被视为丑数。
//
//
//
//
// 提示:
//
//
// -231 <= n <= 231 - 1
//
// Related Topics 数学
// 👍 221 👎 0
package leetcode.editor.cn;
//263:丑数
public class UglyNumber {
public static void main(String[] args) {
//测试代码
Solution solution = new UglyNumber().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isUgly(int n) {
if (n <= 0) {
return false;
}
while (n % 5 == 0) {
n = n / 5;
}
while (n % 3 == 0) {
n = n / 3;
}
while (n % 2 == 0) {
n = n / 2;
}
return n == 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}