当前位置:网站首页>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;
}
边栏推荐
- [pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
- Zhuhai laboratory ventilation system construction and installation instructions
- Construction plan of Zhuhai food physical and chemical testing laboratory
- 记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
- Pbootcms plug-in automatically collects fake original free plug-ins
- Paging of a scratch (page turning processing)
- 【第30天】给定一个整数 n ,求它的因数之和
- Unity | two ways to realize facial drive
- [Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
- The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell
猜你喜欢

Installation and use of esxi

JVM_ 15_ Concepts related to garbage collection

The population logic of the request to read product data on the sap Spartacus home page

cf:C. The Third Problem【关于排列这件事】

Differences between standard library functions and operators

Fibonacci number

Unity | two ways to realize facial drive

1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once

伦敦银走势中的假突破

毕设-基于SSM高校学生社团管理系统
随机推荐
Leetcode1961. Check whether the string is an array prefix
[day 30] given an integer n, find the sum of its factors
How does Huawei enable debug and how to make an image port
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
282. Stone consolidation (interval DP)
SPIR-V初窥
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
Electrical data | IEEE118 (including wind and solar energy)
Construction plan of Zhuhai food physical and chemical testing laboratory
Hundreds of lines of code to implement a JSON parser
China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
Cve-2017-11882 reappearance
Leetcode sword finger offer 59 - ii Maximum value of queue
MYSQL---查询成绩为前5名的学生
Loop structure of program (for loop)
Interview must brush algorithm top101 backtracking article top34
How to see the K-line chart of gold price trend?
Yii console method call, Yii console scheduled task
Unity | 实现面部驱动的两种方式