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 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.

如果一个整数的质因子只有 2,3,5 ,那么这个数称为 Ugly Number。另外, 1 也被认为是 Ugly Number。

这道题比较简单,但是想要一次做对也要仔细考虑清楚各种情况。最开始,我的代码写成了这样:

bool isUgly(int num)
{
    while(num % 2 == 0)
    {
        num /= 2;
    }
    while(num % 3 == 0)
    {
        num /= 3;
    }
    while(num % 5 == 0)
    {
        num /= 5;
    }
    return num == 1;
}

提交之后发现竟然超出时间限制了。仔细想想,当 num = 0 时,代码中的每一个 while 循环都是死循环。所以还要加一句:

bool isUgly(int num)
{
    if(num <= 0) return false;
    while(num % 2 == 0)
    {
        num /= 2;
    }
    while(num % 3 == 0)
    {
        num /= 3;
    }
    while(num % 5 == 0)
    {
        num /= 5;
    }
    return num == 1;
}

这样代码就没有问题了。

时间: 2024-10-29 01:16:34

LeetCode 第 263 题 (Ugly Number)的相关文章

[leetcode 263 264]Ugly Number I II

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 ty

leetcode笔记:Super Ugly Number

一. 题目描述 Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the f

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

leetcode第136题-Single Number

题目要求:给出一个数组,只有一个数字出现一次,其他的都出现两次,找出那出现一次的数字,要求用线性的时间解出题目! 分析:因为题目要求的是用线性时间,所以类似于那种暴力解决的方法会超时,如下面这种: int singleNumber2(int *nums,int n) { int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(nums[i]==nums[j]) break; else continue; } if(j==n) return num

leetcode第202题-Happy Number

说实话,如果不看别人的解答的话,这道题我也是没有思路,不知道该循环几次,也不知道循环的终止条件,后来才知道,[2-6]这个范围内的数字都不是happy number 所以就有了终止条件,n>6就是终止条件,当n跳进这个范围内的时候就终止循环,最后就能判断是否是happy number了 #include<stdio.h> #include<stdlib.h> bool isHappy(int n) { while(n>6) { int sum=0; while(n) {

leetcode第137题-Single Number II

#include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int count[32]={0}; int i,j,number=0; for(i=0;i<numsSize;i++) { for(j=0;j<32;j++) count[j]+=((nums[i]&(1<<j))!=0); } for(i=0;i<32;i++) { if(coun

leetcode Ugly Number II

题目连接 https://leetcode.com/problems/ugly-number-ii/ Ugly Number II Description Write a program to find the $n_{th}$ ugly number. Ugly numbers are positive numbers whose prime factors only include $2, 3, 5.$ For example,$ 1, 2, 3, 4, 5, 6, 8, 9, 10, 12

【13_263】Ugly Number

简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: Easy 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

leetcode:263 Ugly Number-每日编程第十七题

Ugly Number Total Accepted: 31482 Total Submissions: 90982 Difficulty: Easy 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 whil