[Swift]LeetCode264.丑数 II | 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.

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. 1 is typically treated as an ugly number.
  2. n does not exceed 1690.


编写一个程序,找出第 n 个丑数。

丑数就是只包含质因数 2, 3, 5 的正整数。

示例:

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

说明:

  1. 1 是丑数。
  2. n 不超过1690。


16ms

 1 class Solution {
 2     func nthUglyNumber(_ n: Int) -> Int {
 3         if n == 1 { return 1}
 4         var val1 = 2
 5         var val2 = 3
 6         var val3 = 5
 7         var i1 = 0
 8         var i2 = 0
 9         var i3 = 0
10         var result = [1]
11         for i in 1 ..< n {
12             let current = min(min(val1, val2), val3)
13             result.append(current)
14             if current == val1 {
15                 i1 += 1
16                 val1 = result[i1] * 2
17             }
18             if current == val2 {
19                 i2 += 1
20                 val2 = result[i2] * 3
21             }
22             if current == val3 {
23                 i3 += 1
24                 val3 = result[i3] * 5
25             }
26         }
27         return result[n - 1]
28     }
29 }


20ms

 1 class Solution {
 2     func nthUglyNumber(_ n: Int) -> Int{
 3
 4         var res = [1]
 5         var i2 = 0, i3 = 0, i5 = 0
 6         while res.count < n {
 7             let m2 = res[i2]*2, m3 = res[i3]*3, m5 = res[i5]*5
 8             let mn = min(min(m2, m3), m5)
 9             if m2 == mn { i2 += 1 }
10             if m3 == mn { i3 += 1 }
11             if m5 == mn { i5 += 1 }
12             res.append(mn)
13         }
14         return res.last!
15     }
16 }


36ms

 1 class Solution {
 2     func nthUglyNumber(_ n: Int) -> Int {
 3         var res = [1]
 4         var i = 0
 5         var j = 0
 6         var k = 0
 7
 8         while res.count < n {
 9
10             res.append(res[i] * 2)
11             i += 1
12
13             while res[j] * 3 < res[i] * 2 || res[k] * 5 < res[i] * 2 {
14                 if res[j] * 3 < res[i] * 2 && res[j] * 3 < res[k] * 5 {
15                     if res[j] % 2 != 0 {
16                         res.append(res[j] * 3)
17                     }
18                     j += 1
19                 }
20                 else {
21                     if res[k] % 2 != 0 && res[k] % 3 != 0 {
22                         res.append(res[k] * 5)
23                     }
24                     k += 1
25                 }
26             }
27
28         }
29
30         return res[n-1]
31     }
32 }


44ms

 1 class Solution {
 2     func nthUglyNumber(_ n: Int) -> Int {
 3         var res = [1]
 4         var c2 = 0, c3 = 0, c5 = 0
 5         var t2, t3, t5: Int
 6         var minp: Int
 7
 8         for _ in 1 ..< n {
 9             t2 = res[c2] * 2
10             t3 = res[c3] * 3
11             t5 = res[c5] * 5
12             minp = min(t2, t3, t5)
13             if t2 == minp {
14                 c2 += 1
15             }
16             if t3 == minp {
17                 c3 += 1
18             }
19             if t5 == minp {
20                 c5 += 1
21             }
22             res.append(minp)
23         }
24
25         return res.last!
26     }
27 }

原文地址:https://www.cnblogs.com/strengthen/p/10229952.html

时间: 2024-10-08 16:07:23

[Swift]LeetCode264.丑数 II | Ugly Number II的相关文章

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

public class Solution { public int GetUglyNumber_Solution(int index) { if(index<=0){ return 0; } int[] p = new int[index]; p[0]=1; int p2=0; int p3=0; int p5=0; int num=1;//索引 while (num < index){ int min = Min(p[p2]*2,p[p3]*3,p[p5]*5); p[num] = min

LeetCode(264):Ugly Number II

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

leetcode Ugly Number II

题目连接 https://leetcode.com/problems/ugly-number-ii/ Ugly Number II Description 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

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

263. 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.

[LeetCode]70. Ugly Number II第N个丑数

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

LeetCode OJ 之 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 a

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

编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数. 说明: 1 是丑数. n 不超过1690. dp含义: dp[i]表示第i-1个丑数. class Solution { public int nthUglyNumber(int n) { int[] dp = new int[n + 1]; dp[0] = 1; int i2

Leetcode264. Ugly Number II JAVA语言

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

[Swift]LeetCode263. 丑数 | 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. Example 1: Input: 6 Output: true Explanation: 6 = 2 × 3 Example 2: Input: 8 Output: true Explanation: 8 = 2