Easy Python Solution, Sum of Two


  • 0
    Y

    The idea is to use two pointers scanning the sqrt(c) length, one from the beginning, the other from the end.

    class Solution(object):
        def judgeSquareSum(self, c):
            n=int(math.sqrt(c))
            i,j=0,n
            while i<=j:
                res=i*i+j*j
                if res==c:
                    return True
                elif res>c:
                    j-=1
                else:
                    i+=1
            return False
    #124 / 124 test cases passed.
    #Status: Accepted
    #Runtime: 139 ms
    

Log in to reply
 

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