Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1433ff5

Browse files
committed
feat: add solutions to lc problems: No.0492,0988
No.0492.Construct the Rectangle No.0988.Smallest String Starting From Leaf
1 parent 84637d8 commit 1433ff5

File tree

13 files changed

+513
-95
lines changed

13 files changed

+513
-95
lines changed

‎solution/0400-0499/0492.Construct the Rectangle/README.md‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
<li>你设计的页面的长度和宽度必须都是正整数。</li>
3535
</ol>
3636

37-
3837
## 解法
3938

4039
<!-- 这里可写通用的实现逻辑 -->
@@ -48,17 +47,10 @@
4847
```python
4948
class Solution:
5049
def constructRectangle(self, area: int) -> List[int]:
51-
sr = int(math.sqrt(area))
52-
l = w = sr
53-
while l <= area and w >= 1:
54-
s = l * w
55-
if s == area:
56-
break
57-
if s > area:
58-
w -= 1
59-
else:
60-
l += 1
61-
return [l, w]
50+
w = int(sqrt(area))
51+
while area % w != 0:
52+
w -= 1
53+
return [area // w, w]
6254
```
6355

6456
### **Java**
@@ -68,16 +60,37 @@ class Solution:
6860
```java
6961
class Solution {
7062
public int[] constructRectangle(int area) {
71-
int sr = (int) Math.sqrt(area);
72-
int l = sr, w = sr;
73-
while (l <= area && w >= 1) {
74-
int s = l * w;
75-
if (s == area) break;
76-
if (s > area) --w;
77-
else ++l;
63+
int w = (int) Math.sqrt(area);
64+
while (area % w != 0) {
65+
--w;
7866
}
79-
return new int[]{l, w};
67+
return new int[]{area / w, w};
68+
}
69+
}
70+
```
71+
72+
### **C++**
73+
74+
```cpp
75+
class Solution {
76+
public:
77+
vector<int> constructRectangle(int area) {
78+
int w = sqrt(1.0 * area);
79+
while (area % w != 0) --w;
80+
return {area / w, w};
8081
}
82+
};
83+
```
84+
85+
### **Go**
86+
87+
```go
88+
func constructRectangle(area int) []int {
89+
w := int(math.Sqrt(float64(area)))
90+
for area%w != 0 {
91+
w--
92+
}
93+
return []int{area / w, w}
8194
}
8295
```
8396

‎solution/0400-0499/0492.Construct the Rectangle/README_EN.md‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ But according to requirement 2, [1,4] is illegal; according to requirement 3, [
4545
<li><code>1 &lt;= area &lt;= 10<sup>7</sup></code></li>
4646
</ul>
4747

48-
4948
## Solutions
5049

5150
<!-- tabs:start -->
@@ -55,34 +54,48 @@ But according to requirement 2, [1,4] is illegal; according to requirement 3, [
5554
```python
5655
class Solution:
5756
def constructRectangle(self, area: int) -> List[int]:
58-
sr = int(math.sqrt(area))
59-
l = w = sr
60-
while l <= area and w >= 1:
61-
s = l * w
62-
if s == area:
63-
break
64-
if s > area:
65-
w -= 1
66-
else:
67-
l += 1
68-
return [l, w]
57+
w = int(sqrt(area))
58+
while area % w != 0:
59+
w -= 1
60+
return [area // w, w]
6961
```
7062

7163
### **Java**
7264

7365
```java
7466
class Solution {
7567
public int[] constructRectangle(int area) {
76-
int sr = (int) Math.sqrt(area);
77-
int l = sr, w = sr;
78-
while (l <= area && w >= 1) {
79-
int s = l * w;
80-
if (s == area) break;
81-
if (s > area) --w;
82-
else ++l;
68+
int w = (int) Math.sqrt(area);
69+
while (area % w != 0) {
70+
--w;
8371
}
84-
return new int[]{l, w};
72+
return new int[]{area / w, w};
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
vector<int> constructRectangle(int area) {
83+
int w = sqrt(1.0 * area);
84+
while (area % w != 0) --w;
85+
return {area / w, w};
8586
}
87+
};
88+
```
89+
90+
### **Go**
91+
92+
```go
93+
func constructRectangle(area int) []int {
94+
w := int(math.Sqrt(float64(area)))
95+
for area%w != 0 {
96+
w--
97+
}
98+
return []int{area / w, w}
8699
}
87100
```
88101

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
vector<int> constructRectangle(int area) {
4+
int w = sqrt(1.0 * area);
5+
while (area % w != 0) --w;
6+
return {area / w, w};
7+
}
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func constructRectangle(area int) []int {
2+
w := int(math.Sqrt(float64(area)))
3+
for area%w != 0 {
4+
w--
5+
}
6+
return []int{area / w, w}
7+
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
class Solution {
22
public int[] constructRectangle(int area) {
3-
int sr = (int) Math.sqrt(area);
4-
int l = sr, w = sr;
5-
while (l <= area && w >= 1) {
6-
int s = l * w;
7-
if (s == area) break;
8-
if (s > area) --w;
9-
else ++l;
3+
int w = (int) Math.sqrt(area);
4+
while (area % w != 0) {
5+
--w;
106
}
11-
return new int[]{l, w};
7+
return new int[]{area / w, w};
128
}
139
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
class Solution:
22
def constructRectangle(self, area: int) -> List[int]:
3-
sr = int(math.sqrt(area))
4-
l = w = sr
5-
while l <= area and w >= 1:
6-
s = l * w
7-
if s == area:
8-
break
9-
if s > area:
10-
w -= 1
11-
else:
12-
l += 1
13-
return [l, w]
3+
w = int(sqrt(area))
4+
while area % w != 0:
5+
w -= 1
6+
return [area // w, w]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /