Use two pointer like in "Two Sum"


  • 0
    S
    public class Solution {
        public boolean judgeSquareSum(int c) {
            int root = (int) Math.sqrt(c);
            int left = 0, right = root;
            while (left <= right) {
                if (left * left + right * right < c) {
                    left++;
                }
                else if (left * left + right * right > c) {
                    right--;
                }
                else {
                    return true;
                }
            }
            return false;
            
        }
    }
    

Log in to reply
 

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