Given n =
- 5
// 1 * 1 + 2 * 2 = 5
- true
Given n =
Return
- -5
- 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 < 0) {
- return false;
- }
- int c = floor(sqrt(num));
- int i = c;
- if (i * i == num) return true;
- int j = 1;
- while (j <= i) {
- if (i * i + j * j == num) {
- return true;
- } else if (i * i + j * j < num) {
- j++;
- } else {
- i--;
- }
- }
- return false;
- }
- };
来源: http://www.bubuko.com/infodetail-2338026.html