Java solution, greatest divide by 2, 3, 5


  • 23
    Y

    clean solution to greatest divide the num using 2, 3, and 5.

    public class Solution {
        public static boolean isUgly(int num) {
            if (num <= 0) {
                return false;
            }
            
            int[] divisors = {2, 3, 5};
            
            for(int d : divisors) {
                while (num % d == 0) {
                    num /= d;
                }
            }
            return num == 1;
        }
    }

  • 1
    J

    Thanks , very good solution!!!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.