Skip to content

Commit 00c86ae

Browse files
committed
feat: update solutions to lc problems
* No.0633.Sum of Square Numbers * No.1346.Check If N and Its Double Exist * No.1351.Count Negative Numbers in a Sorted Matrix
1 parent 2a691e4 commit 00c86ae

File tree

22 files changed

+1079
-337
lines changed

22 files changed

+1079
-337
lines changed

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

+34-31
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939

4040
![](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0633.Sum%20of%20Square%20Numbers/images/table.png)
4141

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

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

4646
<!-- tabs:start -->
4747

@@ -50,17 +50,17 @@
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53-
class Solution(object):
54-
def judgeSquareSum(self, c):
55-
i, j = 0, int(math.sqrt(c))
56-
while i <= j:
57-
s = i * i + j * j
53+
class Solution:
54+
def judgeSquareSum(self, c: int) -> bool:
55+
a, b = 0, int(sqrt(c))
56+
while a <= b:
57+
s = a**2 + b**2
58+
if s == c:
59+
return True
5860
if s < c:
59-
i += 1
60-
elif s > c:
61-
j -= 1
61+
a += 1
6262
else:
63-
return True
63+
b -= 1
6464
return False
6565
```
6666

@@ -71,15 +71,16 @@ class Solution(object):
7171
```java
7272
class Solution {
7373
public boolean judgeSquareSum(int c) {
74-
int i = 0, j = (int) Math.sqrt(c);
75-
while (i <= j) {
76-
int s = i * i + j * j;
74+
long a = 0, b = (long) Math.sqrt(c);
75+
while (a <= b) {
76+
long s = a * a + b * b;
77+
if (s == c) {
78+
return true;
79+
}
7780
if (s < c) {
78-
++i;
79-
} else if (s > c) {
80-
--j;
81+
++a;
8182
} else {
82-
return true;
83+
--b;
8384
}
8485
}
8586
return false;
@@ -112,12 +113,13 @@ function judgeSquareSum(c: number): boolean {
112113
class Solution {
113114
public:
114115
bool judgeSquareSum(int c) {
115-
long i = 0, j = (long) sqrt(c);
116-
while (i <= j) {
117-
long s = i * i + j * j;
118-
if (s < c) ++i;
119-
else if (s > c) --j;
120-
else return true;
116+
long a = 0, b = (long) sqrt(c);
117+
while (a <= b)
118+
{
119+
long s = a * a + b * b;
120+
if (s == c) return true;
121+
if (s < c) ++a;
122+
else --b;
121123
}
122124
return false;
123125
}
@@ -128,15 +130,16 @@ public:
128130
129131
```go
130132
func judgeSquareSum(c int) bool {
131-
i, j := 0, int(math.Sqrt(float64(c)))
132-
for i <= j {
133-
s := i*i + j*j
133+
a, b := 0, int(math.Sqrt(float64(c)))
134+
for a <= b {
135+
s := a*a + b*b
136+
if s == c {
137+
return true
138+
}
134139
if s < c {
135-
i++
136-
} else if s > c {
137-
j--
140+
a++
138141
} else {
139-
return true
142+
b--
140143
}
141144
}
142145
return false

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

+32-29
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ From the upper right corner of the table, it is not difficult to find that it is
4242
### **Python3**
4343

4444
```python
45-
class Solution(object):
46-
def judgeSquareSum(self, c):
47-
i, j = 0, int(math.sqrt(c))
48-
while i <= j:
49-
s = i * i + j * j
45+
class Solution:
46+
def judgeSquareSum(self, c: int) -> bool:
47+
a, b = 0, int(sqrt(c))
48+
while a <= b:
49+
s = a**2 + b**2
50+
if s == c:
51+
return True
5052
if s < c:
51-
i += 1
52-
elif s > c:
53-
j -= 1
53+
a += 1
5454
else:
55-
return True
55+
b -= 1
5656
return False
5757
```
5858

@@ -61,15 +61,16 @@ class Solution(object):
6161
```java
6262
class Solution {
6363
public boolean judgeSquareSum(int c) {
64-
int i = 0, j = (int) Math.sqrt(c);
65-
while (i <= j) {
66-
int s = i * i + j * j;
64+
long a = 0, b = (long) Math.sqrt(c);
65+
while (a <= b) {
66+
long s = a * a + b * b;
67+
if (s == c) {
68+
return true;
69+
}
6770
if (s < c) {
68-
++i;
69-
} else if (s > c) {
70-
--j;
71+
++a;
7172
} else {
72-
return true;
73+
--b;
7374
}
7475
}
7576
return false;
@@ -102,12 +103,13 @@ function judgeSquareSum(c: number): boolean {
102103
class Solution {
103104
public:
104105
bool judgeSquareSum(int c) {
105-
long i = 0, j = (long) sqrt(c);
106-
while (i <= j) {
107-
long s = i * i + j * j;
108-
if (s < c) ++i;
109-
else if (s > c) --j;
110-
else return true;
106+
long a = 0, b = (long) sqrt(c);
107+
while (a <= b)
108+
{
109+
long s = a * a + b * b;
110+
if (s == c) return true;
111+
if (s < c) ++a;
112+
else --b;
111113
}
112114
return false;
113115
}
@@ -118,15 +120,16 @@ public:
118120
119121
```go
120122
func judgeSquareSum(c int) bool {
121-
i, j := 0, int(math.Sqrt(float64(c)))
122-
for i <= j {
123-
s := i*i + j*j
123+
a, b := 0, int(math.Sqrt(float64(c)))
124+
for a <= b {
125+
s := a*a + b*b
126+
if s == c {
127+
return true
128+
}
124129
if s < c {
125-
i++
126-
} else if s > c {
127-
j--
130+
a++
128131
} else {
129-
return true
132+
b--
130133
}
131134
}
132135
return false
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
class Solution {
2-
public:
3-
bool judgeSquareSum(int c) {
4-
long i = 0, j = (long) sqrt(c);
5-
while (i <= j) {
6-
long s = i * i + j * j;
7-
if (s < c) ++i;
8-
else if (s > c) --j;
9-
else return true;
10-
}
11-
return false;
12-
}
1+
class Solution {
2+
public:
3+
bool judgeSquareSum(int c) {
4+
long a = 0, b = (long) sqrt(c);
5+
while (a <= b)
6+
{
7+
long s = a * a + b * b;
8+
if (s == c) return true;
9+
if (s < c) ++a;
10+
else --b;
11+
}
12+
return false;
13+
}
1314
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
func judgeSquareSum(c int) bool {
2-
i, j := 0, int(math.Sqrt(float64(c)))
3-
for i <= j {
4-
s := i*i + j*j
5-
if s < c {
6-
i++
7-
} else if s > c {
8-
j--
9-
} else {
10-
return true
11-
}
12-
}
13-
return false
1+
func judgeSquareSum(c int) bool {
2+
a, b := 0, int(math.Sqrt(float64(c)))
3+
for a <= b {
4+
s := a*a + b*b
5+
if s == c {
6+
return true
7+
}
8+
if s < c {
9+
a++
10+
} else {
11+
b--
12+
}
13+
}
14+
return false
1415
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
class Solution {
2-
public boolean judgeSquareSum(int c) {
3-
int i = 0, j = (int) Math.sqrt(c);
4-
while (i <= j) {
5-
int s = i * i + j * j;
6-
if (s < c) {
7-
++i;
8-
} else if (s > c) {
9-
--j;
10-
} else {
11-
return true;
12-
}
13-
}
14-
return false;
15-
}
16-
}
1+
class Solution {
2+
public boolean judgeSquareSum(int c) {
3+
long a = 0, b = (long) Math.sqrt(c);
4+
while (a <= b) {
5+
long s = a * a + b * b;
6+
if (s == c) {
7+
return true;
8+
}
9+
if (s < c) {
10+
++a;
11+
} else {
12+
--b;
13+
}
14+
}
15+
return false;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +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
1+
class Solution:
2+
def judgeSquareSum(self, c: int) -> bool:
3+
a, b = 0, int(sqrt(c))
4+
while a <= b:
5+
s = a**2 + b**2
6+
if s == c:
7+
return True
8+
if s < c:
9+
a += 1
10+
else:
11+
b -= 1
12+
return False

0 commit comments

Comments
 (0)