263. Ugly Number
程序员文章站
2022-04-24 15:34:34
...
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5
. For example,6, 8
are ugly while14
is not ugly since it includes another prime factor7
.Note that
1
is typically treated as an ugly number.Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
思路:
丑数,只包含因子2、3、5的数叫丑数
Java 代码(程序写得不好):
public class Solution {
public boolean isUgly(int num) {
int[] arr = {2, 3, 5};
while(num > 1) {
int i = 0;
for(; i < 3; i++) {
if(num % arr[i] == 0) {
num /= arr[i];
break;
}
}
if(i == 3) {
return false;
}
}
return num == 1;
}
}
上一篇: 264. Ugly Number II (python+cpp)
下一篇: 01 HTML快速入门
推荐阅读
-
SQL中Group分组获取Top N方法实现可首选row_number
-
JavaScript中的Number数字类型学习笔记
-
PDO版本问题 Invalid parameter number: no parameters were bound
-
利用ROW_NUMBER() OVER函数给SQL数据库中每一条记录分配行号的方法
-
JS大坑之19位数的Number型精度丢失问题详解
-
详解JS转换数值函数Number()、parseInt()、parseFloat()
-
详解JS转换数值函数Number()、parseInt()、parseFloat()
-
Oracle 分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法
-
sqlServer使用ROW_NUMBER时不排序的解决方法
-
Javascript之Number对象介绍