当前位置:网站首页>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;
}
}
边栏推荐
- Awk from getting started to digging in (21) awk script debugging
- Syntax summary of easygui
- [eloquence] negotiation persuasion skills and Strategies
- The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
- easygui使用的语法总结
- (original) customize a scrolling recyclerview
- I2C设备驱动程序的层次结构
- C language and SQL Server database technology
- Dpkg package download addresses of various platforms (including arm64)
- I2C device driver hierarchy
猜你喜欢

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

Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer

English语法_不定代词 - other / another

The main function of component procurement system, digital procurement helps component enterprises develop rapidly

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

44 Sina navigation, Xiaomi sidebar exercise

06、类神经网络

Filters get the data in data; Filters use data in data

微信公众号正式环境上线部署,第三方公众平台接入

51单片机学习笔记(1)
随机推荐
The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level
51单片机学习笔记(2)
(原创)自定义一个滚屏的RecyclerView
快速搭建Dobbo小Demo
45padding won't open the box
About RDBMS and non RDBMS [database system]
Is it safe for Guolian securities to buy shares and open an account?
Idea error failed to determine a suitable driver class
English语法_不定代词 - other / another
L1和L2正则化
[Nuxt 3] (十一) 传送 & 模块
软件测试 -- 1 软件测试知识大纲梳理
QObject源码剖析-d指针和q指针
Paddlenlp's UIE relationship extraction model [executive relationship extraction as an example]
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
37 element mode (inline element, block element, inline block element)
没错,请求DNS服务器还可以使用UDP协议
filters获取data中的数据;filters使用data中的数据
Gameframework making games (II) making UI interface
English grammar_ Indefinite pronoun - other / other