作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/ugly-number/

Total Accepted: 22335 Total Submissions: 67449 Difficulty: Easy

题目描述

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.

Example 1:

Input: 6
Output: true
Explanation: 6 = 2 × 3

Example 2:

Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2

Example 3:

Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7.

Note:

  1. 1 is typically treated as an ugly number.
  2. Input is within the 32-bit signed integer range: [−231, 231 − 1].

题目大意

如果一个数的因子只有2,3,5,那么这个数是丑数。判断一个数是不是丑数。

解题方法

除去2,3,5因子

把这个数中的所有的2,3,5的因子全部除去,剩余的数为1,则说明全部为这几个因子构成。

python解法。

class Solution(object):
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
if num <= 0: return False
while num % 2 == 0:
num /= 2
while num % 3 == 0:
num /= 3
while num % 5 == 0:
num /= 5
return num == 1

Java解法:

public static boolean isUgly(int num) {
if (num == 1) {
return true;
} while (num >= 2 && num % 2 == 0) {
num = num / 2;
}
while (num >= 3 && num % 3 == 0) {
num = num / 3;
}
while (num >= 5 && num % 5 == 0) {
num = num / 5;
}
return num == 1;
}

另外有精简的答案如下。

public class Solution {
public boolean isUgly(int num) {
int[] divides ={2,3,5};
for(int i = 0; i < divides.length && num > 0; i++){
while(num % divides[i] == 0){
num /= divides[i];
}
}
return num == 1;
}
}

日期

2015/10/16 21:20:23
2018 年 11 月 21 日 —— 又是一个美好的开始

【LeetCode】263. Ugly Number 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  2. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  5. Java [Leetcode 263]Ugly Number

    题目描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

  6. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  7. LN : leetcode 263 Ugly Number

    lc 263 Ugly Number lc 263 Ugly Number Write a program to check whether a given number is an ugly num ...

  8. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

随机推荐

  1. CSS浮动效果

    #div1{ background-color: yellow; width: 150px; height:150px; position: absolute; top:150px; left: 15 ...

  2. Python基础之基本运算符

    目录 1. 算数运算符 2. 比较运算符 3. 赋值运算符 4. 逻辑运算符 5. 身份运算 6. 运算符优先级 1. 算数运算符 常用算术运算符使用方法如下: x = 5 y = 2 a = x + ...

  3. EXCEL excel中运用ctrl+D、ctrl+enter、ctrl+E批量填充数据

    在excel中,利用鼠标拖动可以快速向下或者向右填充序列或者复制单元格.但是利用快捷键也可以实现多种填充方式,本文就为大家介绍一些ctrl系列快捷键的填充,一起来看看吧. 封面tu 一,ctrl+D/ ...

  4. 03 Windows安装Java环境

    Java环境安装 使用微信扫码关注微信公众号,并回复:"Java环境",免费获取下载链接! 1.卸载(电脑未装此程序,跳过此过程)    找到电脑上面的控制面板    找到这两个文 ...

  5. 【.Net】使用委托实现被引用的项目向上级项目的消息传递事件

    前言:在实际项目过程中,经常可能遇到被引用的项目要向上传递消息,但是又不能通过方法进行返回等操作,这个时候委托就派上用场了.以下使用委托,来实现被引用的项目向上传递消息的小教程,欢迎各位大佬提供建议. ...

  6. nodejs-npm模块管理器

    JavaScript 标准参考教程(alpha) 草稿二:Node.js npm模块管理器 GitHub TOP npm模块管理器 来自<JavaScript 标准参考教程(alpha)> ...

  7. Sharding-JDBC 实现水平分库分表

    1.需求分析

  8. 乱序拼图验证的识别并还原-puzzle-captcha

    一.前言 乱序拼图验证是一种较少见的验证码防御,市面上更多的是拖动滑块,被完美攻克的有不少,都在行为轨迹上下足了功夫,本文不讨论轨迹模拟范畴,就只针对拼图还原进行研究. 找一个市面比较普及的顶像乱序拼 ...

  9. zabbix之修改中文

    #在zabbix服务器安装中文名包 root@ubuntu:~# sudo apt-get install language-pack-zh* #:修改环境变量 root@ubuntu:~# sudo ...

  10. 【编程思想】【设计模式】【创建模式creational】Borg/Monostate

    Python版 https://github.com/faif/python-patterns/blob/master/creational/borg.py #!/usr/bin/env python ...