Skip to content

Commit 65bc6f2

Browse files
committed
feat: add solutions to lc problems: No.1859,1860,1861
1 parent 34f0d78 commit 65bc6f2

File tree

12 files changed

+320
-64
lines changed

12 files changed

+320
-64
lines changed

solution/1800-1899/1859.Sorting the Sentence/README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<li><code>s</code> 不包含任何前导或者后缀空格。</li>
4646
</ul>
4747

48-
4948
## 解法
5049

5150
<!-- 这里可写通用的实现逻辑 -->
@@ -57,15 +56,32 @@
5756
<!-- 这里可写当前语言的特殊实现逻辑 -->
5857

5958
```python
60-
59+
class Solution:
60+
def sortSentence(self, s: str) -> str:
61+
words = s.split(' ')
62+
arr = [None] * len(words)
63+
for word in words:
64+
idx = int(word[-1]) - 1
65+
arr[idx] = word[:-1]
66+
return ' '.join(arr)
6167
```
6268

6369
### **Java**
6470

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

6773
```java
68-
74+
class Solution {
75+
public String sortSentence(String s) {
76+
String[] words = s.split(" ");
77+
String[] arr = new String[words.length];
78+
for (String word : words) {
79+
int idx = word.charAt(word.length() - 1) - '0' - 1;
80+
arr[idx] = word.substring(0, word.length() - 1);
81+
}
82+
return String.join(" ", arr);
83+
}
84+
}
6985
```
7086

7187
### **...**

solution/1800-1899/1859.Sorting the Sentence/README_EN.md

+19-20
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,18 @@
66

77
<p>A <strong>sentence</strong> is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.</p>
88

9-
10-
119
<p>A sentence can be <strong>shuffled</strong> by appending the <strong>1-indexed word position</strong> to each word then rearranging the words in the sentence.</p>
1210

13-
14-
1511
<ul>
1612
<li>For example, the sentence <code>&quot;This is a sentence&quot;</code> can be shuffled as <code>&quot;sentence4 a3 is2 This1&quot;</code> or <code>&quot;is2 sentence4 This1 a3&quot;</code>.</li>
1713
</ul>
1814

19-
20-
2115
<p>Given a <strong>shuffled sentence</strong> <code>s</code> containing no more than <code>9</code> words, reconstruct and return <em>the original sentence</em>.</p>
2216

23-
24-
2517
<p>&nbsp;</p>
2618

2719
<p><strong>Example 1:</strong></p>
2820

29-
30-
3121
<pre>
3222

3323
<strong>Input:</strong> s = &quot;is2 sentence4 This1 a3&quot;
@@ -38,12 +28,8 @@
3828

3929
</pre>
4030

41-
42-
4331
<p><strong>Example 2:</strong></p>
4432

45-
46-
4733
<pre>
4834

4935
<strong>Input:</strong> s = &quot;Myself2 Me1 I4 and3&quot;
@@ -54,14 +40,10 @@
5440

5541
</pre>
5642

57-
58-
5943
<p>&nbsp;</p>
6044

6145
<p><strong>Constraints:</strong></p>
6246

63-
64-
6547
<ul>
6648
<li><code>2 &lt;= s.length &lt;= 200</code></li>
6749
<li><code>s</code> consists of lowercase and uppercase English letters, spaces, and digits from <code>1</code> to <code>9</code>.</li>
@@ -77,13 +59,30 @@
7759
### **Python3**
7860

7961
```python
80-
62+
class Solution:
63+
def sortSentence(self, s: str) -> str:
64+
words = s.split(' ')
65+
arr = [None] * len(words)
66+
for word in words:
67+
idx = int(word[-1]) - 1
68+
arr[idx] = word[:-1]
69+
return ' '.join(arr)
8170
```
8271

8372
### **Java**
8473

8574
```java
86-
75+
class Solution {
76+
public String sortSentence(String s) {
77+
String[] words = s.split(" ");
78+
String[] arr = new String[words.length];
79+
for (String word : words) {
80+
int idx = word.charAt(word.length() - 1) - '0' - 1;
81+
arr[idx] = word.substring(0, word.length() - 1);
82+
}
83+
return String.join(" ", arr);
84+
}
85+
}
8786
```
8887

