// sqrt classSolution { public: booljudgeSquareSum(int c){ for (long a = 0; a * a <= c; a++) { double b = sqrt(c - a * a); if (b == (int)b) { returntrue; } } returnfalse; } };
// 双指针 classSolution { public: booljudgeSquareSum(int c){ long a = 0, b = (long)sqrt(c); while (a <= b) { long sum = a * a + b * b; if (sum == c) { returntrue; } elseif (sum < c) { a++; } else { b--; } } returnfalse; } };