Leet Code OJ 263. Ugly Number [Difficulty: Easy]
生活随笔
收集整理的這篇文章主要介紹了
Leet Code OJ 263. Ugly Number [Difficulty: Easy]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
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]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leet Code OJ 169. Ma
- 下一篇: Leet Code OJ 100. Sa