Skip to content

Commit 6c44bdf

Browse files
committed
feat: add solutions to lc problem: No.0917
No.0917.Reverse Only Letters
1 parent 89de238 commit 6c44bdf

File tree

6 files changed

+278
-124
lines changed

6 files changed

+278
-124
lines changed

solution/0900-0999/0917.Reverse Only Letters/README.md

+111-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定一个字符串&nbsp;<code>S</code>,返回&nbsp;&ldquo;反转后的&rdquo;&nbsp;字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。</p>
9+
<p>给你一个字符串 <code>s</code> ,根据下述规则反转字符串:</p>
10+
11+
<ul>
12+
<li>所有非英文字母保留在原有位置。</li>
13+
<li>所有英文字母(小写或大写)位置反转。</li>
14+
</ul>
15+
16+
<p>返回反转后的 <code>s</code><em> 。</em></p>
1017

1118
<p>&nbsp;</p>
1219

@@ -15,52 +22,142 @@
1522

1623
<p><strong>示例 1:</strong></p>
1724

18-
<pre><strong>输入:</strong>&quot;ab-cd&quot;
19-
<strong>输出:</strong>&quot;dc-ba&quot;
25+
<pre>
26+
<strong>输入:</strong>s = "ab-cd"
27+
<strong>输出:</strong>"dc-ba"
2028
</pre>
2129

30+
<ol>
31+
</ol>
32+
2233
<p><strong>示例 2:</strong></p>
2334

24-
<pre><strong>输入:</strong>&quot;a-bC-dEf-ghIj&quot;
25-
<strong>输出:</strong>&quot;j-Ih-gfE-dCba&quot;
35+
<pre>
36+
<strong>输入:</strong>s = "a-bC-dEf-ghIj"
37+
<strong>输出:</strong>"j-Ih-gfE-dCba"
2638
</pre>
2739

40+
<ol>
41+
</ol>
42+
2843
<p><strong>示例 3:</strong></p>
2944

30-
<pre><strong>输入:</strong>&quot;Test1ng-Leet=code-Q!&quot;
31-
<strong>输出:</strong>&quot;Qedo1ct-eeLg=ntse-T!&quot;
45+
<pre>
46+
<strong>输入:</strong>s = "Test1ng-Leet=code-Q!"
47+
<strong>输出:</strong>"Qedo1ct-eeLg=ntse-T!"
3248
</pre>
3349

3450
<p>&nbsp;</p>
3551

36-
<p><strong>提示</strong></p>
52+
<p><strong>提示</strong></p>
3753

38-
<ol>
39-
<li><code>S.length &lt;= 100</code></li>
40-
<li><code>33 &lt;= S[i].ASCIIcode &lt;= 122</code>&nbsp;</li>
41-
<li><code>S</code> 中不包含&nbsp;<code>\</code> or <code>&quot;</code></li>
42-
</ol>
54+
<ul>
55+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
56+
<li><code>s</code> 仅由 ASCII 值在范围 <code>[33, 122]</code> 的字符组成</li>
57+
<li><code>s</code> 不含 <code>'\"'</code> <code>'\\'</code></li>
58+
</ul>
4359

4460
## 解法
4561

4662
<!-- 这里可写通用的实现逻辑 -->
4763

64+
双指针遍历字符串,交换两个指针指向的字母。
65+
4866
<!-- tabs:start -->
4967

5068
### **Python3**
5169

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

5472
```python
55-
73+
class Solution:
74+
def reverseOnlyLetters(self, s: str) -> str:
75+
s = list(s)
76+
i, j = 0, len(s) - 1
77+
while i < j:
78+
while i < j and not s[i].isalpha():
79+
i += 1
80+
while i < j and not s[j].isalpha():
81+
j -= 1
82+
if i < j:
83+
s[i], s[j] = s[j], s[i]
84+
i, j = i + 1, j - 1
85+
return ''.join(s)
5686
```
5787

5888
### **Java**
5989

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

6292
```java
93+
class Solution {
94+
public String reverseOnlyLetters(String s) {
95+
char[] chars = s.toCharArray();
96+
int i = 0, j = s.length() - 1;
97+
while (i < j) {
98+
while (i < j && !Character.isLetter(chars[i])) {
99+
++i;
100+
}
101+
while (i < j && !Character.isLetter(chars[j])) {
102+
--j;
103+
}
104+
if (i < j) {
105+
char t = chars[i];
106+
chars[i] = chars[j];
107+
chars[j] = t;
108+
++i;
109+
--j;
110+
}
111+
}
112+
return new String(chars);
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
string reverseOnlyLetters(string s) {
123+
int i = 0, j = s.size() - 1;
124+
while (i < j)
125+
{
126+
while (i < j && !isalpha(s[i])) ++i;
127+
while (i < j && !isalpha(s[j])) --j;
128+
if (i < j)
129+
{
130+
swap(s[i], s[j]);
131+
++i;
132+
--j;
133+
}
134+
}
135+
return s;
136+
}
137+
};
138+
```
63139
140+
### **Go**
141+
142+
```go
143+
func reverseOnlyLetters(s string) string {
144+
ans := []rune(s)
145+
i, j := 0, len(s)-1
146+
for i < j {
147+
for i < j && !unicode.IsLetter(ans[i]) {
148+
i++
149+
}
150+
for i < j && !unicode.IsLetter(ans[j]) {
151+
j--
152+
}
153+
if i < j {
154+
ans[i], ans[j] = ans[j], ans[i]
155+
i++
156+
j--
157+
}
158+
}
159+
return string(ans)
160+
}
64161
```
65162

66163
### **...**

solution/0900-0999/0917.Reverse Only Letters/README_EN.md

+101-69
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,34 @@
44

55
## Description
66

