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 it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

class Solution {
public:
bool isUgly(int num) {
if(num <= ) return false;
if(num == ) return true; while(num% == ){
num/=;
if(num == ) return true;
}
while(num% == ){
num/=;
if(num == ) return true;
}
while(num% == ){
num/=;
if(num == ) return true;
}
return false;
}
};

leetcode 263: Ugly Numbers

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 10 ugly numbers.

Note that 1 is typically treated as an ugly number.

Hint:

  1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3
class Solution {
public:
int nthUglyNumber(int n) {
if(n <= ) return n; vector<long long> dp(n+, );
for(int i=;i<=;++i) dp[i] = i; for(int i=;i<=n;++i) {
long long Min = numeric_limits<long long>::max();
for(int pre=;pre<=i-;++pre) {
if(dp[pre]* > dp[i-]) {Min = min(Min, dp[pre] * ); continue;}
else if(dp[pre] * > dp[i-]) {Min = min(Min, dp[pre] * );continue;}
else if(dp[pre] * > dp[i-]) Min = min(Min, dp[pre] * );
}
dp[i] = Min;
} return dp[n];
}
};

leetcode 264: Ugly Numbers II

leetcode@ [263/264] Ugly Numbers & Ugly Number II的更多相关文章

  1. 【Leetcode】【Medium】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  2. 【leetcode刷题笔记】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  3. 136 - Ugly Numbers

     Ugly Numbers  Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...

  4. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  5. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  7. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  8. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  9. [leetcode] 264. Ugly Number II (medium)

    263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...

随机推荐

  1. HTML字符实体(Character Entities),转义字符串(Escape Sequence)

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  2. SOCI、LiteSQL、POCO数据库访问类库对比

    最近在做视频的开发,其中视频的设备接入管理服务器.流媒体管理服务器.中心服务器都涉及到了数据库的操作,同时需要兼容大多数版本的数据库,包括mysql.sqlite.oracle.公司原来使用的是ado ...

  3. HDU4647+贪心

    /* 贪心. 题意:给定一些点 一些边 点和边都有价值.现在A B 选点.求A-B的maxVal 思路:分割边.边的1/2分给两个端点. 如果这两个点被同一个人取,则ok:否则 做减法也行,对题意无影 ...

  4. UR #13 Yist

    第一次打UR,打了一个半小时就弃疗了QAQ 这是我唯一一道考试的时候做出来的题目,其他两道连暴力都懒得写了 很容易发现对于每个要删除的点 我们找到左边第一个比他小的不用删除的点,右边第一个比他小的不用 ...

  5. codeforces #309 div1 A

    先说我的解法吧 首先设f(i,j)表示选了前i个球且j种颜色都已经选完了的方案数 这显然是可以随便转移的 #include<cstdio> #include<cstring> ...

  6. beyond compare ftp 文件夹同步

    因为经常要同步服务器上的代码,今天试了一下beyond compare 的ftp同步非常爽.以前都只用了beyond compare的文件夹比较功能了,ftp功能没有使用过. 步骤1:点击:会话——& ...

  7. 两页pdf打印为一页,并且放大(打印英文pdf常用)

    多很英文书籍都是小书,若我们直接打印它的pdf会很厚,比如我要打印一本 thinking in C++,就要800+页.不如把两页打成一页.但是打成一页之后又太小了,需要放大.具体方法如下:   前提 ...

  8. dom解析器机制 web基本概念 tomcat

    0 作业[cn.itcast.xml.sax.Demo2]   1)在SAX解析器中,一定要知道每方法何时执行,及SAX解析器会传入的参数含义 1 理解dom解析器机制 1)dom解析和dom4j原理 ...

  9. 在XML里的XSD和DTD以及standalone的使用2----具体使用详解

    如何定义XSD并在XML中使用XSD 同时XSD可以对XML中的格式进行约束,当约束失败时给出提示. 下面以下使用VS2010为平台进行演示. 1.新建一个项目,然后在项目中添加xml架构文件(.xs ...

  10. SDOI2008Cave 洞穴勘测

    无限膜拜CLJ大牛…… 不会动态树的弱弱在CLJ的帮助下AC了此题 我想到了并查集(人人都会想到的吧……囧),但不知道应该如何处理destroy操作…… 其实 make操作的实质就是:把x节点到其所在 ...