A simple C++ solution


  • 0

    Key of this problem is to find from 2 to n there are how many numbers that can be divided as n=a*a.

    int bulbSwitch(int n) 
    {
    	if (0 == n)
    		return 0;
    	int x = 2;
    	int res = 1;
    	for (; pow(x, 2) < INT_MAX && pow(x, 2) <= n; ++x)
    		++res;
    	return res;
    }

Log in to reply
 

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