让我们一起学学丑数,你会了吗?

开发 前端
我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。

[[440250]]

本文转载自微信公众号「程序员千羽」,作者程序员千羽。转载本文请联系程序员千羽公众号。

Leetcode : https://leetcode-cn.com/problems/chou-shu-lcof/

“GitHub : https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_36_nthUglyNumber/Solution.java

丑数

“题目描述 :我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。难度:中等

示例 :

  1. 输入: n = 10 
  2. 输出: 12 
  3. 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 

思路:

“丑数的递推性质:丑数只包含因子2,3,5, 因此有“丑数=某较小丑数x洇子”(例如: 10=5x2)。

因此,可设置指针a, b,c指向首个丑数(即1 ),循环根据递推公式得到下个丑数, 并每轮将对应指针执行 +1即可。

复杂度分析:

  • 时间复杂度O(N) :中N=n,动态规划需遍历计算dp列表。
  • 空间复杂度O(N) :长度为N的dp列表使用0(N)的额外空间。
  1. package com.nateshao.sword_offer.topic_36_nthUglyNumber; 
  2.  
  3. /** 
  4.  * @date Created by 邵桐杰 on 2021/12/11 22:54 
  5.  * @微信公众号 程序员千羽 
  6.  * @个人网站 www.nateshao.cn 
  7.  * @博客 https://nateshao.gitee.io 
  8.  * @GitHub https://github.com/nateshao 
  9.  * @Gitee https://gitee.com/nateshao 
  10.  * Description: 丑数 
  11.  * 描述:我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。 
  12.  */ 
  13. public class Solution { 
  14.     public static void main(String[] args) { 
  15.         System.out.println("nthUglyNumber(10) = " + nthUglyNumber(10));//nthUglyNumber(10) = 12 
  16.         System.out.println("nthUglyNumber2(10) = " + nthUglyNumber2(10));//nthUglyNumber2(10) = 12 
  17.     } 
  18.  
  19.     /** 
  20.      * 思路:乘 2 或 3 或 5,之后比较取最小值。 
  21.      * 
  22.      * @param n 
  23.      * @return 
  24.      */ 
  25.     public static int nthUglyNumber(int n) { 
  26.         if (n <= 0) return 0; 
  27.         int[] arr = new int[n]; 
  28.         arr[0] = 1;// 第一个丑数为 1 
  29.         int multiply2 = 0, multiply3 = 0, multiply5 = 0; 
  30.         for (int i = 1; i < n; i++) { 
  31.             int min = Math.min(arr[multiply2] * 2, Math.min(arr[multiply3] 
  32.                     * 3, arr[multiply5] * 5)); 
  33.             arr[i] = min
  34.             if (arr[multiply2] * 2 == min) multiply2++; 
  35.             if (arr[multiply3] * 3 == min) multiply3++; 
  36.             if (arr[multiply5] * 5 == min) multiply5++; 
  37.         } 
  38.         return arr[n - 1];// 返回第 n 个丑数 
  39.     } 
  40.  
  41.     /** 
  42.      * 作者:Krahets 
  43.      * 
  44.      * @param n 
  45.      * @return 
  46.      */ 
  47.     public static int nthUglyNumber2(int n) { 
  48.         int a = 0, b = 0, c = 0; 
  49.         int[] dp = new int[n]; 
  50.         dp[0] = 1; 
  51.         for (int i = 1; i < n; i++) { 
  52.             int n2 = dp[a] * 2, n3 = dp[b] * 3, n5 = dp[c] * 5; 
  53.             dp[i] = Math.min(Math.min(n2, n3), n5); 
  54.             if (dp[i] == n2) a++; 
  55.             if (dp[i] == n3) b++; 
  56.             if (dp[i] == n5) c++; 
  57.         } 
  58.         return dp[n - 1]; 
  59.     } 
  60.  

 

参考链接:https://leetcode-cn.com/problems/chou-shu-lcof/solution/mian-shi-ti-49-chou-shu-dong-tai-gui-hua-qing-xi-t/

 

责任编辑:武晓燕 来源: 程序员千羽
相关推荐

2021-05-31 09:23:04

管道模式责任链

2023-11-13 18:36:04

知识抽取NER

2023-10-31 14:04:17

Rust类型编译器

2023-01-03 08:13:26

GoModulesMaven

2023-06-07 14:07:00

架构

2022-03-31 18:59:43

数据库InnoDBMySQL

2023-04-13 08:40:12

MySQL服务器SELECT

2021-08-27 07:06:10

IOJava抽象

2021-12-29 08:27:05

ByteBuffer磁盘服务器

2022-03-08 17:52:58

TCP格式IP

2021-11-26 07:00:05

反转整数数字

2021-07-15 07:23:28

Singlefligh设计

2016-09-06 10:39:30

Dell Techno

2022-02-14 07:03:31

网站安全MFA

2022-02-14 10:16:22

Axios接口HTTP

2022-06-26 09:40:55

Django框架服务

2023-08-14 08:38:26

反射reflect结构体

2023-08-02 08:35:54

文件操作数据源

2022-07-10 23:15:46

Go语言内存

2022-08-01 07:57:03

数组操作内存
点赞
收藏

51CTO技术栈公众号