Skip to content

Commit

Permalink
feat: add solutions to leetcode problem No.0633. Sum of Square Numbers (
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong committed Apr 28, 2021
1 parent 8ba96a2 commit f40e0dc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
36 changes: 34 additions & 2 deletions solution/0600-0699/0633.Sum of Square Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,54 @@

<!-- 这里可写通用的实现逻辑 -->

![](./images/table.png)

上图为 a,b,c 之间的关系,这题其实就是在这张“表”里查找 c

从表的右上角看,不难发现它类似一棵二叉查找树,所以只需从右上角开始,按照二叉查找树的规律进行搜索

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution(object):
def judgeSquareSum(self, c):
i, j = 0, int(math.sqrt(c))
while i <= j:
s = i * i + j * j
if s < c:
i += 1
elif s > c:
j -= 1
else:
return True
return False
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public boolean judgeSquareSum(int c) {
int i = 0, j = (int) Math.sqrt(c);
while (i <= j) {
int s = i * i + j * j;
if (s < c) {
++i;
} else if (s > c) {
--j;
} else {
return true;
}
}
return false;
}
}
```

### **...**
Expand Down
36 changes: 34 additions & 2 deletions solution/0600-0699/0633.Sum of Square Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,50 @@

## Solutions

![](./images/table.png)

The picture above shows the relationship between `a`, `b`, and `c`. This question is actually looking up `c` in this table

From the upper right corner of the table, it is not difficult to find that it is similar to a binary search tree, so just start from the upper right corner and search according to the law of the binary search tree

<!-- tabs:start -->

### **Python3**

```python

class Solution(object):
def judgeSquareSum(self, c):
i, j = 0, int(math.sqrt(c))
while i <= j:
s = i * i + j * j
if s < c:
i += 1
elif s > c:
j -= 1
else:
return True
return False
```

### **Java**

```java

class Solution {
public boolean judgeSquareSum(int c) {
int i = 0, j = (int) Math.sqrt(c);
while (i <= j) {
int s = i * i + j * j;
if (s < c) {
++i;
} else if (s > c) {
--j;
} else {
return true;
}
}
return false;
}
}
```

### **...**
Expand Down
12 changes: 12 additions & 0 deletions solution/0600-0699/0633.Sum of Square Numbers/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution(object):
def judgeSquareSum(self, c):
i, j = 0, int(math.sqrt(c))
while i <= j:
s = i * i + j * j
if s < c:
i += 1
elif s > c:
j -= 1
else:
return True
return False
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f40e0dc

Please sign in to comment.