1. 程式人生 > >[leetcode]633. Sum of Square Numbers

[leetcode]633. Sum of Square Numbers

body ges sum pre -- mat sqrt else 註意

雙指針比較簡單的應用,搜索範圍要註意

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