lintcode:Ugly Number I
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 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.
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的更多相关文章
- [LintCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number`. Ugly numbers are positive number ...
- [LintCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 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 ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- 【13_263】Ugly Number
简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: E ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
随机推荐
- maven学习手记 - 3
学习目标 maven插件的定义: maven插件的使用. 前言 在手记2中说过maven的阶段命令是通过插件实现的.在手记1中也有简单的示范过插件的用法.但是总觉得有些泛泛了,想在这里再捋一下,以 ...
- 深入浅出TCP/IP簇
TCP/IP是“transmission Control Protocol/Internet Protocol”的简写,中文译名为传输控制协议/互联网络协议.TCP/IP不是一个协议,而是一个协议簇的 ...
- [转]ubuntu 12.04添加launcher方法
[转]ubuntu 12.04添加launcher方法 http://www.cnblogs.com/Jerryshome/archive/2012/08/21/2649500.html 对ubunt ...
- 将获得到的json赋值到下拉框
$(function () { $.getJSON('handler/addEmployees.ashx', function (json) { alert(json.length); //遍历赋值 ...
- A taste of urllib3
import urllib3 import certifi http = urllib3.PoolManager( cert_reqs='CERT_REQUIRED', # Force certifi ...
- P1572: [Usaco2009 Open]工作安排Job
做这道题走了不少弯路,其实本身也是很简单的,类似单调队列的东西.刚开始以为双关键字排序就行了,结果连WA两遍,忽然意识到可以在截止之前做这件事!!于是就规规矩矩的打队列,然而忘记找队列里的最小P做,当 ...
- 基于PBOC电子钱包的消费过程详解
智能卡金融行业应用电子钱包的消费交易流程,开发人员可参考 首先终端和卡片有一个共同的密钥叫做消费密钥:PurchKey (针对每种特定的交易,比如,圈存,消费,都有特定的密钥与之对应) 假设Purch ...
- 如何解决读取到文件末尾时碰到EOF导致的重复输出或者无用输出
当读取到文件末尾时,会碰到EOF,如何解决呢? 方法一:我们可以通过(ch=fin.get())!=EOF来结束读取,这样就不会像eof()那样碰到EOF之后,还会再进行一次读取,导致输出一个无 ...
- loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写
使用loadrunner进行压力测试主要分两步,第一步是编写脚本(比较重点),第二步执行测试(配置都是在界面上点点就行了,当然我只的是比较简单的,能满足日常需要的),第三步分析结果(这一步比较高深,但 ...
- 嵌入字体@font-face
嵌入字体@font-face @font-face能够加载服务器端的字体文件,让浏览器端可以显示用户电脑里没有安装的字体. 语法: @font-face { font-family : 字体名称; s ...