7-
<p>Given a string <code>S</code>, return the &quot;reversed&quot; string where all characters that are not a letter&nbsp;stay in the same place, and all letters reverse their positions.</p>
7+
<p>Given a string <code>s</code>, reverse the string according to the following rules:</p>
88

9-
<p>&nbsp;</p>
10-
11-
<div>
12-
13-
<div>
14-
15-
<div>
16-
17-
<ol>
9+
<ul>
10+
<li>All the characters that are not English letters remain in the same position.</li>
11+
<li>All the English letters (lowercase or uppercase) should be reversed.</li>
12+
</ul>
1813

19-
</ol>
20-
21-
</div>
22-
23-
</div>
24-
25-
</div>
26-
27-
<div>
14+
<p>Return <code>s</code><em> after reversing it</em>.</p>
2815

16+
<p>&nbsp;</p>
2917
<p><strong>Example 1:</strong></p>
30-
31-
<pre>
32-
33-
<strong>Input: </strong><span id="example-input-1-1">&quot;ab-cd&quot;</span>
34-
35-
<strong>Output: </strong><span id="example-output-1">&quot;dc-ba&quot;</span>
36-
18+
<pre><strong>Input:</strong> s = "ab-cd"
19+
<strong>Output:</strong> "dc-ba"
20+
</pre><p><strong>Example 2:</strong></p>
21+
<pre><strong>Input:</strong> s = "a-bC-dEf-ghIj"
22+
<strong>Output:</strong> "j-Ih-gfE-dCba"
23+
</pre><p><strong>Example 3:</strong></p>
24+
<pre><strong>Input:</strong> s = "Test1ng-Leet=code-Q!"
25+
<strong>Output:</strong> "Qedo1ct-eeLg=ntse-T!"
3726
</pre>
38-
39-
<div>
40-
41-
<p><strong>Example 2:</strong></p>
42-
43-
<pre>
44-
45-
<strong>Input: </strong><span id="example-input-2-1">&quot;a-bC-dEf-ghIj&quot;</span>
46-
47-
<strong>Output: </strong><span id="example-output-2">&quot;j-Ih-gfE-dCba&quot;</span>
48-
49-
</pre>
50-
51-
<div>
52-
53-
<p><strong>Example 3:</strong></p>
54-
55-
<pre>
56-
57-
<strong>Input: </strong><span id="example-input-3-1">&quot;Test1ng-Leet=code-Q!&quot;</span>
58-
59-
<strong>Output: </strong><span id="example-output-3">&quot;Qedo1ct-eeLg=ntse-T!&quot;</span>
60-
61-
</pre>
62-
6327
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
6429

65-
<div>
66-
67-
<p><strong><span>Note:</span></strong></p>
68-
69-
<ol>
70-
<li><code>S.length &lt;= 100</code></li>
71-
<li><code>33 &lt;= S[i].ASCIIcode &lt;= 122</code>&nbsp;</li>
72-
<li><code>S</code> doesn&#39;t contain <code>\</code> or <code>&quot;</code></li>
73-
</ol>
74-
75-
</div>
76-
77-
</div>
78-
79-
</div>
80-
81-
</div>
30+
<ul>
31+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
32+
<li><code>s</code> consists of characters with ASCII values in the range <code>[33, 122]</code>.</li>
33+
<li><code>s</code> does not contain <code>&#39;\&quot;&#39;</code> or <code>&#39;\\&#39;</code>.</li>
34+
</ul>
8235

8336
## Solutions
8437

@@ -87,13 +40,92 @@
8740
### **Python3**
8841

8942
```python
90-
43+
class Solution:
44+
def reverseOnlyLetters(self, s: str) -> str:
45+
s = list(s)
46+
i, j = 0, len(s) - 1
47+
while i < j:
48+
while i < j and not s[i].isalpha():
49+
i += 1
50+
while i < j and not s[j].isalpha():
51+
j -= 1
52+
if i < j:
53+
s[i], s[j] = s[j], s[i]
54+
i, j = i + 1, j - 1
55+
return ''.join(s)
9156
```
9257

9358
### **Java**
9459

9560
```java
61+
class Solution {
62+
public String reverseOnlyLetters(String s) {
63+
char[] chars = s.toCharArray();
64+
int i = 0, j = s.length() - 1;
65+
while (i < j) {
66+
while (i < j && !Character.isLetter(chars[i])) {
67+
++i;
68+
}
69+
while (i < j && !Character.isLetter(chars[j])) {
70+
--j;
71+
}
72+
if (i < j) {
73+
char t = chars[i];
74+
chars[i] = chars[j];
75+
chars[j] = t;
76+
++i;
77+
--j;
78+
}
79+
}
80+
return new String(chars);
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
string reverseOnlyLetters(string s) {
91+
int i = 0, j = s.size() - 1;
92+
while (i < j)
93+
{
94+
while (i < j && !isalpha(s[i])) ++i;
95+
while (i < j && !isalpha(s[j])) --j;
96+
if (i < j)
97+
{
98+
swap(s[i], s[j]);
99+
++i;
100+
--j;
101+
}
102+
}
103+
return s;
104+
}
105+
};
106+
```
96107
108+
### **Go**
109+
110+
```go
111+
func reverseOnlyLetters(s string) string {
112+
ans := []rune(s)
113+
i, j := 0, len(s)-1
114+
for i < j {
115+
for i < j && !unicode.IsLetter(ans[i]) {
116+
i++
117+
}
118+
for i < j && !unicode.IsLetter(ans[j]) {
119+
j--
120+
}
121+
if i < j {
122+
ans[i], ans[j] = ans[j], ans[i]
123+
i++
124+
j--
125+
}
126+
}
127+
return string(ans)
128+
}
97129
```
98130

99131
### **...**

0 commit comments

Comments
 (0)