[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;
    }

 

posted @ 2018-02-28 18:37  stAr_1  阅读(102)  评论(0编辑  收藏  举报