Valid Perfect Square Solution in Java


  • 0
    A

    Hi,
    The simplest solution would be to first finding square root of given number and converting it to a integer value. Now multiply square root with square root and check if value is same as given number.

    public class PerfectSquare {

    public boolean isPerfectSquare(int number) {
    	
    	//Square root of given number and down cast it to integer
    	int sqrt =(int) Math.sqrt(number);
    	
    	//Multiple square root with square root and compare with given number
    	if(sqrt*sqrt == number)
    	{
    		return true;
    	}
    	return false;
    }
    

    }


Log in to reply
 

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