【LeetCode & 剑指offer刷题】特殊数题4:263 Ugly Number(系列)

【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

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 .
Example 1:
Input: 6
Output: true
Explanation: 6 = 2 × 3
Example 2:
Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2
Example 3:
Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7 .
Note:
  1. 1 is typically treated as an ugly number.
  2. Input is within the 32-bit signed integer range: [−2 31 ,  2 31  − 1].
 
class Solution
{
public :
    bool isUgly ( int num )
    {
        if ( num <= 0 ) return false ;
        while ( num % 2 == 0 ) num /= 2 ; //提取因子(如果除得进就一直除)
        while ( num % 3 == 0 ) num /= 3 ;
        while ( num % 5 == 0 ) num /= 5 ;
       
        return ( num == 1 );
    }
};
 
 
264 .   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 .
 
思路: We have an array   k   of first n ugly number. We only know, at the beginning, the first one, which is 1. Then
k[1] = min( k[0]x2, k[0]x3, k[0]x5). The answer is k[0]x2. So we move 2's pointer to 1. Then we test:
k[2] = min( k[1]x2, k[0]x3, k[0]x5). And so on. Be careful about the cases such as 6, in which we need to forward both pointers of 2 and 3.
/*
如:
k[0] = 1, 1*2 1*3 1*5
k[1] = 2, 2*2 1*3 1*5
k[2] = 3, 2*2 2*3 1*5
*/
#include <algorithm>
class Solution
{
public :
    int nthUglyNumber ( int n )
    {
        if ( n <= 0 ) return 0 ; //表示不存在
       
        vector < int > k ( n ); //用于存各丑数
        k [ 0 ] = 1 ;
        int p2 = 0 , p3 = 0 , p5 = 0 ; //对需要乘因子2,3,5的数的指针 
        for ( int i = 1 ; i < n ; i ++) //i = 1~n-1
        {
            k [ i ] = min(k[p2]*2, min(k[p3]*3, k[p5]*5));
            if ( k [ i ] == k [ p2 ]* 2 ) p2 ++; //只要相等就要移动,如6,需同时移动2和3的指针
            if ( k [ i ] == k [ p3 ]* 3 ) p3 ++;
            if ( k [ i ] == k [ p5 ]* 5 ) p5 ++;
        }
        return k [ n - 1 ];
    }
};
 
313 .   Super Ugly Number
Write a program to find the   n th   super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list   primes   of size   k .
Example:
Input: n = 12, primes = [2,7,13,19] Output: 32
Explanation: [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12
super ugly numbers given primes = [2,7,13,19] of size 4.
Note:
  • 1  is a super ugly number for any given  primes .
  • The given numbers in  primes  are in ascending order.
  • 0 <  k  ≤ 100, 0 <  n  ≤ 10 6 , 0 <  primes[i]  < 1000.
  • The n th  super ugly number is guaranteed to fit in a 32-bit signed integer.
 
//类似ugly number II
#include <climits>
class Solution
{
public :
    int nthSuperUglyNumber ( int n , vector < int >& primes )
    {
        if ( n <= 0 ) return 0 ; //表示不存在丑数
        int k = primes . size ();
        vector < int > index ( k , 0 ); //用于指向要乘某因子的丑数,初始化为0
        vector < int > ugly ( n );
        ugly [ 0 ] = 1 ;
        for ( int i = 1 ; i < n ; i ++) //i=1~n-1
        {
            int temp = INT_MAX ; //初始化为最大数,初始化为ugly[index[0]] * primes[0]较好
            for ( int j = 0 ; j < k ; j ++) temp = min ( temp , ugly [ index [ j ]] * primes [ j ]);
            for ( int j = 0 ; j < k ; j ++) //如果选取的是之前某个丑数乘某因子作为下个丑数,则该因子指针移动到下一个位置(否则下次乘出来还是最小的)
            {
                if ( temp == ugly [ index [ j ]] * primes [ j ]) index [ j ]++;
            }
            ugly [ i ] = temp ;
        }
        return ugly [ n - 1 ];
    }
};
 
 
 

猜你喜欢

转载自www.cnblogs.com/wikiwen/p/10225080.html