personal blog: http://fuxuemingzhu.cn/

directory

Double pointer list generation loop

The date of

Title address: https://leetcode.com/problems/sum-of-square-numbers/discuss/

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

### Read More:

- When generating a test report in HTML format, report [typeerror: a bytes like object is required, not ‘STR’]
- Anaconda opens Navigator to report an error and a web page appears Navigator Error An unexpected error occurred on Navigator start-up Report
- CONDITIONS EVALUATION REPORT
- Fallbackfactory error report solution
- Solution of xshell error report on 2021.05.03
- Using JSON to report errors
- Error report of mongodb startup under Linux
- Does flutter report an error after upgrading 2.5?
- Report ippicv error during opencv3 compilation
- Using Lombok to compile and report errors
- Minifiedreacterror — react error report notes
- POI related problem help report error recordformatexception
- Git report HTTP Basic: Access denied
- The project uses fastjason to report an error
- View the error report after Android APK confusion
- Java decompilation problem report international error
- Error report and flashback after packaging pynput into exe
- Error report when Hyper-V creates virtual machine
- Error report when installing arpspoof in Kali
- Design of MQ asynchronous collection report data