633. Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:

Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

Example 2:

Input: 3
Output: False
题目含义:给一个数字c,判断是否满足a平方+b平方=c
思路:a和b最大为c开根号,最小为0.
复制代码
 1     public boolean judgeSquareSum(int c) {
 2         int a=0;
 3         int b=(int)Math.sqrt(c);
 4         while (a<=b)
 5         {
 6             double sum = a*a + b*b;
 7             if (sum ==c) return true;
 8             if (sum<c) a++;
 9             else b--;
10         }
11         return false;        
12     }
复制代码

 

posted @   daniel456  阅读(107)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示