8988
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public String sortSentence(String s) {
3+
String[] words = s.split(" ");
4+
String[] arr = new String[words.length];
5+
for (String word : words) {
6+
int idx = word.charAt(word.length() - 1) - '0' - 1;
7+
arr[idx] = word.substring(0, word.length() - 1);
8+
}
9+
return String.join(" ", arr);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def sortSentence(self, s: str) -> str:
3+
words = s.split(' ')
4+
arr = [None] * len(words)
5+
for word in words:
6+
idx = int(word[-1]) - 1
7+
arr[idx] = word[:-1]
8+
return ' '.join(arr)

solution/1800-1899/1860.Incremental Memory Leak/README.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<li><code>0 &lt;= memory1, memory2 &lt;= 2<sup>31</sup> - 1</code></li>
4646
</ul>
4747

48-
4948
## 解法
5049

5150
<!-- 这里可写通用的实现逻辑 -->
@@ -57,15 +56,47 @@
5756
<!-- 这里可写当前语言的特殊实现逻辑 -->
5857

5958
```python
60-
59+
class Solution:
60+
def memLeak(self, memory1: int, memory2: int) -> List[int]:
61+
i = 1
62+
while 1:
63+
if memory1 >= memory2:
64+
if memory1 < i:
65+
break
66+
memory1 -= i
67+
else:
68+
if memory2 < i:
69+
break
70+
memory2 -= i
71+
i += 1
72+
return [i, memory1, memory2]
6173
```
6274

6375
### **Java**
6476

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

6779
```java
68-
80+
class Solution {
81+
public int[] memLeak(int memory1, int memory2) {
82+
int i = 1;
83+
while (true) {
84+
if (memory1 >= memory2) {
85+
if (memory1 < i) {
86+
break;
87+
}
88+
memory1 -= i;
89+
} else {
90+
if (memory2 < i) {
91+
break;
92+
}
93+
memory2 -= i;
94+
}
95+
++i;
96+
}
97+
return new int[]{i, memory1, memory2};
98+
}
99+
}
69100
```
70101

71102
### **...**

solution/1800-1899/1860.Incremental Memory Leak/README_EN.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,52 @@
4343
<li><code>0 &lt;= memory1, memory2 &lt;= 2<sup>31</sup> - 1</code></li>
4444
</ul>
4545

46-
4746
## Solutions
4847

4948
<!-- tabs:start -->
5049

5150
### **Python3**
5251

5352
```python
54-
53+
class Solution:
54+
def memLeak(self, memory1: int, memory2: int) -> List[int]:
55+
i = 1
56+
while 1:
57+
if memory1 >= memory2:
58+
if memory1 < i:
59+
break
60+
memory1 -= i
61+
else:
62+
if memory2 < i:
63+
break
64+
memory2 -= i
65+
i += 1
66+
return [i, memory1, memory2]
5567
```
5668

5769
### **Java**
5870

5971
```java
60-
72+
class Solution {
73+
public int[] memLeak(int memory1, int memory2) {
74+
int i = 1;
75+
while (true) {
76+
if (memory1 >= memory2) {
77+
if (memory1 < i) {
78+
break;
79+
}
80+
memory1 -= i;
81+
} else {
82+
if (memory2 < i) {
83+
break;
84+
}
85+
memory2 -= i;
86+
}
87+
++i;
88+
}
89+
return new int[]{i, memory1, memory2};
90+
}
91+
}
6192
```
6293

6394
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] memLeak(int memory1, int memory2) {
3+
int i = 1;
4+
while (true) {
5+
if (memory1 >= memory2) {
6+
if (memory1 < i) {
7+
break;
8+
}
9+
memory1 -= i;
10+
} else {
11+
if (memory2 < i) {
12+
break;
13+
}
14+
memory2 -= i;
15+
}
16+
++i;
17+
}
18+
return new int[]{i, memory1, memory2};
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def memLeak(self, memory1: int, memory2: int) -> List[int]:
3+
i = 1
4+
while 1:
5+
if memory1 >= memory2:
6+
if memory1 < i:
7+
break
8+
memory1 -= i
9+
else:
10+
if memory2 < i:
11+
break
12+
memory2 -= i
13+
i += 1
14+
return [i, memory1, memory2]

solution/1800-1899/1861.Rotating the Box/README.md

+55-3
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,79 @@
6969
<li><code>box[i][j]</code> 只可能是 <code>'#'</code> ,<code>'*'</code> 或者 <code>'.'</code> 。</li>
7070
</ul>
7171

72-
7372
## 解法
7473

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

76+
先旋转,再挪动箱子。
77+
7778
<!-- tabs:start -->
7879

7980
### **Python3**
8081

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

8384
```python
84-
85+
class Solution:
86+
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
87+
m, n = len(box), len(box[0])
88+
res = [[None] * m for _ in range(n)]
89+
for i in range(m):
90+
for j in range(n):
91+
res[j][m - i - 1] = box[i][j]
92+
for j in range(m):
93+
q = collections.deque()
94+
for i in range(n - 1, -1, -1):
95+
if res[i][j] == '*':
96+
q.clear()
97+
continue
98+
if res[i][j] == '.':
99+
q.append(i)
100+
else:
101+
if not q:
102+
continue
103+
res[q.popleft()][j] = '#'
104+
res[i][j] = '.'
105+
q.append(i)
106+
return res
85107
```
86108

87109
### **Java**
88110

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

91113
```java
92-
114+
class Solution {
115+
public char[][] rotateTheBox(char[][] box) {
116+
int m = box.length, n = box[0].length;
117+
char[][] res = new char[n][m];
118+
for (int i = 0; i < m; ++i) {
119+
for (int j = 0; j < n; ++j) {
120+
res[j][m - i - 1] = box[i][j];
121+
}
122+
}
123+
for (int j = 0; j < m; ++j) {
124+
Deque<Integer> q = new ArrayDeque<>();
125+
for (int i = n - 1; i >= 0; --i) {
126+
if (res[i][j] == '*') {
127+
q.clear();
128+
continue;
129+
}
130+
if (res[i][j] == '.') {
131+
q.offer(i);
132+
} else {
133+
if (q.isEmpty()) {
134+
continue;
135+
}
136+
res[q.poll()][j] = '#';
137+
res[i][j] = '.';
138+
q.offer(i);
139+
}
140+
}
141+
}
142+
return res;
143+
}
144+
}
93145
```
94146

95147
### **...**

0 commit comments

Comments
 (0)