【LeetCode】263. Ugly Number

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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

按定义做。

class Solution {
public:
    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-08-25 09:04:21

【LeetCode】263. Ugly Number的相关文章

【Leetcode】Super Ugly Number

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

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c

【LeetCode】268. Missing Number

Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you i

【LeetCode】202. Happy Number

Happy Number Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the proc

【LeetCode】-- 260. Single Number III

问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, b). 要求常量空间,线性时间. 问题解决: 这题用到“神奇的位运算”. 1.因为除了特殊的两个元素,其余两两出现,那么就想到了XOR(异或). 2.从头到尾XOR之后,会得到a xor b 的结果.接下来我们试着把这两个元素分离开来. 3.我们在a xor b 中找到任意一位XOR[diff_po

【leetcode】Valid Triangle Number

题目: Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: Input: [2,2,3,4] Output: 3 Explanation: Valid c

【LeetCode】179. Largest Number

Description: For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Analysis: The problem can be solved by sorting. It's reallllllllllllllllllllllllllllllllllllllllllly a tallent and brilliant idea to give a compare function like

LeetCode之263. Ugly Number

------------------------------------------------------------- 如果一个数的质因子只包括2,3,5,那么这个数n可以表示为:n=2x+3y+5z AC代码: import java.math.BigInteger; public class Solution { public boolean isUgly(int n) { if(n<1) return false; while(n%2==0) n/=2; while(n%3==0) n