问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3862 访问。

编写一个程序判断给定的数是否为丑数。丑数就是只包含质因数 2, 3, 5 的正整数。

输入: 6

输出: true

解释: 6 = 2 × 3

输入: 8

输出: true

解释: 8 = 2 × 2 × 2

输入: 14

输出: false

解释: 14 不是丑数,因为它包含了另外一个质因数 7。

说明:

  1. 1 是丑数。
  2. 输入不会超过 32 位有符号整数的范围: [−231,  231 − 1]。

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.

Input: 6

Output: true

Explanation: 6 = 2 × 3

Input: 8

Output: true

Explanation: 8 = 2 × 2 × 2

Input: 14

Output: false

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

Note:

  1. 1 is typically treated as an ugly number.
  2. Input is within the 32-bit signed integer range: [−231,  231 − 1].

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3862 访问。

public class Program {

    public static void Main(string[] args) {
var n = 60;
var res = IsUgly(n);
Console.WriteLine(res); Console.ReadKey();
} private static bool IsUgly(int num) {
var uglyList = new int[] { 2, 3, 5 };
foreach(var ugly in uglyList) {
while(num % ugly == 0 && (num /= ugly) > 0) { }
}
return num == 1;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3862 访问。

True

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#263-丑数(Ugly Number)的更多相关文章

  1. [Swift]LeetCode263. 丑数 | Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  2. leetcode刷题笔记-1. 两数之和(java实现)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

  3. C#LeetCode刷题之#202-快乐数(Happy Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3856 访问. 编写一个算法来判断一个数是不是"快乐数& ...

  4. #leetcode刷题之路15-三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  5. #leetcode刷题之路12-整数转罗马数字

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 ...

  6. #leetcode刷题之路29- 两数相除

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...

  7. #leetcode刷题之路1-两数之和

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...

  8. C#LeetCode刷题之#507-完美数(Perfect Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...

  9. C#LeetCode刷题之#9-回文数(Palindrome Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3840 访问. 判断一个整数是否是回文数.回文数是指正序(从左向右 ...

  10. leetcode刷题第二天<两数相加>

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

随机推荐

  1. 三、Python系列——Pandas数据库读取数据

    Pandas主要先读取表格类型的数据,然后进行分析. import pandas as pd# 由于是用pandas模块操作数据,因此不用在路径前加open,否则就是python直接打开文件,可能还会 ...

  2. 【面试题资源共享】一文总结最高频软件测试|sq|语句|思维发散|计算机基础|Linux|测试用例|接口测试|等技术面试题

    思维发散 1.一个球, -把尺子长度是球直径的2/3,怎样测出半径?2.四枚硬币,花面朝上,每次翻转三个,几次可以将四枚硬币变为字面朝上?3. U2合唱团在1 7分钟内赶到演唱会现场问题?4.小明一家 ...

  3. void operator()()的功能

    在学习多线程的时候看到这样的一段代码,为什么要重载()呢?真有这个必要吗? #include <iostream> #include <thread> class Counte ...

  4. Ethical Hacking - GAINING ACCESS(6)

    Server Side Attack Analysing scan results and exploiting target system. Go to the Analysis page and ...

  5. Python Ethical Hacking - DNS Spoofing

    What is DNS Spoofing Sniff the DNSRR packet and show on the terminal. #!/usr/bin/env python from net ...

  6. C++ RMQ问题

    RMQ问题是区间求最值问题,就是求一个数组第i个到第j个中最大数或最小数的算法. 这个算法有一些倍增思想,也有一些二分思想.具体是一个数组,m[i][j]表示从i开始往后数2的j次方个数的最大值或最小 ...

  7. db2创建nickname

    db2创建nickname创建步骤 1.创建 server create server servername type DB2/AIX version 10.5 wrapper drda authid ...

  8. 最小生成树的java实现

    文章目录 一.概念 二.算法 2.1 Prim算法 2.2 Kruskal算法 笔记来源:中国大学MOOC王道考研 一.概念 连通图:图中任意两点都是连通的,那么图被称作连通图 生成树:连通图包含全部 ...

  9. websphere8.5配置db2数据源

    websphere8.5配置db2数据源 1. 打开websphere控制台  2.进入websphere变量页面  3. 进入DB2UNIVERSAL_JDBC_DRIVER_NATIVEPATH变 ...

  10. pyhton 3.6 pip 出现 Fatal error in launcher: Unable to create process using 解决方法

    ERROR:Fatal error in launcher: Unable to create process using '"' 出现这个  打开  终端  输入 python36 -m ...