- 浏览: 183455 次
- 性别:
- 来自: 济南
文章分类
最新评论
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取余,然后取整,如果最终的结果为1,说明为ugly number;否则返回false。代码如下:
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取余,然后取整,如果最终的结果为1,说明为ugly number;否则返回false。代码如下:
public class Solution { public boolean 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; if(num == 1) return true; return false; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
各种数据结构、算法及实用的C#源代码.C#,阿格里数(Ugly Number)的多种算法与源代码 阿格里数,即丑数(Ugly Number)、逊数(Humble Number)。 一般而言:把只包含质因子2,3和5的数称作丑数(Ugly Number)。...
各种数据结构、算法及实用的C#源代码 C#,超级阿格里数字(超级丑数,Super Ugly Number)的算法与源...超级阿格里数字(超级丑数,Super Ugly Number)由丑数(Ugly Number)拓展而来,不过其因子质数,是事先给定的。
算法与数据结构它们分别涵盖了以下主要内容: 数据结构(Data Structures): 逻辑结构:描述数据元素之间的逻辑关系,如线性结构(如数组、链表)、树形结构(如二叉树、堆、B树)、图结构(有向图、无向图等)...
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
1.4 Is Ugly Number(是否是丑数) 1.5 Is Power Of Two(是否是2的幂) 1.6 Is Power Of Three(是否是3的幂) 1.7 Count Primes(质数的个数) 2. Algorithm Implementation Questions (算法实现题) 3. Linked...
ugly[i] = next_ugly_number; if (next_ugly_number == next_multiple_of_2) { ++i2; next_multiple_of_2 = ugly[i2] * 2; } if (next_ugly_number == next_multiple_of_3) { ++i3; next_multiple_of_3 = ...
代码中的`It is not ugly number`程序生成并存储了前4000个丑数,然后根据用户输入查询特定位置的丑数。这里使用了动态规划和排序技术,动态规划的核心是避免重复计算,提高效率。在计算过程中,每次生成新的丑数时...
一、丑数(Ugly Number) 丑数是指只包含因子2、3和5的数。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。判断一个数是否为丑数可以通过连续除以2、3和5来实现。如果最后得到的...
丑数,又称不规则数,是指只包含质因数2、3和5的正整数。在编程领域,我们经常需要编写程序来判断一个...通过阅读和分析`UglyNumber-master.zip`中的源代码,你可以更深入地学习到丑数算法的实现细节和可能的优化策略。
1. 丑数的定义:在数论中,一个数如果其因子只有 2、3 和 5,则称这个数为丑数(Ugly Number)。例如 6、8 都是丑数,而 14 不是,因为它包含因子 7。 2. 丑数的判別算法:通过使用 while 循环不断地除以 2、3 和 5...
这类数被称为“丑数”(Ugly Number),是计算机科学中一个有趣的问题。 #### 解决思路: 为了找到第1500个丑数,可以通过动态规划的方式构建一个队列,并使用三个指针来维护当前的丑数。初始时,队列中只有一个...
java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring ...263.Ugly Number 264.Ugly Number II
- **UglyNumber(丑数)**:正整数中只包含质因数2、3、5的数。 ### 排序相关问题 - **SortColorsII(颜色分类 II)**:将数组中的元素分为有限种颜色,要求时间复杂度低于O(nlogn)。 - **MajorityNumber(多数元素...
1201.Ugly-Number-III(待定) (H) (男) (H) Binary Search by Value (男) (H-) (H-) (H) (H-) (H-) (H) (H-) (H-) (H) (H-) (H-) (男) (男) (H-) (M+) (男) (M+) (男) (M+) (H-) (M+
- **UglyNumber** - 寻找丑数。 - **PlusOne** - 数组表示的数字加1。 **5. 链表(LinkedList)** - **RemoveDuplicatesfromSortedList** - 删除排序链表中的重复元素。 - **RemoveDuplicatesfromSortedListII** ...
java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: echo server/client ##数据结构与算法 ...[Ugly Number] () [Ugly Number II] () [Repeated DNA Sequences] () [Lar
面试题49. 丑数题目链接面试题49. 丑数题目描述把只包含质因子2、3和5的数称作丑数(Ugly Number)。题解public class Solutio
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。 解法一:循环法 # -*- coding:utf-8 -*- ...