Skip to content

Commit f40e0dc

Browse files
authored
feat: add solutions to leetcode problem No.0633. Sum of Square Numbers (doocs#359)
1 parent 8ba96a2 commit f40e0dc

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

solution/0600-0699/0633.Sum of Square Numbers/README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,54 @@
5353

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

56+
![](./images/table.png)
57+
58+
上图为 a,b,c 之间的关系,这题其实就是在这张“表”里查找 c
59+
60+
从表的右上角看,不难发现它类似一棵二叉查找树,所以只需从右上角开始,按照二叉查找树的规律进行搜索
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

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

6268
```python
63-
69+
class Solution(object):
70+
def judgeSquareSum(self, c):
71+
i, j = 0, int(math.sqrt(c))
72+
while i <= j:
73+
s = i * i + j * j
74+
if s < c:
75+
i += 1
76+
elif s > c:
77+
j -= 1
78+
else:
79+
return True
80+
return False
6481
```
6582

6683
### **Java**
6784

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

7087
```java
71-
88+
class Solution {
89+
public boolean judgeSquareSum(int c) {
90+
int i = 0, j = (int) Math.sqrt(c);
91+
while (i <= j) {
92+
int s = i * i + j * j;
93+
if (s < c) {
94+
++i;
95+
} else if (s > c) {
96+
--j;
97+
} else {
98+
return true;
99+
}
100+
}
101+
return false;
102+
}
103+
}
72104
```
73105

74106
### **...**

solution/0600-0699/0633.Sum of Square Numbers/README_EN.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,50 @@
5353

5454
## Solutions
5555

56+
![](./images/table.png)
57+
58+
The picture above shows the relationship between `a`, `b`, and `c`. This question is actually looking up `c` in this table
59+
60+
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
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
```python
61-
67+
class Solution(object):
68+
def judgeSquareSum(self, c):
69+
i, j = 0, int(math.sqrt(c))
70+
while i <= j:
71+
s = i * i + j * j
72+
if s < c:
73+
i += 1
74+
elif s > c:
75+
j -= 1
76+
else:
77+
return True
78+
return False
6279
```
6380

6481
### **Java**
6582

6683
```java
67-
84+
class Solution {
85+
public boolean judgeSquareSum(int c) {
86+
int i = 0, j = (int) Math.sqrt(c);
87+
while (i <= j) {
88+
int s = i * i + j * j;
89+
if (s < c) {
90+
++i;
91+
} else if (s > c) {
92+
--j;
93+
} else {
94+
return true;
95+
}
96+
}
97+
return false;
98+
}
99+
}
68100
```
69101

70102
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def judgeSquareSum(self, c):
3+
i, j = 0, int(math.sqrt(c))
4+
while i <= j:
5+
s = i * i + j * j
6+
if s < c:
7+
i += 1
8+
elif s > c:
9+
j -= 1
10+
else:
11+
return True
12+
return False
Loading

0 commit comments

Comments
 (0)