ID | Title | Difficulty | |
---|---|---|---|
Loading... |
633. Sum of Square Numbers
Medium
LeetCode
Math, Two Pointers, Binary Search
Problem
Given a non-negative integer c, decide whether there’re two integers a and b such that $a^2 + b^2 = c$.
Example 1:
Input: c = 5
Output: true
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: c = 3
Output: false
Constraints:
- $0 <= c <= 2^{31} - 1$
Code
class Solution {
public boolean judgeSquareSum(int c) {
long left = 0;
long right = (long) Math.sqrt(c);
while (left <= right) {
long curr = left * left + right * right;
if(curr == c) return true;
if (curr < c) {
left++;
} else if (curr > c) {
right--;
}
}
return false;
}
}
按 <- 键看上一题!
628. Maximum Product of Three Numbers
按 -> 键看下一题!
634. Find the Derangement of An Array