1. 程式人生 > >【LeetCode & 劍指offer刷題】特殊數題4:263 Ugly Number(系列)

【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 ];     } };