Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:

Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

 

Example 2:

Input: 3
Output: False
 1 class Solution {
 2 public:
 3     bool judgeSquareSum(int c) {
 4       int i = 0;
 5         int j =sqrt(c);
 6         while (i <= j)
 7         {
 8             if (i*i + j*j == c)
 9                 return true;
10             else
11             if (i*i + j*j < c)
12             {
13                 i++;
14             }
15             else
16                 j--;
17         }
18         return false;
19     }
20 };

 

posted on 2017-07-13 10:15  无惧风云  阅读(168)  评论(0编辑  收藏  举报