Why is it runtime error?


  • 0
    H
       bool isUgly(int num) 
    {
       bool tag = true;
    	if (num<1)
    	{
    		tag = false;
    		return tag;
    	}
    	int *p = new int[num+1];
    	memset(p, 0, sizeof(p)*(num + 1));
    	int n = abs(num);
    	int i = 2;
    	int j = 2;
    
    	for (i; i <= n; ++i)
    	{
    		if (p[i] != 1)
    		{
    			j = i*i;
    			while (j <= num)
    			{
    				p[j] = 1;
    				j += i;
    			}
    		}
    	}
    	for (i = 6; i <= n; ++i)
    	{
    		if (p[i] != 1 && num%i == 0)
    		{
    			tag = false;
    			break;
    		}
    	}
    	delete[]p;
    	return tag;
    }

  • 0
    M

    o(n^2) is too larger,you just need to judge if n can be divede by 2,3 or 5


  • 0
    D

    Correct, there is O(logn) complexity solution to this problem.


  • 0
    M

    go on,you will be better


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.