Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to leetcode problem No.0633. Sum of Square Numbers #359

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.