LeetCode 263 Ugly Number

Source: Internet
Author: User

LeetCode 263 Ugly Number

Write a program to check whether the given number is an Ugly number ). An ugly number is a positive number. Its Qualitative factors include only 2, 3, and 5. For example, 6 and 8 are ugly numbers, and 14 is not ugly because they contain 7. Note that 1 is generally regarded as an 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.

When I encounter a question that is not very clear about the question, my usual practice is to first write a method that knows it is wrong to verify it.

For example, I tried it at the beginning and found that 4 is an ugly number: 2X2X1. Note 1 here. Note that 1 is an ugly number. Naturally, there is a line like this:

if(num == 1) return true;

There is another use of this Code, which will be discussed later. Because the ugly number is positive, 0 is not:

if(num == 0) return false;

Return to the example above, 4. We can divide it by 2, but then it can be divided by 2. Obviously, this requires recursion. If it is 8, divide it by three times, and the final factor is 1 to return the true result.

For 14, 7 is divided by 2, but 7 is not 0 for the remainder of 2, 3, and 5; similarly, if it is 28, it is divided by two times 2, the final result is the same.

OK, return to 0 and 1. They are so important because they must be used as recursive termination conditions. Obviously, 1 should return true, 0 will return false, but if the final result cannot be divisible directly as a return false, can it be merged so that it does not determine whether num is 0? No, because this will lead to an endless loop. Dividing 0 by any number is still 0.

So this question is solved.

class Solution {public:    bool isUgly(int num) {        if (num == 0) return false;        else if (num == 1) return true;        else if (num % 2 == 0)            return isUgly(num / 2);        else if (num % 3 == 0)            return isUgly(num / 3);        else if (num % 5 == 0)            return isUgly(num / 5);        else return false;    }};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.