lintcode: Check Sum of Square Numbers
2024-09-23 23:46:55
Check Sum of Square Numbers
Given a integer c
, your task is to decide whether there're two integers a and b such that a^2 + b^2 = c
.
您在真实的面试中是否遇到过这个题?
Yes
样例
Given n = 5
Return true
// 1 * 1 + 2 * 2 = 5
Given n = -5
Return false
代码
class Solution {
public:
/*
* @param : the given number
* @return: whether whether there're two integers
*/
bool checkSumOfSquareNumbers(int num) {
// write your code here
if (num < ) {
return false;
}
int c = floor(sqrt(num));
int i = c;
if (i * i == num) return true;
int j = ;
while (j <= i) {
if (i * i + j * j == num) {
return true;
} else if (i * i + j * j < num) {
j++;
} else {
i--;
}
}
return false;
}
};
最新文章
- ruby -- 进阶学习(一)subdomain配置与实现
- 你的java 代码对JIT编译友好吗?
- Objective-C设计模式——原型Prototype(对象创建)
- 【UVA1379】Pitcher Rotation (贪心+DP)
- js中的潜伏者之Arguments对象
- nutch2.3中nutch-site.xml设置说明
- FileStream:The process cannot access the file because it is being used by another process
- UITextField 设置 placeholder 的字体颜色方法
- 集美大学网络1413第十五次作业成绩(团队十) -- 项目复审与事后分析(Beta版本)
- 偏置方差分解Bias-variance Decomposition
- UOJ#73. 【WC2015】未来程序 提交答案题
- 【XSY2727】Remove Dilworth定理 堆 树状数组 DP
- js操作数组元素
- Hdu2015 偶数求和
- 彻底删除windows残留启动引导
- _ai_creature
- mysql row日志格式下 查看binlog sql语句
- kafka 主要内容介绍
- 《剑指offer》第二十一题(调整数组顺序使奇数位于偶数前面)
- Mysql主从同步在线实施步骤【适合大数据库从库配置】