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. 微信扫码支付asp.net(C#)实现步骤

    支付提交页面: [HttpPost] public ActionResult index(decimal amount) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯 ...

  2. javascript常用方法函数收集

    收集了一些比较常用的javascript函数. 1.字符串长度截取 function cutstr(str, len) { var temp, icount = 0, patrn = /[^\x00- ...

  3. SqlServer中Sql语句的逻辑执行顺序

    准备数据 Sql脚本如下,两张表,一张客户表Customers只包含customerid和city字段,一张订单表Orders包含orderid和customerid(关联Customers的cust ...

  4. centos安装redis3为系统服务

    源地址:http://my.oschina.net/haoqoo/blog/464247 <span></span>#无wget,请通过命令yum install wget安装 ...

  5. Node.js 学习(六)Node.js EventEmitter

    Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一个fs.read ...

  6. SASS学习笔记_01

      scss两种格式 sass    大括号 scss   css写法   Arguments:  --no-cache –style compressed --update $FileName$:c ...

  7. HDU 3555 Bomb 数位dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...

  8. Leetcode#145 Binary Tree Postorder Traversal

    原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...

  9. 了解javascript中的事件(二)

    本文目录如下: 零.寒暄 一.事件的分类 二.事件代理 2.1 问题引出 2.2 什么是事件代理 2.3 完整示例 二.事件代理 三.事件代理思想的用处 四.总结 零.寒暄 这篇博客本该出现在两个月以 ...

  10. shader 里面的分支

    shader 里面的真分支会降低效率 一种方法:构造一个分段函数出来 比如saturate(depth*1.5f)