leetcode@ [263/264] Ugly Numbers & Ugly Number II
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:
- The naive approach is to call
isUglyfor every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. - An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
- 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的更多相关文章
- 【Leetcode】【Medium】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 136 - Ugly Numbers
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
- 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 ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] 264. Ugly Number II 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- [leetcode] 264. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
随机推荐
- c/c++强制类型转换
转自c/c++强制类型转换 Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变 ...
- IDFA问题,苹果上传问题。improper Advertising identifier [IDFA] Usage.
原地址: 报告 improper Advertising identifier [IDFA] Usage. Your app contains the Advertising Identifier [ ...
- 在线学习SQL语句?没问题~~
以前弄得少,没注意.. http://sqlfiddle.com/ CREATE TABLE Presidents ( Id INT UNSIGNED NOT NULL AUTO_INCREMENT, ...
- JS作用域与闭包--实例
<script> "use strict" //函数作用域 function func(){ var arr = [1,3,5,7,9]; var sum = 0; f ...
- 如何用Java语言向串口读写数据
原作者:赛迪网作者 shihuchen ,我在他的基础上进行了部分修改 [赛迪网讯]串口, RS-232-C(又称EIA RS-232-C,以下简称RS232)是在1970年由美国电子工业协会(EIA ...
- Servlet课程0424(一) 通过实现Servlet接口来开发Servlet
//这是我的第一个Servlet,使用实现Servlet接口的方式来开发 package com.tsinghua; import javax.servlet.*; import java.io.*; ...
- iOS顶部滑动菜单:FDSlideBar 与NinaPagerView
FDSlideBar 是一个顶部滑动菜单,如常见的网易.腾讯新闻等样式.该控件支持自定颜色.字体等多种样式风格.菜单间切换流畅,具有较好的体验性.下部的内容展示经过挣 扎,最后选择了 UITableV ...
- Linux如何在虚拟机中挂载iso yum源
首先,将作为源的iso的挂载到系统上. 代码如下: mount -o loop /dev/cdrom /mnt/iso/ 或者 mount -o loop /xxx/xxx.iso /mnt/iso/ ...
- QT的QWidget和Delphi的TPanel很像,都是万能的基础控件
都只提供了最基本的功能,实际可以在上面随心所欲的创造新的控件.而自身也已经拥有基础的显示功能,而TCustomControl就不行. 比如,这样使用QWidget,直接就可以显示: void Main ...
- Android:监听ListView
本文目录 监听ListView点击事件 监听ListView滚动事件 监听ListView点击事件 使用监听器OnItemClickListener package com.example.tests ...