题目地址:Ugly Number II - LeetCode


Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Example:

Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note:

1 is typically treated as an ugly number.
n does not exceed 1690.


这道题目是经典的求丑数的题目。
比较容易想到的方法是使用队列,然后不断pop,把当前的数乘以2,3,5。
Python解法如下:

class Solution:def nthUglyNumber(self, n: int) -> int:count = 0l = [1]for i in range(0, n-1):temp = l.pop(0)for j in [2,3,5]:if j*temp not in l:l.append(j*temp)l.sort()return l[0]

这个做法的缺点是比较慢,因为每次都要做很多次判断。
用最小堆和set减少判断次数,时间消耗显著减少。

from heapq import heappop, heappush
class Solution:def nthUglyNumber(self, n: int) -> int:count = 0heap = []heappush(heap, 1)see=set(heap)for i in range(0, n-1):temp = heappop(heap)for j in [2,3,5]:if j*temp not in see:see.add(j*temp)heappush(heap, j*temp)return heappop(heap)

更快的,不用排序的解法如下:

class Solution:def nthUglyNumber(self, n: int) -> int:if n == 1:return 1l1 = [2]l2 = [3]l3 = [5]for i in range(0, n-2):temp = min(l1[0], l2[0], l3[0])if temp == l1[0]:l1.pop(0)l1.append(temp*2)l2.append(temp*3)l3.append(temp*5)elif temp == l2[0]:l2.pop(0)l2.append(temp*3)l3.append(temp*5)else:l3.pop(0)l3.append(temp*5)return min(l1[0], l2[0], l3[0])

上面这个解法也有问题,问题是需要pop,而pop操作是比较花费时间的,官方提供的动态规划的解法如下:

class Solution:def nthUglyNumber(self, n: int) -> int:if n == 1:return 1nums = [1]i2 = i3 = i5 = 0for i in range(0, n):ugly = min(nums[i2] * 2, nums[i3] * 3, nums[i5] * 5)nums.append(ugly)if ugly == nums[i2] * 2:i2 += 1if ugly == nums[i3] * 3:i3 += 1if ugly == nums[i5] * 5:i5 += 1return nums[n-1]

C++解法如下:

class Solution {public:int nthUglyNumber(int n) {if(n <= 0) return false; // get rid of corner cases if(n == 1) return true; // base caseint t2 = 0, t3 = 0, t5 = 0; //pointers for 2, 3, 5vector<int> k(n);k[0] = 1;for(int i  = 1; i < n ; i ++){k[i] = min(k[t2]*2,min(k[t3]*3,k[t5]*5));if(k[i] == k[t2]*2) t2++; if(k[i] == k[t3]*3) t3++;if(k[i] == k[t5]*5) t5++;}return k[n-1];}
};

LeetCode 264. Ugly Number II--C++,Python解法相关推荐

  1. leetcode 264. Ugly Number II

    传送门 264. Ugly Number II QuestionEditorial Solution My Submissions Total Accepted: 36259 Total Submis ...

  2. LeetCode - Medium - 264. Ugly Number II

    Topic Math Dynamic Programming Heap Description https://leetcode.com/problems/ugly-number-ii/ Analys ...

  3. 【异或】LeetCode 137. Single Number II

    LeetCode 137. Single Number II Solution1:不会做,抄的 博客转载自:http://www.cnblogs.com/grandyang/p/4263927.htm ...

  4. LeetCode 289. Game of Life--Java,Python解法

    此题链接:Game of Life - LeetCode According to the Wikipedia's article: "The Game of Life, also know ...

  5. LeetCode 264. 丑数 II

    264. 丑数 II Ideas 竟然没想到用小根堆,白学了,再把小根堆抄一遍. Code Python class Solution:def nthUglyNumber(self, n: int) ...

  6. LeetCode 1108. Defanging an IP Address--C++,Python解法

    题目地址:Defanging an IP Address - LeetCode Given a valid (IPv4) IP address, return a defanged version o ...

  7. LeetCode 709 To Lower Case -- java,python解法

    题目地址:To Lower Case - LeetCode Implement function ToLowerCase() that has a string parameter str, and ...

  8. leetcode - 264. 丑数 II

    264. 丑数 II -------------------------------------------- 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示 ...

  9. LeetCode 263. Ugly Number

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

最新文章

  1. 重建控制文件具体解释
  2. 【JavaSE_06】Java中的数组(array)-思维导图
  3. P4248 [AHOI2013]差异
  4. stm32中stm32f10x_type.h(固件3.0以前)、stm32f10x.h(固件3.0以后)、stdint.h文件的关系
  5. pytorch学习笔记(1):开始一个简单的分类器
  6. 用JavaScript将数字转换为大写金额
  7. 铜川市2021年高考成绩查询,2021年铜川高考各高中成绩排名查询,铜川高考成绩公布榜单...
  8. ios笔记一 追加数据
  9. 因为机遇,不会轻易悲伤
  10. “无法为保留分区分配驱动器号”的解决
  11. iview中Page分页组件添加首页尾页按钮
  12. C语言文件详解(一)文件介绍,文件打开和关闭
  13. 导出MySQL数据项到excel及数据错位的解决办法
  14. Ansible Role详解
  15. oracle如何进行多列分组统计,ORACLE分组统计
  16. [Paper Note] Densely Residual Laplacian Super-Resolution
  17. 【soft6星评论】北京软件规模破万亿,如何高质量发展?
  18. 交换机如何进行交换?三种交换机交换方式介绍
  19. matlab模糊函数
  20. 自然语言处理NLP星空智能对话机器人系列:深入理解Transformer自然语言处理 Training a GPT-2 language model Steps 2 to 6

热门文章

  1. 科学计算工具NumPy(2):ndarray的矩阵处理
  2. input禁止后怎么实现复制功能_(变强、变秃)Java从零开始之JQuery购物车功能实操...
  3. 边缘分布律_概率论笔记-Ch3随机向量及其分布
  4. 微信小程序setinterval_微信小程序中setInterval的使用方法
  5. aes key长度_原创 | 浅谈Shiro反序列化获取Key的几种方式
  6. 生信入门必须掌握的 30 个 Linux 命令
  7. QIIME 2教程. 27语义类型Semantic(2021.2)
  8. 一站式论文提升服务,助您顺利发文章!
  9. Office Word 2019中找不到EndnoteX9的解决方案
  10. 学术论文模式图、统计图绘制