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 10
ugly numbers.
Note that 1
is typically treated as an ugly number.
public class Solution { public int nthUglyNumber(int n) { int[] ugly = new int[n]; ugly[0] = 1; int i2 = 0; int i3 = 0; int i5 = 0; for (int i = 1; i < n; i++) { int cur = Math.min(ugly[i2] * 2, Math.min(ugly[i3] * 3, ugly[i5] * 5)); ugly[i] = cur; if (cur == ugly[i2] * 2) { i2++; } if (cur == ugly[i3] * 3) { i3++; } if (cur == ugly[i5] * 5) { i5++; } } return ugly[n-1]; } }
相关推荐
各种数据结构、算法及实用的C#源代码.C#,阿格里数(Ugly Number)的多种算法与源代码 阿格里数,即丑数(Ugly Number)、逊数(Humble Number)。 一般而言:把只包含质因子2,3和5的数称作丑数(Ugly Number)。...
各种数据结构、算法及实用的C#源代码 C#,超级阿格里数字(超级丑数,Super Ugly Number)的算法与源...超级阿格里数字(超级丑数,Super Ugly Number)由丑数(Ugly Number)拓展而来,不过其因子质数,是事先给定的。
java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic ...II ...II ...263.Ugly Number 264.Ugly Number II
137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [C++](./C++/single-number-ii.cpp) [Python](./Python/single-number-ii.py) | _O(n)_ | _O(1)_ | Medium ||| 190 | [Reverse Bits]...
算法与数据结构它们分别涵盖了以下主要内容: 数据结构(Data Structures): 逻辑结构:描述数据元素之间的逻辑关系,如线性结构(如数组、链表)、树形结构(如二叉树、堆、B树)、图结构(有向图、无向图等)...
java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: echo server/client ##数据结构与算法 ...[Ugly Number] ...[Ugly Number II] () [Repeated DNA Sequences] () [Lar
leetcode最大蓄水量 leetcode 记录自己leetocde的过程 2021.1.31 496 Next Greater Element I 复习了 linkedlist,arraylist的区别 ...Number II 三指针 2 3 5 2021.2.5 105 ConstructBinaryTreefromPreorderandI
scala-cli-maven-ugly-number 描述 将数字除以2、3和5的最大可除幂,如果数字变为1,则它是一个丑陋的数字,否则不是。 科技栈 OpenJDK的8 Scala 专家 Docker堆栈 docker-cli openjdk:8 要求 必须安装Docker桌面...
scala-cli-sbt-ugly-number-pattern-match描述将数字除以2、3和5的最大可除幂,如果数字变为1,则它是一个丑陋的数字,否则不是。 遵循函数式编程实践。 模式匹配的示例。科技栈OpenJDK的8 ScalasbtDocker堆栈docker...
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+
int ugly(int number){ while (number % 2 == 0) number /= 2; while (number % 3 == 0) number /= 3; while (number % 5 == 0) number /= 5; return (number == 1) ? 1 : 0; } int fun(int num){ if (num ) ...
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...
面试题49. 丑数题目链接面试题49. 丑数题目描述把只包含质因子2、3和5的数称作丑数(Ugly Number)。题解public class Solutio
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 = ...
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 Note: 1 is ...
假设您在ugly_code.m有一些代码。 您可以使用一个简单的命令来分析此代码中的问题: check ugly_code.m 这可能会生成如下报告: Code Analysis for ugly_code.m Required files: ugly_code.m, ugly_toolbox.m ...
代码中的`It is not ugly number`程序生成并存储了前4000个丑数,然后根据用户输入查询特定位置的丑数。这里使用了动态规划和排序技术,动态规划的核心是避免重复计算,提高效率。在计算过程中,每次生成新的丑数时...
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。 解法一:循环法 # -*- coding:utf-8 -*- ...