LeetCode之旅(17)-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.
思路分析:
题目的意思是判断一个数字是否由2,3,5乘积组成,1是除外的,座位符合条件的。通过是否是整除的思路解决,另外一个问题的思考:判断一个数字是不是被2,3,5相加组成
代码:
public class Solution {
public boolean isUgly(int num) {
if(num <= 0){
return false;
}
while(num % 2 == 0){
num = num/2;
}
while(num %3 == 0){
num = num/3;
}
while(num % 5 == 0){
num = num/5;
}
return num == 1;
}
}
LeetCode之旅(17)-Ugly Number的更多相关文章
- 【leetcode❤python】263. Ugly Number
class Solution(object): def isUgly(self, num): if num<=0:return False comlist=[2 ...
- 【leetcode】313. Super Ugly Number
题目如下: 解题思路:总结一下这么几点,一出一进,优先级队列排序,保证每次输出的都是当前的最小值.解法大致如图: 代码如下: #include<map> #include<queue ...
- [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 ...
- leetcode@ [263/264] Ugly Numbers & Ugly Number II
https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...
- 【一天一道LeetCode】#263. Ugly Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- <LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- 力不从心 Leetcode(ugly number heap) 263, 264,313
Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...
随机推荐
- iOS开发之自己封装的提示框(警告框)样式BHAlertView
最近需要使用到提示框(警告框)进行信息的展示和提醒,所以进行了一个类的封装,想用Swift调用此OC文件,但是发现有些困难,所以暂时先把OC代码进行展示,随后再好好研究一下在Swift中的使用. 对于 ...
- lager_transform未定义错误
lager_transform未定义错误rebar编译时报错:D:\server\six>d:/tools/rebar/rebar.cmd compile==> mysql (compil ...
- javascript之DOM对象
document方法 document.createElement(Tag) :创建一个html标签对象 document.getElementById(ID) :获得指定ID值的对象 documen ...
- Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略
Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略 事实上写这个也是因为自己实际在项目中用到了才会去研究已经写文章,对于View的移动,其实说实话,已经有很多文章 ...
- 安卓中的消息循环机制Handler及Looper详解
我们知道安卓中的UI线程不是线程安全的,我们不能在UI线程中进行耗时操作,通常我们的做法是开启一个子线程在子线程中处理耗时操作,但是安卓规定不允许在子线程中进行UI的更新操作,通常我们会通过Handl ...
- I/O操作之概述与导读
I/O流可以表示很多不同种类的输入源和输出目的地,包括简单的字节流,基本数据(int.boolean.double等),本地化的字符,以及对象.一些流只是简单地传递数据,还有一些流可以操作和转换数据 ...
- gradle2.0笔记——让项目升级到gradle2.0
昨晚看到QQ群消息说gradle2.0发布了,今天去看了一下,确实是昨天发布的,为rc版本:Gradle 2.0-rc-2.于是决定试一下. gradle可以在官网上下载,地址如下:http://ww ...
- 【一天一道LeetCode】#114. Flatten Binary Tree to Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 网站开发进阶(三十六)String.getBytes()方法中的中文编码问题
String.getBytes()方法中的中文编码问题 String的getBytes()方法是得到一个系统默认的编码格式的字节数组. getBytes("utf-8")得到一个U ...
- Oracle Enterprise Linux 64-bit 下Oracle11g的监听配置修改及测试步骤
测试环境:Oracle Enterprise Linux 64-bit (5.8版本) + Oracle 11g 64位 相关说明: Oracle11g64位软件的安装位置为/u01/app/orac ...