LeetCode 633 Sum of Square Numbers
2017-09-13 14:54
363 查看
题目:
Given a non-negative integer
that a2 + b2 = c.
Example 1:
Example 2:
题目链接
题意:
给一个非负的整数c,判断是否存在两个整数a,b满足a2 +
b2 =
c。
c是非负的,所以a和b的取值可以为0,所以a和b取值应该是[0, sqrt(c)],从0到sqrt(c)枚举一遍,假如c-a*a开平方后可以整除1,那么说明此时的a,b满足条件。
代码如下:
class Solution {
public:
bool judgeSquareSum(int c) {
int temp = sqrt(c);
for (int i = 0; i <= temp; i ++) {
int j = c - i * i;
if (fmod(sqrt(j), 1.00) != 0) {
continue;
}
return true;
}
return false;
}
};
Given a non-negative integer
c, your task is to decide whether there're two integers
aand
bsuch
that a2 + b2 = c.
Example 1:
Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3 Output: False
题目链接
题意:
给一个非负的整数c,判断是否存在两个整数a,b满足a2 +
b2 =
c。
c是非负的,所以a和b的取值可以为0,所以a和b取值应该是[0, sqrt(c)],从0到sqrt(c)枚举一遍,假如c-a*a开平方后可以整除1,那么说明此时的a,b满足条件。
代码如下:
class Solution {
public:
bool judgeSquareSum(int c) {
int temp = sqrt(c);
for (int i = 0; i <= temp; i ++) {
int j = c - i * i;
if (fmod(sqrt(j), 1.00) != 0) {
continue;
}
return true;
}
return false;
}
};
相关文章推荐
- Leetcode 633 Sum of Square Numbers
- LeetCode 633 : Sum of Square Numbers(python)
- leetcode[Sum of Square Numbers]
- [LeetCode] Sum of Square Numbers 平方数之和
- LeetCode Sum of Square Numbers
- Math-633-Sum of Square Numbers
- 【LeetCode】Sum of Square Numbers 解题报告
- [LeetCode] Print All Combinations of a Number as a Sum of Candidate Numbers
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- Sum of Square Numbers问题描述
- LeetCode.50(69\367\633) Pow(x,n)&&Sqrt(x)&&Valid Perfect Square&&Sum of Square Numbers
- Sum of Square Numbers
- [LeetCode] Print All Combinations of a Number as a Sum of Candidate Numbers
- LeetCode 371 : Sum of Two Integers(Java)
- [Leetcode] Sum Root to Leaf Numbers
- LeetCode 371: Sum of Two Integers 题解
- leetcode Sum Root to Leaf Numbers
- LeetCode(201) Bitwise AND of Numbers Range
- poj 2739 Sum of Consecutive Prime Numbers【素数筛】
- leetcode 129 —— Sum Root to Leaf Numbers