Python - 2sum modified


  • 1
    S
    class Solution(object):
        def judgeSquareSum(self, c):
            """
            :type c: int
            :rtype: bool
            """
            maxs = 1
            while (maxs * maxs) < c :
                maxs +=1 
            
            
            lo = 0
            hi = maxs
            while lo <= hi:
                if ((lo * lo) + (hi * hi)) == c :
                    return True
                if ((lo * lo) + (hi * hi)) < c :
                    lo += 1
                if ((lo * lo) + (hi * hi)) > c :
                    hi -= 1
            
            # Bruthe force until maxs O(n^2)
            # for i in range(1,maxs):
            #    for j in range(1,maxs):
            #        if c == (i*i + j*j):
            #            return True
            
            return False

Log in to reply
 

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