当前位置:网站首页>leetcode刷题_平方数之和
leetcode刷题_平方数之和
2022-07-06 01:19:00 【身影王座】
题目描述
给定一个非负整数 c
,你要判断是否存在两个整数 a
和 b
,使得 a2 + b2 = c
。
Java解决方法
class Solution {
public boolean judgeSquareSum(int c) {
long a = 0;
int b = (int)Math.sqrt(c);
while(a <= b)
{
// a * a + b * b有可能超过int取值范围,需要用long
if(a * a + b * b > c)
{
b--;
}
else if(a * a + b * b < c)
{
a++;
}
else
{
return true;
}
}
return false;
}
}
C解决方法
bool judgeSquareSum(int c){
long a = 0;
int b = (int)sqrt(c);
while(a <= b)
{
// a * a + b * b有可能超过int取值范围,需要用long
if(a * a + b * b > c)
{
b--;
}
else if(a * a + b * b < c)
{
a++;
}
else
{
return true;
}
}
return false;
}
边栏推荐
- Cannot resolve symbol error
- [day 30] given an integer n, find the sum of its factors
- 3D model format summary
- servlet(1)
- Programmer growth Chapter 9: precautions in real projects
- ThreeDPoseTracker项目解析
- Development trend of Ali Taobao fine sorting model
- JVM_ 15_ Concepts related to garbage collection
- Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
- 【第30天】给定一个整数 n ,求它的因数之和
猜你喜欢
Beginner redis
Finding the nearest common ancestor of binary tree by recursion
2020.2.13
False breakthroughs in the trend of London Silver
Hcip---ipv6 experiment
ADS-NPU芯片架构设计的五大挑战
Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
Cf:h. maximum and [bit operation practice + K operations + maximum and]
Vulhub vulnerability recurrence 74_ Wordpress
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
随机推荐
WordPress collection plug-in automatically collects fake original free plug-ins
Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
servlet(1)
RAID disk redundancy queue
MySQL learning notes 2
Zhuhai's waste gas treatment scheme was exposed
ThreeDPoseTracker项目解析
[Arduino syntax - structure]
Leetcode sword finger offer 59 - ii Maximum value of queue
毕设-基于SSM高校学生社团管理系统
MATLB|实时机会约束决策及其在电力系统中的应用
ORA-00030
Threedposetracker project resolution
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
Vulhub vulnerability recurrence 75_ XStream
Is chaozhaojin safe? Will it lose its principal
Leetcode daily question solution: 1189 Maximum number of "balloons"
Some features of ECMAScript
Spir - V premier aperçu
视频直播源码,实现本地存储搜索历史记录