自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

讓我們一起學(xué)學(xué)丑數(shù),你會(huì)了嗎?

開(kāi)發(fā) 前端
我們把只包含質(zhì)因子 2、3 和 5 的數(shù)稱作丑數(shù)(Ugly Number)。求按從小到大的順序的第 n 個(gè)丑數(shù)。

[[440250]]

本文轉(zhuǎn)載自微信公眾號(hào)「程序員千羽」,作者程序員千羽。轉(zhuǎn)載本文請(qǐng)聯(lián)系程序員千羽公眾號(hào)。

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

丑數(shù)

“題目描述 :我們把只包含質(zhì)因子 2、3 和 5 的數(shù)稱作丑數(shù)(Ugly Number)。求按從小到大的順序的第 n 個(gè)丑數(shù)。難度:中等

示例 :

  1. 輸入: n = 10 
  2. 輸出: 12 
  3. 解釋: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 個(gè)丑數(shù)。 

思路:

“丑數(shù)的遞推性質(zhì):丑數(shù)只包含因子2,3,5, 因此有“丑數(shù)=某較小丑數(shù)x洇子”(例如: 10=5x2)。

因此,可設(shè)置指針a, b,c指向首個(gè)丑數(shù)(即1 ),循環(huán)根據(jù)遞推公式得到下個(gè)丑數(shù), 并每輪將對(duì)應(yīng)指針執(zhí)行 +1即可。

復(fù)雜度分析:

  • 時(shí)間復(fù)雜度O(N) :中N=n,動(dòng)態(tài)規(guī)劃需遍歷計(jì)算dp列表。
  • 空間復(fù)雜度O(N) :長(zhǎng)度為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.  * @微信公眾號(hào) 程序員千羽 
  6.  * @個(gè)人網(wǎng)站 www.nateshao.cn 
  7.  * @博客 https://nateshao.gitee.io 
  8.  * @GitHub https://github.com/nateshao 
  9.  * @Gitee https://gitee.com/nateshao 
  10.  * Description: 丑數(shù) 
  11.  * 描述:我們把只包含質(zhì)因子 2、3 和 5 的數(shù)稱作丑數(shù)(Ugly Number)。求按從小到大的順序的第 n 個(gè)丑數(shù)。 
  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;// 第一個(gè)丑數(shù)為 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 個(gè)丑數(shù) 
  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/

 

責(zé)任編輯:武曉燕 來(lái)源: 程序員千羽
相關(guān)推薦

2021-05-31 09:23:04

管道模式責(zé)任鏈

2023-11-13 18:36:04

知識(shí)抽取NER

2023-10-31 14:04:17

Rust類型編譯器

2023-06-07 14:07:00

架構(gòu)

2023-01-03 08:13:26

GoModulesMaven

2023-04-13 08:40:12

MySQL服務(wù)器SELECT

2022-03-31 18:59:43

數(shù)據(jù)庫(kù)InnoDBMySQL

2021-08-27 07:06:10

IOJava抽象

2021-12-29 08:27:05

ByteBuffer磁盤(pán)服務(wù)器

2022-03-08 17:52:58

TCP格式IP

2021-07-15 07:23:28

Singlefligh設(shè)計(jì)

2021-11-26 07:00:05

反轉(zhuǎn)整數(shù)數(shù)字

2022-06-26 09:40:55

Django框架服務(wù)

2022-02-14 07:03:31

網(wǎng)站安全MFA

2016-09-06 10:39:30

Dell Techno

2022-02-14 10:16:22

Axios接口HTTP

2023-08-14 08:38:26

反射reflect結(jié)構(gòu)體

2022-06-15 08:00:50

磁盤(pán)RedisRocketMQ

2022-07-10 23:15:46

Go語(yǔ)言內(nèi)存

2022-08-01 07:57:03

數(shù)組操作內(nèi)存
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)