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 ...
随机推荐
- Android Studio SDK 更新方法
通常情况下,下载Android SDK需要连接谷歌的服务器进行下载,由于国内水深火热的网络,速度基本为0.好在国内也有一个更新的镜像地址.本文章介绍如何在不FQ的情况下,使用国内镜像地址,更新andr ...
- VS2012 常用web.config配置解析之自定义配置节点
在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...
- SQL-LINQ-Lambda语法对照,好记性不如烂笔头
忘记的时候就翻阅翻阅吧~~ SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Empl ...
- jquery页面刷新reload
今天知道了jquery页面刷新的一个方法,很简单的一句: window.location.reload()刷新当前页面,不得不说我jquery学的很糟糕,明明都学过,但是等用到的时候就想不起来了.不过 ...
- verilog 学习笔记
1.在寄存器中: -1=1111 -2=1110 -3=1101 2.{1,0}=64‘H00000001_00000000;//默认是32位的位数-拼接: 3.defparam P1.Depth=1 ...
- linux查看tomcat版本
进入tomcat bin目录下 然后执行 ./version.sh Server version: Apache Tomcat/6.0.26Server built: March 9 2010 1 ...
- 向Array中添加改进的冒泡排序
改进冒泡思路 如果在某次的排序中没有出现交换的情况,那么说明在无序的元素现在已经是有序了,就可以直接返回了. 改进冒泡实现 Function.prototype.method = function(n ...
- cygwin chmod 失效
问题背景 为了在 Cygwin 下使用之前最喜爱的 screen 命令, 安装 Cygwin 时就选上了 screen 来运行一把 ganiks.liu@MAMIS-Gaiks-Liu /tmp $ ...
- boost之thread
1.boost里的thread创建之后会立即启动. 代码示例: #include <iostream> #include <string> #include <vecto ...
- 【BZOJ】【1031】【JSOI2007】字符加密Cipher
后缀数组 当年感觉好神的题现在好像变水了…… 题意其实有点蛋疼……一开始没看懂<_< 将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们 ...