[leetcode]633. Sum of Square Numbers

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]633. Sum of Square Numbers相关的知识,希望对你有一定的参考价值。

双指针比较简单的应用,搜索范围要注意

public boolean judgeSquareSum(int c) {
        /*
        双指针,搜索范围是0到sqrt(c)
         */
        if (c<0) return false;
        int left = 0;
        int right = (int)Math.sqrt(c);
        while (left<=right)
        {
            int cur = left*left+right*right;
            if (c>cur) left++;
            else if (c<cur) right--;
            else return true;
        }
        return false;
    }

 

以上是关于[leetcode]633. Sum of Square Numbers的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 633. Sum of Square Numbers

[leetcode]633. Sum of Square Numbers

633. Sum of Square Numbers

633. Sum of Square Numbers

633. Sum of Square Numbers

leecode题型整理之双指针