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

Discuss中总有惊喜:

Java:

1 for (int i=2; i<6 && num>0; i++)
2     while (num % i == 0)
3         num /= i;
4 return num == 1;

Python:

1 for p in 2, 3, 5:
2     while num % p == 0 < num:
3         num /= p
4 return num == 1

C++:(其中&&之后的num怎么理解?)

1 for (int i=2; i<6 && num; i++)
2     while (num % i == 0)
3         num /= i;
4 return num == 1;

自己的:

C++:

 1 class Solution {
 2 public:
 3     bool isUgly(int num) {
 4         if (num <= 0)
 5             return false;
 6         if (num == 1)
 7             return true;
 8
 9         while (num != 1)    {
10             if (num % 2 == 0)   {
11                 num = num / 2;
12             }
13             else if (num % 3 == 0)  {
14                 num = num / 3;
15             }
16             else if (num % 5 == 0)  {
17                 num = num / 5;
18             }
19             else{
20                 return false;
21             }
22         }
23         return true;
24     }
25 };
时间: 2024-11-05 20:30:31

【13_263】Ugly Number的相关文章

【Leetcode】Ugly Number

题目链接:https://leetcode.com/problems/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 i

【Leetcode】Ugly Number II

题目链接:https://leetcode.com/problems/ugly-number-ii/ 题目: 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 is the sequence of the first

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<

【LeetCode从零单刷】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 i

【LeetCode】Single Number I &amp; II

Single Number I : Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Solution: 解法不少,贴一种: 1 cla

【leetcode78】Single Number II

题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra m

【LeetCode】Largest Number 解题报告

[题目] Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of

【LeetCode】Single Number

原文: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可