0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Leetcode 263. Ugly Number

Last updated at Posted at 2022-11-18

263. Ugly Number

難易度

  • EASY

アプローチ

  • Bruthforce

Java

class Solution {
    public boolean isUgly(int n) {
        if (n == 0) {
            return false;
        }
        if (2 <= n && n <= 6) {
            return true;
        }

        while (true) {
            if (n % 2 == 0) {
                n /= 2;
                continue;
            }

            if (n % 3 == 0) {
                n /= 3;
                continue;
            }
            if (n % 5 == 0) {
                n /= 5;
                continue;
            }
            break;
        }
        return n == 1;
    }
}

Golang

func isUgly(n int) bool {
    if n == 0 {
        return false
    }

    for {
        if n % 2 == 0{
            n /= 2
            continue
        }
        if n % 3 == 0{
            n /= 3
            continue
        }
        if n % 5 == 0{
            n /= 5;
            continue
        }
        break
    }
    return n == 1
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?