# [sum of code] report

personal blog: http://fuxuemingzhu.cn/

directory
Double pointer list generation loop
The date of

Topic describes
Given a non-negative integer c, your task is to decide whether there’s 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
``````

Subject to
Can a number consist of the sum of squares of two Numbers?
The problem solving method
Double pointer
The two Pointers are closer to the center, so it’s easier to understand.

``````class Solution(object):
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
left = 0
right = int(c ** 0.5)
while left <= right:
cur = left ** 2 + right ** 2
if cur < c:
left += 1
elif cur > c:
right -= 1
else:
return True
return False

``````

List generation
Xrange is a spanning form, and range returns a list. Determine if the number left after removing a square is a square.

``````class Solution(object):
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
def is_square(N):
return int(N ** 0.5) ** 2 == N

return any(is_square(c - a ** 2) for a in xrange(int(c ** 0.5) + 1))
``````

cycle
Use the loop to see if there is an A, so that c minus a squared is a perfect square.
There are a lot of ways to tell if something is a perfect square, but I’m going to use the simplest way, which is to take the square root and then square it to see if it’s equal.
The Python solution is as follows:

``````class Solution(object):
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
if c == 0: return True
for a in range(1, int(math.sqrt(c) + 1)):
b = c - a * a
if int(math.sqrt(b)) ** 2 == b:
return True
return False
``````

C++ solution is as follows:

``````class Solution {
public:
bool judgeSquareSum(int c) {
if (c == 0) return true;
for (int a = 1; a < (int) sqrt(c) + 1; ++a){
double b = sqrt(c - a * a);
if (b == (int) b){
return true;
}
}
return false;
}
};
``````

The date of
August 24, 2017
November 24, 2018 — starting on Sunday! A week has passed