当前位置:网站首页>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;
}
边栏推荐
- cf:C. The Third Problem【关于排列这件事】
- BiShe - College Student Association Management System Based on SSM
- Hundreds of lines of code to implement a JSON parser
- [Arduino syntax - structure]
- Gartner released the prediction of eight major network security trends from 2022 to 2023. Zero trust is the starting point and regulations cover a wider range
- Leetcode 208. Implement trie (prefix tree)
- Ubantu check cudnn and CUDA versions
- Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
- 电气数据|IEEE118(含风能太阳能)
- The inconsistency between the versions of dynamic library and static library will lead to bugs
猜你喜欢
随机推荐
Ubantu check cudnn and CUDA versions
Logstash clear sincedb_ Path upload records and retransmit log data
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
Vulhub vulnerability recurrence 75_ XStream
How to extract MP3 audio from MP4 video files?
False breakthroughs in the trend of London Silver
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
Superfluid_ HQ hacked analysis
What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
Cf:c. the third problem
Cannot resolve symbol error
FFT 学习笔记(自认为详细)
现货白银的一般操作方法
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Unity | 实现面部驱动的两种方式
Unity | two ways to realize facial drive
Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
MATLB|实时机会约束决策及其在电力系统中的应用









