[LeetCode]#263. Ugly Number

Fatboy Slim
1 min readMar 20, 2020

--

Environment: Python 3.7

Key technique: For, while

Example 1:

Input: 6
Output: true
Explanation: 6 = 2 × 3

Example 2:

Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2

Example 3:

Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7.

Analysis:

Use “while” and % to determine that input num can be
Divisible by 2, 3, and 5.

Solution:

class Solution:
def isUgly(self, num):
if num<=0:
return False
n=[2,3,5]
for i in n:
while num%i==0:
num=num / i
if num==1:
return True
else:
return False

Submitted result:

Lesson learn:

Due to I don’t notice that Input is within the 32-bit signed integer range: [−2^31, 2^31 − 1]. I get many Time Limit Exceeded.

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet