题目:
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。例如6,8是丑数,而14不是一个丑数,因为它有一个质因子7。
提示,1作为特例被认为是一个丑数。

分析:
尝试去除以2,3,5,如果可以除尽,则递归调用函数,如果都除不尽,说明这不是一个丑数。

代码:

public class Solution {public boolean isUgly(int num) {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;}
}

Leet Code OJ 263. Ugly Number [Difficulty: Easy]相关推荐

  1. Leet Code OJ 202. Happy Number [Difficulty: Easy]

    题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...

  2. Leet Code OJ 66. Plus One [Difficulty: Easy]

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  3. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  4. Leet Code OJ 283. Move Zeroes [Difficulty: Easy]

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  5. Leet Code OJ 292. Nim Game [Difficulty: Easy]

    题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

  6. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  7. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  8. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  9. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

最新文章

  1. IJCAI 2019:中国团队录取论文超三成,北大、南大榜上有名
  2. PyTorch机器学习自动化:自动框架搜索、超参优化
  3. Pytorch 词嵌入word_embedding2实例(加载已训练词向量)
  4. 学习程序开发的方法,仅供参考吧
  5. System Verilog Assertion for debug
  6. C程序设计语言现代方法05:选择语句
  7. 类字面常量和静态代码执行顺序
  8. linux/work
  9. WindStyle ExifInfo for Windows Live Writer发布
  10. bak 服务器备份文件怎么恢复,bak文件怎么还原
  11. linux基础ppt下载,《Linux基础》PPT课件.ppt
  12. 第19部分- Linux x86 64位汇编GDB单步调试
  13. docker 设备共享
  14. 计算机的防呆接口,PC接口全释:防呆口很重要,小心硬插烧硬件!
  15. mysql主从配置duxi_DUX主题
  16. crontab 定时任务时间格式设置
  17. vue3 腾讯地图输入地址或拖动标记获取经纬度
  18. 代码补全漫谈(1) - 从TabNine说起
  19. 涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
  20. [转载] 全本张广泰——第十二回 广泰退贼寇 技惊哈大人

热门文章

  1. 任意给4条边求形成四边形面积最大值
  2. Python Type Hints 从入门到实践
  3. 从 301 跳转,聊聊边缘规则的那些小妙用
  4. TCP协议疑难杂症全景解析|硬核
  5. Java集合框架:LinkedHashMap
  6. 深度解密Go语言之sync.map
  7. 锲而不舍 —— M 是怎样找工作的?(八)
  8. Django静态文件处理、中间件及Admin站点
  9. linux下使用python操作mysql
  10. 字节跳动---毕业旅行问题