zoukankan      html  css  js  c++  java
  • LeetCode 263. Ugly Number

    原题链接在这里:https://leetcode.com/problems/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.

    题解:

    判断一个数是不是ugly number, 是看它能否被2, 3, 5整除即可.

    AC Java:

     1 public class Solution {
     2     public boolean isUgly(int num) {
     3         if(num == 0){
     4             return false;
     5         }
     6         while(num%2 == 0){
     7             num/=2;
     8         }
     9         while(num%3 == 0){
    10             num/=3;
    11         }
    12         while(num%5 == 0){
    13             num/=5;
    14         }
    15         return num == 1;
    16     }
    17 }

    跟上Ugly Number II.

  • 相关阅读:
    特殊集合
    推箱子
    集合
    数组

    循环语句 练习题
    穷举与迭代
    循环语句
    练习题
    switch case
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4824954.html
Copyright © 2011-2022 走看看