当前位置:网站首页>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:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
- How to extract MP3 audio from MP4 video files?
- Dede collection plug-in free collection release push plug-in
- Overview of Zhuhai purification laboratory construction details
- 1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
- Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
- cf:C. The Third Problem【关于排列这件事】
- False breakthroughs in the trend of London Silver
- Kotlin basics 1
- JVM_ 15_ Concepts related to garbage collection
猜你喜欢
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
How to see the K-line chart of gold price trend?
After 95, the CV engineer posted the payroll and made up this. It's really fragrant
Loop structure of program (for loop)
Threedposetracker project resolution
Idea sets the default line break for global newly created files
Building core knowledge points
Four commonly used techniques for anti aliasing
Mathematical modeling learning from scratch (2): Tools
cf:C. The Third Problem【关于排列这件事】
随机推荐
ThreeDPoseTracker项目解析
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
What is the most suitable book for programmers to engage in open source?
The inconsistency between the versions of dynamic library and static library will lead to bugs
1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
JVM_ 15_ Concepts related to garbage collection
95后CV工程师晒出工资单,狠补了这个,真香...
Zhuhai's waste gas treatment scheme was exposed
Recommended areas - ways to explore users' future interests
Introduction to robotics I. spatial transformation (1) posture, transformation
Vulhub vulnerability recurrence 75_ XStream
SPIR-V初窥
Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
黄金价格走势k线图如何看?
282. Stone consolidation (interval DP)
ubantu 查看cudnn和cuda的版本
毕设-基于SSM高校学生社团管理系统
The population logic of the request to read product data on the sap Spartacus home page
MYSQL---查询成绩为前5名的学生
Tcpdump: monitor network traffic