当前位置:网站首页>LeetCode_ Factorization_ Simple_ 263. Ugly number
LeetCode_ Factorization_ Simple_ 263. Ugly number
2022-07-25 14:50:00 【Old street of small town】
1. subject
Ugly numbers are just prime factors 2、3 and 5 The positive integer .
Give you an integer n , Please judge n Whether it's ugly or not . If it is , return true ; otherwise , return false .
Example 1:
Input :n = 6
Output :true
explain :6 = 2 × 3
Example 2:
Input :n = 1
Output :true
explain :1 No prime factor , So all its prime factors are {2, 3, 5} Empty set of . It is customary to regard it as the first ugly number .
Example 3:
Input :n = 14
Output :false
explain :14 It's not ugly , Because it contains another prime factor 7 .
Tips :
-231 <= n <= 231 - 1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/ugly-number
2. Ideas
(1) Factorization
If n It's ugly , Then there must be non negative integers a、b、c, bring n == 2a x 3b x 5c, And when the a = b = c = 0 when ,n = 1. So for judgment n Whether the above form is satisfied , It can be done to n Divide repeatedly by 2、3、5, until n No more prime factors 2、3、5 until . If the remaining number equals 1, shows n Excluding other prime factors , It's ugly , return true; otherwise , explain n Including other prime factors , It's not ugly , return false.
3. Code implementation (Java)
// Ideas 1———— Factorization
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;
}
}
边栏推荐
- H5页面input输入框弹起数字键盘,需要支持小数点
- 应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
- PS making and loading GIF pictures tutorial
- 43 盒子模型
- English语法_不定代词 - other / another
- The concept and operation rules of calculus of variations
- 基于浏览器的分屏阅读
- [MySQL must know and know] trigger | permission management
- [eloquence] negotiation persuasion skills and Strategies
- [C题目]力扣876. 链表的中间结点
猜你喜欢

Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount

I2C device driver hierarchy

Gameframework making games (I)

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

基于AMD EPYC服务器的EDA芯片设计解决方案

D2. Chopping Carrots (Hard Version) (每日一题)

L1 and L2 regularization

I2C设备驱动程序的层次结构

【MySQL系列】-索引知多少

Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer
随机推荐
About RDBMS and non RDBMS [database system]
QObject源码剖析-d指针和q指针
English语法_不定代词 - other / another
阿里云安装MYSQL5.7
English grammar_ Indefinite pronoun - other / other
H5页面input输入框弹起数字键盘,需要支持小数点
I2C device driver hierarchy
AS查看依赖关系和排除依赖关系的办法
Overview of cloud security technology development
QObject source code analysis -d pointer and Q pointer
Gameframework making games (I)
河源市区推出消防安全主题奶茶 助推夏季火灾防控
PHP 通过原生CURL实现非阻塞(并发)请求模式
苹果官网产品打折 买iPhone 13 Pro Max 可省600元
PHP implements non blocking (concurrent) request mode through native curl
基于AMD EPYC服务器的EDA芯片设计解决方案
Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
The concept and operation rules of calculus of variations
MySQL 45讲 | 06 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
二维数组赋初值你会几种方法?