当前位置:网站首页>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;
}
}
边栏推荐
- C language and SQL Server database technology
- 阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
- 关于ROS2安装connext RMW的进度条卡在13%问题的解决办法
- 27 选择器的分类
- QObject source code analysis -d pointer and Q pointer
- Realsense-Ros安装配置介绍与问题解决
- Flask SSTI injection learning
- IP地址分类,判断一个网段是子网超网
- Spark parameter adjustment and tuning
- The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level
猜你喜欢

GameFramework制作游戏(二)制作UI界面

【MySQL系列】-索引知多少

L1和L2正则化

Resource not found: rgbd_launch 解决方案

37 元素模式(行内元素,块元素,行内块元素)

Gameframework making games (I)

阿里云安装MYSQL5.7

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

Live classroom system 05 background management system

基于AMD EPYC服务器的EDA芯片设计解决方案
随机推荐
Qt connect 中, SIGNAL,SLOT 与 lambda 对比
Jmeter的随机数函数怎么用
06、类神经网络
转载----如何阅读代码?
【cartographer_ros】八: 官方Demo参数配置和效果
Sudo rosdep init error ROS installation problem solution
Awk from getting started to digging in (21) awk script debugging
Gateway 网关报错 SERVICE_UNAVAILABLE
The security market has entered a trillion era, and the security B2B online mall platform has been accurately connected to deepen the enterprise development path
Vs2017 large factory ERP management system source code factory general ERP source code
Introduction to PHP
SSH服务器拒绝了密码
Overview of cloud security technology development
H5页面input输入框弹起数字键盘,需要支持小数点
51 single chip microcomputer learning notes (1)
The concept and operation rules of calculus of variations
Educational Codeforces Round 132 (Rated for Div. 2) C,D+AC自动机
微信公众号正式环境上线部署,第三方公众平台接入
Spark parameter adjustment and tuning
awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递