Ugly Number leetcode java
2024-09-03 04:03:57
问题描述:
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 while 14
is not ugly since it includes another prime factor 7
.
Note that 1
is typically treated as an ugly number.
分析:一个数,因式分解后,素数因子若只包含2/3/5,称为是ugly number;否则,不是ugly number。
给定一个数,判定它是不是ugly number。
算法:注意将 <=0的数过滤掉
/**
* 判断一个数是不是ugly number
* @param num
* @return
*/ //将num除去2,3,5这三个因子后,应该得到最终结果为1,若不为1,就不是ugly number
public static boolean isUgly(int num) {
if(num <= 0) //题目要求是整数,所以对于非整数一定要进行排除,否则会超时
return false;
while(num % 2 == 0)//如果存在2的质因子,则将所有2都找出来
num = num / 2;
while(num % 3 == 0)//接着找出所有的3
num = num / 3;
while(num % 5 == 0)//找出所有的5
num = num / 5;
return (num == 1) ;//若除了2,3,5之外,不存在其他质数因子,则为ugly number }
最新文章
- js 字符串转换成数字的三种方法
- struts2学习笔记之二:基本环境搭建
- Android中突发情况Activity数据的保存和恢复
- Linux C 程序 空语句-gcc编译命令(SIX)
- Android Butterknife框架配置
- rotatelogs分割apache日志文件
- Luogu3373【模板】线段树2
- 201521123028 《Java程序设计》第8周学习总结
- Shell 变量详解教程之位置变量与预定义变量。
- tomcat在centos下的操作
- 数据结构树之AVL树(平衡二叉树)
- python 中 __init__方法
- react-native “Unable to resolve module &#39;AccessibilityInfo&#39;” 的解决方案
- static成员函数不能调用non-static成员函数
- CodeForces - 1097D:Makoto and a Blackboard (积性)
- TcpListener、TcpClient
- 【ASP.NET】IHttpHandler和IHttpModule
- darknet53 yolo 下的识别训练
- 域名解析的DNS缓存如何清理
- 基于jetty镜像的ossfs镜像docker镜像构建
热门文章
- Python hasattr() 函数 // python中hasattr()、getattr()、setattr()函数的使用
- Sql 通过表名查找所有列名
- 题解——HDU 1848 Fibonacci again and again
- vue中find函数
- 【ASP.NET】System.Web.Routing - RouteCollection Class
- 【译】第40节---EF6-命令监听
- new和malloc的用法和区别
- 线程中为控件赋值Winform
- JAVA读取CSV文件到MySQL数据库中
- _attribute_character