当前位置:网站首页>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;
}
边栏推荐
- Five challenges of ads-npu chip architecture design
- C language programming (Chapter 6 functions)
- In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
- Fibonacci number
- Superfluid_ HQ hacked analysis
- How to see the K-line chart of gold price trend?
- 037 PHP login, registration, message, personal Center Design
- The growth path of test / development programmers, the problem of thinking about the overall situation
- Opinions on softmax function
- 3D model format summary
猜你喜欢
毕设-基于SSM高校学生社团管理系统
Five challenges of ads-npu chip architecture design
Huawei converged VLAN principle and configuration
Dede collection plug-in free collection release push plug-in
Introduction to robotics I. spatial transformation (1) posture, transformation
现货白银的一般操作方法
Ubantu check cudnn and CUDA versions
ORA-00030
MUX VLAN configuration
Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
随机推荐
Kotlin core programming - algebraic data types and pattern matching (3)
3D model format summary
Electrical data | IEEE118 (including wind and solar energy)
The growth path of test / development programmers, the problem of thinking about the overall situation
Unity VR resource flash surface in scene
MYSQL---查询成绩为前5名的学生
【第30天】给定一个整数 n ,求它的因数之和
Leetcode 208. 实现 Trie (前缀树)
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Live video source code, realize local storage of search history
Vulhub vulnerability recurrence 74_ Wordpress
Modify the ssh server access port number
面试必刷算法TOP101之回溯篇 TOP34
记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
Superfluid_ HQ hacked analysis
FFT 学习笔记(自认为详细)
普通人下场全球贸易,新一轮结构性机会浮出水面
Leetcode daily question solution: 1189 Maximum number of "balloons"
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
Programmer growth Chapter 9: precautions in real projects