package com.heu.wsq.leetcode.double_point;
/**
* 633. 平方数之和
* @author wsq
* @date 2021/4/28
* 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。
*
* 示例 1:
* 输入:c = 5
* 输出:true
* 解释:1 * 1 + 2 * 2 = 5
*
* 示例 2:
* 输入:c = 3
* 输出:false
*
* 链接:https://leetcode-cn.com/problems/sum-of-square-numbers
*/
public class JudgeSquareSum {
/**
* 面对问题,首选不是要考虑使用什么算法或者数据结构,要针对问题进行分析,知道问题的解是什么、由什么组成,
* 当我们确定问题的解是什么了,这时候我们再去思考使用什么样的数据结构或者算法去 快速 的从解空间中查找到解
* @param c
* @return
*/
public boolean judgeSquareSum(int c){
if(c == 0){
return true;
}
long a = 0;
long b = (long) Math.sqrt(c);;
while(a <= b){
long x = a * a;
long y = b * b;
if(x + y == c){
return true;
}else if(x + y > c){
b--;
}else{
a++;
}
}
return false;
}
}