Ugly Number

Write a program to check whether a given number is an ugly number`.

Ugly numbers are positive numbers whose prime factors only include 235. For example, 68 are ugly while 14 is not ugly since it includes another prime factor 7.

注意事项

Note that 1 is typically treated as an ugly number.

样例

Given num = 8 return true
Given num = 14 return false

解题

直接递归

public class Solution {
/**
* @param num an integer
* @return true if num is an ugly number or false
*/
public boolean isUgly(int num) {
// Write your code here
if(num <=0)
return false;
if(num == 1)
return true;
if(num %2 ==0)
return isUgly(num/2);
if(num %3 ==0)
return isUgly(num/3);
if(num%5 ==0)
return isUgly(num/5);
return false; }
}

while 循环‘

public class Solution {
/**
* @param num an integer
* @return true if num is an ugly number or false
*/
public boolean isUgly(int num) {
// Write your code here
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; }
}

lintcode:Ugly Number I的更多相关文章

  1. [LintCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number`. Ugly numbers are positive number ...

  2. [LintCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  3. Lintcode: Kth Prime Number (Original Name: Ugly Number)

    Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...

  4. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  5. [LeetCode] Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. [LeetCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  7. 【13_263】Ugly Number

    简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: E ...

  8. Leetcode 313. super ugly number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  9. Leetcode 264. Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

随机推荐

  1. Android Studio SDK 更新方法

    通常情况下,下载Android SDK需要连接谷歌的服务器进行下载,由于国内水深火热的网络,速度基本为0.好在国内也有一个更新的镜像地址.本文章介绍如何在不FQ的情况下,使用国内镜像地址,更新andr ...

  2. VS2012 常用web.config配置解析之自定义配置节点

    在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...

  3. SQL-LINQ-Lambda语法对照,好记性不如烂笔头

    忘记的时候就翻阅翻阅吧~~ SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Empl ...

  4. jquery页面刷新reload

    今天知道了jquery页面刷新的一个方法,很简单的一句: window.location.reload()刷新当前页面,不得不说我jquery学的很糟糕,明明都学过,但是等用到的时候就想不起来了.不过 ...

  5. verilog 学习笔记

    1.在寄存器中: -1=1111 -2=1110 -3=1101 2.{1,0}=64‘H00000001_00000000;//默认是32位的位数-拼接: 3.defparam P1.Depth=1 ...

  6. linux查看tomcat版本

    进入tomcat bin目录下 然后执行 ./version.sh Server version: Apache Tomcat/6.0.26Server built:   March 9 2010 1 ...

  7. 向Array中添加改进的冒泡排序

    改进冒泡思路 如果在某次的排序中没有出现交换的情况,那么说明在无序的元素现在已经是有序了,就可以直接返回了. 改进冒泡实现 Function.prototype.method = function(n ...

  8. cygwin chmod 失效

    问题背景 为了在 Cygwin 下使用之前最喜爱的 screen 命令, 安装 Cygwin 时就选上了 screen 来运行一把 ganiks.liu@MAMIS-Gaiks-Liu /tmp $ ...

  9. boost之thread

    1.boost里的thread创建之后会立即启动. 代码示例: #include <iostream> #include <string> #include <vecto ...

  10. 【BZOJ】【1031】【JSOI2007】字符加密Cipher

    后缀数组 当年感觉好神的题现在好像变水了…… 题意其实有点蛋疼……一开始没看懂<_< 将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们 ...