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 ...
随机推荐
- Labview实现脉波调制( PDM )
Labview实现脉波调制( PDM ) 根据定义为脉冲宽度调制 生成一个正弦信号,得到其幅值输入给一个方波信号的占空比 由于方波信号的占空比里面含有正弦信号的信息 因此通过滤出方波信号的占空比信息则 ...
- Redis基础教程
说明:本文中涉及的代码是c#所写,连接redis的第三方驱动为ServiceStack.Redis.连接redis的客户端软件为redis-desktop-manager. 一.Redis是什么 Re ...
- cnblogs体验
用博客园可以让我更好的关注我们班同学的情况,通过关注他们,浏览他们发布的博客,学习到了很 多,比如不同的设计思想,良好的变成习惯,变量命名,缩进,注释等,而且自己不会的,可以帮助自己 有一些想法,是自 ...
- C++输出四则运算设计题的思路
一,(1)题目避免重复:使用srand(seed)函数进行随机化,随seed的不同,可以产生不同的随机数二,(1)控制数量:输入变量n控制三,(1)控制是否有乘除:(chengchu=0,没有乘除:c ...
- Python科学计算(一)环境简介——Anaconda Python
Anaconda Python 是 Python 科学技术包的合集,功能和 Python(x,y) 类似.它是新起之秀,已更新多次了.包管理使用 conda,GUI基于 PySide,所有的包基本上都 ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Python 文件读和写
- NSIS中判断.net framework 是否安装
在网上找了几个脚本,原理都是读取注册表进行判断,都可以用. 1.CheckDotNetFramework /* * Name: CheckDotNetFramework.nsh * Version: ...
- Jquery方法的应用
<body> <div id="one"><span>one</span></div><div class=&qu ...
- eclipse部署Tomcat6 : The server does not support version 3.0 of the JEE Web module specification
为项目添加tomcat 6,发现不能添加,原因如下 这是因为Tomcat6不能为JavaEE3.0版本服务,把项目的版本降低到2.5就可以了 现在可以部署了