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:

- Anaconda opens Navigator to report an error and a web page appears Navigator Error An unexpected error occurred on Navigator start-up Report
- Using JSON to report errors
- Report ippicv error during opencv3 compilation
- Using Lombok to compile and report errors
- Minifiedreacterror — react error report notes
- Design of MQ asynchronous collection report data
- Start cell keyerror and report an error
- Nailing dd.httpRequest report errors error:12
- Solution to report undefined a error when using jquery
- Error report on next page of GridView query view
- Nginx reverse proxy report 400 error solution!
- Python Report OpenSSL.SSL.SysCallError (10054 WSAECONRESET)
- Solve the problem of MySQL database report 1055 error
- CentOS uses Yum install to report errors
- SSHD key_read error report solution
- 107_ Error report and solution of erc20 token transfer
- PIP report failed to create process
- Using nginx page to report an error occurred
- Use keil software to report syntax error near ‘;
- Solution of connection error report in MySQL installation