当前位置:网站首页>LeetCode_因式分解_简单_263.丑数
LeetCode_因式分解_简单_263.丑数
2022-07-25 14:44:00 【小城老街】
1.题目
丑数就是只包含质因数 2、3 和 5 的正整数。
给你一个整数 n ,请你判断 n 是否为丑数 。如果是,返回 true ;否则,返回 false 。
示例 1:
输入:n = 6
输出:true
解释:6 = 2 × 3
示例 2:
输入:n = 1
输出:true
解释:1 没有质因数,因此它的全部质因数是 {2, 3, 5} 的空集。习惯上将其视作第一个丑数。
示例 3:
输入:n = 14
输出:false
解释:14 不是丑数,因为它包含了另外一个质因数 7 。
提示:
-231 <= n <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/ugly-number
2.思路
(1)因式分解
如果 n 是丑数,那么一定存在非负整数 a、b、c,使得 n == 2a x 3b x 5c,并且当 a = b = c = 0 时,n = 1。所以为判断 n 是否满足上述形式,可以对 n 反复除以 2、3、5,直到 n 不再包含质因数 2、3、5 为止。如果剩下的数等于 1,则说明 n 不包含其他质因数,是丑数,返回 true;否则,说明 n 包含其他质因数,不是丑数,返回 false。
3.代码实现(Java)
//思路1————因式分解
class Solution {
public boolean isUgly(int n) {
if (n <= 0) {
return false;
}
int[] factors = {
2, 3, 5};
for (int i = 0; i < factors.length; i++) {
while (n % factors[i] == 0) {
n /= factors[i];
}
}
return n == 1;
}
}
边栏推荐
猜你喜欢

Melodic + Realsense D435i 配置及错误问题解决

English grammar_ Indefinite pronoun - other / other

Go language founder leaves Google

51 single chip microcomputer learning notes (2)

Under the epidemic, the biomedical industry may usher in breakthrough development

06. Neural network like

41 图片背景综合-五彩导航图

EDA chip design solution based on AMD epyc server

easygui使用的语法总结

基于PaddleOCR开发uni-app离线身份证识别插件
随机推荐
Jmeter的随机数函数怎么用
awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递
IP address classification, which determines whether a network segment is a subnet supernetwork
Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer
Examples of bio, NiO, AIO
转载----如何阅读代码?
Filters get the data in data; Filters use data in data
06. Neural network like
39 简洁版小米侧边栏练习
The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level
awk从入门到入土(21)awk脚本调试
Introduction to PHP
Realsense-Ros安装配置介绍与问题解决
云安全技术发展综述
Go语言创始人从Google离职
Browser based split screen reading
Gameframework making games (I)
Wechat official account official environment online deployment, third-party public platform access
Idea error failed to determine a suitable driver class
[cartographer_ros] VIII: Official demo parameter configuration and effect