Skip to content

Commit f128c98

Browse files
committed
Ugly number.
1 parent 31d4c88 commit f128c98

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
3+
4+
Given an integer n, return true if n is an ugly number.
5+
6+
 
7+
8+
Example 1:
9+
Input: n = 6
10+
Output: true
11+
Explanation: 6 = 2 × 3
12+
13+
Example 2:
14+
Input: n = 1
15+
Output: true
16+
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
17+
18+
Example 3:
19+
Input: n = 14
20+
Output: false
21+
Explanation: 14 is not ugly since it includes the prime factor 7.
22+
 
23+
24+
Constraints:
25+
- -231 <= n <= 231 - 1
26+
*/
27+
class Solution {
28+
func isUgly(_ n: Int) -> Bool {
29+
if n == 0 { return false }
30+
var num = n
31+
while num % 2 == 0 || num % 3 == 0 || num % 5 == 0 {
32+
if num % 2 == 0 {
33+
num /= 2
34+
}
35+
if num % 3 == 0 {
36+
num /= 3
37+
}
38+
if num % 5 == 0 {
39+
num /= 5
40+
}
41+
}
42+
43+
if num == 1 { return true }
44+
return false
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/263.Ugly Number.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
56. [Valid Anagram](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/242.Valid%20Anagram.playground/Contents.swift)
6161
57. [Binary Tree Paths](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/257.Binary%20Tree%20Paths.playground/Contents.swift)
6262
58. [Add Digits](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/258.Add%20Digits.playground/Contents.swift)
63+
59. [Ugly Number](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/263.Ugly%20Number.playground/Contents.swift)
6364

6465
#### Medium
6566

0 commit comments

Comments
 (0)