leetcode263题 题解 翻译 C语言版 Python版
来源:互联网 发布:java 日志系统 编辑:程序博客网 时间:2024/06/06 21:44
263. 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.
263.丑数
写一个程序来检查一个给定的数是不是一个丑陋的数。
丑陋的数是指那些质因子只包括2,3,5的正整数。比如说:6,8是丑数,但14不是丑数因为它有一个质因子7.
注意1比较特殊,我们把他看作丑数。
思路:给定一个数后,我们只需要将其每次除2或除3或除5,如果三个同时除不通就说明有其他质因子,只要有一个能除通就继续下一轮。注意特殊情况的单独处理。
bool isUgly(int num) { if (num <= 0) return false; if (num == 1) return true; while (num > 1){ int flag = 0; if (num % 2 != 0){ flag++; } else{ num /= 2; continue; } if (num % 3 != 0){ flag++; } else{ num /= 3; continue; } if (num %5 != 0){ flag++; } else { num /= 5; continue; } if (flag == 3) return false; } return true;}
class Solution(object): def isUgly(self, num): """ :type num: int :rtype: bool """ if num <= 0: return False if num == 1: return True while num > 1: flag = 0 if num % 2 != 0: flag += 1 else: num /= 2 continue if num % 3 != 0: flag += 1 else: num /= 3 continue if num % 5 != 0: flag += 1 else: num /= 5 continue if flag == 3: return False return True
0 0
- leetcode263题 题解 翻译 C语言版 Python版
- leetcode292题 题解 翻译 C语言版 Python版
- leetcode258题 题解 翻译 C语言版 Python版
- leetcode104题 题解 翻译 C语言版 Python版
- leetcode237题 题解 翻译 C语言版 Python版
- leetcode226题 题解 翻译 C语言版 Python版
- leetcode283题 题解 翻译 C语言版 Python版
- leetcode100题 题解 翻译 C语言版 Python版
- leetcode242题 题解 翻译 C语言版 Python版
- leetcode171题 题解 翻译 C语言版 Python版
- leetcode235题 题解 翻译 C语言版 Python版
- leetcode206题 题解 翻译 C语言版 Python版
- leetcode328题 题解 翻译 C语言版 Python版
- leetcode191题 题解 翻译 C语言版 Python版
- leetcode70题 题解 翻译 C语言版 Python版
- leetcode83题 题解 翻译 C语言版 Python版
- leetcode326题 题解 翻译 C语言版 Python版
- leetcode231题 题解 翻译 C语言版 Python版
- 安装Microsoft Expression Blend 4 的时候,提示 需要重启系统
- 1056. Mice and Rice (25)
- 适配器模式——媒婆
- 美名校书单
- 设计web容器
- leetcode263题 题解 翻译 C语言版 Python版
- GitHub上排名前50的iOS项目:总有一款你用得着
- Android IPC机制(五)用Socket实现跨进程聊天程序
- 64位PIL的安装
- Dolby(杜比)
- Laravel-lumen 配置JWT
- MyCat - 使用篇(3)
- hdu1298T9
- #学习笔记#(43)CSS-border绘制三角形