LeetCode 633: Sum of Square Numbers Solution
Master LeetCode problem 633 (Sum of Square Numbers), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
633. Sum of Square Numbers
Problem Explanation
Explanation:
To solve this problem, we can use a two-pointer approach. We initialize two pointers, left at 0 and right at the square root of c. At each step, we calculate the sum of the squares of the values at these pointers. If the sum is equal to c, we return true. If the sum is less than c, we increment the left pointer to increase the sum, and if the sum is greater than c, we decrement the right pointer to decrease the sum. We continue this process until left is less than or equal to right.
Time complexity: O(sqrt(c)) Space complexity: O(1)
:
Solution Code
class Solution {
public boolean judgeSquareSum(int c) {
int left = 0, right = (int)Math.sqrt(c);
while (left <= right) {
int sum = left*left + right*right;
if (sum == c) {
return true;
} else if (sum < c) {
left++;
} else {
right--;
}
}
return false;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 633 (Sum of Square Numbers)?
This page provides optimized solutions for LeetCode problem 633 (Sum of Square Numbers) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 633 (Sum of Square Numbers)?
The time complexity for LeetCode 633 (Sum of Square Numbers) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 633 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 633 in Java, C++, or Python.