public class Solution {
private bool Judge(int x)
{
if (x <= )
{
return false;
}
int bound = Convert.ToInt32(Math.Sqrt(x)); for (int i = ; i <= bound; i++)
{
if (x % i == )
{
return false;
}
}
return true;
} //素数筛法
private List<int> init(int num)
{
var prime = new List<int>();
bool[] mark = new bool[num + ];
for (int i = ; i <= num; i++)
{
mark[i] = false;
} for (int i = ; i <= num; i++)
{
if (mark[i])//排除2,3,5
{
continue;
}
prime.Add(i);
for (int j = i * i; j < num + ; j += i)
{
mark[j] = true;
}
}
return prime;
} public bool IsUgly(int num)
{
if (num <= ) { return false; }
if (num == ) { return true; }
if (num % == )
{
return IsUgly(num / );
}
if (num % == )
{
return IsUgly(num / );
}
if (num % == )
{
return IsUgly(num / );
}
return false;
}
}

https://leetcode.com/problems/ugly-number/#/description

本题同剑指Offer49

05-23 13:22