Ugly Number leetcode java
问题描述:
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 }
Ugly Number leetcode java的更多相关文章
- Super Ugly Number -- LeetCode
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- LeetCode算法题-Ugly Number(Java实现-四种解法)
这是悦乐书的第199次更新,第208篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第64题(顺位题号是263).编写一个程序来检查给定的数字是否是一个丑陋的数字.丑陋的数 ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- 【LeetCode】263. Ugly Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
- Java [Leetcode 263]Ugly Number
题目描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
随机推荐
- MySQL 命令操作数据表
MySQL 命令操作数据表 1.查看表信息 desc hs_user_credit_info; 2.新增表字段 alter table hs_credit_order add search_relat ...
- 物体检测算法 SSD 的训练和测试
物体检测算法 SSD 的训练和测试 GitHub:https://github.com/stoneyang/caffe_ssd Paper: https://arxiv.org/abs/1512.02 ...
- IE7及以下浏览器不支持json的解决方法
在页面 alert(JSON);//大写 IE7及以下浏览器不支持json所以不会弹出object 解决方法打开json.org json的官网找到javascript的json2.js然后会转到gi ...
- Linux命令3——c
cal:calender,显示月历 -j:用凯撒历(dates of julius caesar)的形式来显示月历,不分月份.1-365/366 -m:显示月历时,把星期一定为一周的开始.默认星期日为 ...
- Vue学习二:v-model指令使用方法
本文为博主原创,未经允许不得转载: <!DOCTYPE html> <html lang="zh"> <head> <script src ...
- java中List,Set,Map用法以及区别
List,Set,Map是否继承自Collection接口? 答:List,Set是,Map不是. Collection是最基本的集合接口,一个Collection代表一组Object,即Collec ...
- Ubuntu18.04下搭建LAMP环境
一.Apache2 web 服务器的安装 : 可以先更新一下服务器 1.sudo apt-get update # 获取最新资源包 2.sudo apt-get upgrade ...
- c++ cmakelist 详解
基本元素 首先cmaklist必须包括以下几个部分: #工程名 project(study_case) #cmake最低版本需求 cmake_minimum_required(VERSION 2.8. ...
- 使用openpyxl实现excel文件的读取操作
1.环境准备 python3环境.安装openpyxl模块 2.excel文件数据准备 3.为方便直接调用,本代码直接封装成类 from openpyxl import load_workbook c ...
- Python 新建程序
1.建立一个新的文件夹 2.建一个包 3.建一个程序项目 4.默认模板配置