当前位置:网站首页>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;
}
边栏推荐
- MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
- Cf:c. the third problem
- Is chaozhaojin safe? Will it lose its principal
- Some features of ECMAScript
- What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
- SPIR-V初窥
- FFT learning notes (I think it is detailed)
- Overview of Zhuhai purification laboratory construction details
- Dede collection plug-in free collection release push plug-in
- [Arduino syntax - structure]
猜你喜欢
How to see the K-line chart of gold price trend?
SSH login is stuck and disconnected
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
BiShe - College Student Association Management System Based on SSM
General operation method of spot Silver
Convert binary search tree into cumulative tree (reverse middle order traversal)
VSphere implements virtual machine migration
VMware Tools安装报错:无法自动安装VSock驱动程序
关于softmax函数的见解
Unity | 实现面部驱动的两种方式
随机推荐
MySQL learning notes 2
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
Leetcode1961. Check whether the string is an array prefix
How to get the PHP version- How to get the PHP Version?
Hundreds of lines of code to implement a JSON parser
Is chaozhaojin safe? Will it lose its principal
关于softmax函数的见解
Crawler request module
Leetcode1961. 检查字符串是否为数组前缀
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
IP storage and query in MySQL
Redis' cache penetration, cache breakdown, cache avalanche
Modify the ssh server access port number
Vulhub vulnerability recurrence 74_ Wordpress
Study diary: February 13, 2022
VMware Tools installation error: unable to automatically install vsock driver
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
Cve-2017-11882 reappearance
WGet: command line download tool
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa