LeetCode题解 -- 双指针(633)

北战南征 提交于 2020-03-07 02:13:59

Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there’re two integers a and b such that a2 + b2 = c.

相似题目:LeetCode题解 – 双指针(167)

时间复杂度:O(n)
空间复杂度:O(1)

public boolean judgeSquareSum(int c) {
        int hi = (int)Math.sqrt(c);
        int lo = 0;
        while(lo <= hi) {
            int temp = lo * lo + hi * hi;
            if(temp == c){
                return true;
            }else if(temp > c){
                hi--;
            }else{
                lo++;
            }
        }
        
        return false;
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!