LeetCode | 263. Ugly Number
题目
An ugly number is a positive integer whose prime factors are limited to 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 = 8
Output: true
Explanation: 8 = 2 × 2 × 2
Example 3:
Input: n = 14
Output: false
Explanation: 14 is not ugly since it includes the prime factor 7.
Example 4:
Input: n = 1
Output: true
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
Constraints:
-231 <= n <= 231 - 1
代码
class Solution {public:bool isUgly(int num) {if(num == 1)return true;if(num == 0)return false;while(num > 1){if(num % 2 == 0)num /= 2;else if(num % 3 == 0)num /= 3;else if(num % 5 == 0)num /= 5;elsebreak;}if(num == 1)return true;return false;}
};
虽然又是一次AC,但好像效率还可以再优化~
LeetCode | 263. Ugly Number相关推荐
- LeetCode 263. Ugly Number
题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...
- LeetCode - 263 - Ugly Number
给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质因数 2.3 和/或 5 的正整数. 示例: 输入:n = 6 输出:true ...
- 263.Ugly Number||202 happy number||476 Number Complement||136 Single Number
263.Ugly Number 判断因数是否只有素数2.3.5.. 感觉比较简单: class Solution(object):def isUgly(self, num):""& ...
- LeetCode 264. Ugly Number II--C++,Python解法
题目地址:Ugly Number II - LeetCode Write a program to find the n-th ugly number. Ugly numbers are positi ...
- leetcode 264. Ugly Number II
传送门 264. Ugly Number II QuestionEditorial Solution My Submissions Total Accepted: 36259 Total Submis ...
- Leet Code OJ 263. Ugly Number [Difficulty: Easy]
题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...
- LeetCode Super Ugly Number
题意:给出一个素数数组,求其第n个的丑数 思路:用idx数组表示在计算第i个丑数时,与素数数组对应的前一个丑数的下标.在计算完第i个丑数后,需要更新下次计算时的对应素数的下标 代码如下: public ...
- String | 263. Ugly Number
题目:丑数 方法1 class Solution { public:bool isUgly ( int num ) {if (num == 0)return false;while (num % 5 ...
- leetcode 263, 264, 1201, 313. Ugly Number I, II, III, Super Ugly Number(leetcode 丑数问题合集)
263. Ugly Number https://leetcode.com/problems/ugly-number/ 本题题解由下面的 264. Ugly Number II 改造而来,所以效率会比 ...
最新文章
- 2019年上半年收集到的人工智能迁移学习干货文章
- 仅支持BCH的众筹平台Coinfundr或能革新众筹业
- Android使用popueWindow自定义Spinner
- 随机数生成算法-二谈
- python在运行调试程序时常出现的错误有_Python 如何调试程序崩溃错误
- mysql 优化版_MySQL优化(超完整版)(一)
- MySQL数据库性能优化--SQL优化
- lua string操作
- C#的类,构造函数以及Array阵列的数据填充与绑定
- 坚持不放弃,修得好结果。
- C# foreach遇到async和await
- 修改Linux内核的启动Logo和禁用启动光标
- 在a标签中添加点击事件
- 干加个偏旁可以变成什么字_字好看的孩子,都是用了这些小学生学写字的方法...
- StarUML Choice控件写上文字Text
- 一个简单的JavaWeb项目
- Simulated maze模拟迷宫(C++实现)
- java中负数_Java中负数以及类型转换问题
- 明日书苑:史上书法字体分类最全(收藏版)
- 开源云服务器集群管理系统,#分享# Nano - 免费、开源基于CentOS/KVM虚拟化管理系统...