- 浏览: 183453 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
找到第n个ugly number, 第一我们维护ugly number从小到大的顺序,类似给三个有序链表l1, l2, l3排序,假设已经找到第k个ugly number, 那么第k+1个ugly number就是Math.min(l1 * 2, l2 * 3, l3 * 5)。代码如下:
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.
找到第n个ugly number, 第一我们维护ugly number从小到大的顺序,类似给三个有序链表l1, l2, l3排序,假设已经找到第k个ugly number, 那么第k+1个ugly number就是Math.min(l1 * 2, l2 * 3, l3 * 5)。代码如下:
public class Solution { public int nthUglyNumber(int n) { List<Integer> list = new ArrayList<Integer>(); int a = 0; int b = 0; int c = 0; list.add(1); while(list.size() < n) { int tem = Math.min(Math.min(list.get(a) * 2, list.get(b) * 3), list.get(c) * 5); list.add(tem); if(tem == list.get(a) * 2) a ++; if(tem == list.get(b) * 3) b ++; if(tem == list.get(c) * 5) c ++; } return list.get(n - 1); } }
发表评论
-
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)拓展而来,不过其因子质数,是事先给定的。
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 -*- ...