标签:
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
.
Note that 1
is typically treated as an ugly number.
Given num = 8
return true
Given num = 14
return false
分析:
根据ugly number的定义,不难想出答案。
1 public class Solution { 2 /** 3 * @param num an integer 4 * @return true if num is an ugly number or false 5 */ 6 public boolean isUgly(int num) { 7 if (num <= 0) return false; 8 9 while(num % 5 == 0) { 10 num = num / 5; 11 } 12 13 while(num % 3 == 0) { 14 num = num / 3; 15 } 16 17 while(num % 2 == 0) { 18 num = num / 2; 19 } 20 21 return num == 1; 22 } 23 }
Ugly number is a number that only have factors 2
, 3
and 5
.
Design an algorithm to find the nth ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12...
Note that 1
is typically treated as an ugly number.
If n=9
, return 10
.
分析:
1 class Solution { 2 /** 3 * @param k: The number k. 4 * @return: The kth prime number as description. 5 */ 6 public int nthUglyNumber(int k) { 7 if (k < 1) 8 return 0; 9 int i2 = 0, i3 = 0, i5 = 0; 10 int[] arr = new int[k]; 11 arr[0] = 1; 12 13 int index = 1; 14 while (index < arr.length) { 15 int v2 = arr[i2] * 2; 16 int v3 = arr[i3] * 3; 17 int v5 = arr[i5] * 5; 18 19 int min = min(v2, v3, v5); 20 arr[index] = min; 21 22 // cannot use if-else if-else structure because in some cases, v2, v3 v5 can be the same. 23 if (min == v2) { 24 i2++; 25 } 26 if (min == v3) { 27 i3++; 28 } 29 if (min == v5) { 30 i5++; 31 } 32 index++; 33 } 34 return arr[arr.length - 1]; 35 } 36 37 int min(int m1, int m2, int m3) { 38 return Math.min(m1, Math.min(m2, m3)); 39 } 40 41 42 };
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k
. For example,[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
.
1
is a super ugly number for any given primes.Given n = 6
, primes = [2, 7, 13, 19]
return 13
分析:
原理同上,这次只是改成了数组,而不是3个数。
1 public class Solution { 2 /** 3 * @param n a positive integer 4 * @param primes the given prime list 5 * @return the nth super ugly number 6 */ 7 8 public int nthSuperUglyNumber(int k, int[] primes) { 9 if (k < 1) 10 return 0; 11 int[] indexes = new int[primes.length]; 12 int[] values = new int[primes.length]; 13 int[] arr = new int[k]; 14 arr[0] = 1; 15 16 int index = 1; 17 while (index < arr.length) { 18 for (int i = 0; i < indexes.length; i++) { 19 values[i] = primes[i] * arr[indexes[i]]; 20 } 21 22 int min = getMin(values); 23 arr[index] = min; 24 25 for (int i = 0; i < values.length; i++) { 26 if (values[i] == min) { 27 indexes[i]++; 28 } 29 } 30 index++; 31 } 32 return arr[arr.length - 1]; 33 } 34 35 public int getMin(int[] arr) { 36 int min = arr[0]; 37 for (int k : arr) { 38 min = Math.min(min, k); 39 } 40 return min; 41 } 42 }
标签:
原文地址:http://www.cnblogs.com/beiyeqingteng/p/5645587.html