JAVA-------------Easy Version To Understand!!!!!!!!!!!!!


  • 0
    H
    	public int numSquares(int n) {
    	if (n <= 0)
    		return 0;
    	while (n % 4 == 0)
    		n = n / 4;
    	if (n % 8 == 7)
    		return 4;
    	for (int i = 0; i * i <= n; i++) {
    		int j = (int) Math.sqrt(n - i * i);
    		if (i * i + j * j == n) {
    			if (i == 0 || j == 0)
    				return 1;
    			else
    				return 2;
    		}
    	}
    	return 3;
    }

  • 0
    B

    Could you add some comments? Cannot understand.


  • 0
    C

    what do those magic number (3, 2, 1, 4 and etc.) mean?


Log in to reply
 

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