-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
52 lines (49 loc) · 1.19 KB
/
main.cpp
File metadata and controls
52 lines (49 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Source: https://leetcode.com/problems/ugly-number
// Title: Ugly Number
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// An **ugly number** is a positive integer which does not have a prime factor other than 2, 3, and 5.
//
// Given an integer `n`, return `true` if `n` is an **ugly number**.
//
// **Example 1:**
//
// ```
// Input: n = 6
// Output: true
// Explanation: 6 = 2 × 3
// ```
//
// **Example 2:**
//
// ```
// Input: n = 1
// Output: true
// Explanation: 1 has no prime factors.
// ```
//
// **Example 3:**
//
// ```
// Input: n = 14
// Output: false
// Explanation: 14 is not ugly since it includes the prime factor 7.
// ```
//
// **Constraints:**
//
// - `-2^31 <= n <= 2^31 - 1`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
class Solution {
public:
bool isUgly(int n) {
if (n <= 0) return false;
while (n > 1 && n % 2 == 0) n /= 2;
while (n > 1 && n % 3 == 0) n /= 3;
while (n > 1 && n % 5 == 0) n /= 5;
return n == 1;
}
};