Skip to content

Commit 2261df2

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解: * 面试题58 - I. 翻转单词顺序 * 面试题58 - II. 左旋转字符串
1 parent 97a1ef5 commit 2261df2

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [面试题58 - I. 翻转单词顺序](https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/)
2+
3+
## 题目描述
4+
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. ",则输出"student. a am I"。
5+
6+
**示例 1:**
7+
8+
```
9+
输入: "the sky is blue"
10+
输出: "blue is sky the"
11+
```
12+
13+
**示例 2:**
14+
15+
```
16+
输入: "  hello world!  "
17+
输出: "world! hello"
18+
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
19+
```
20+
21+
**示例 3:**
22+
23+
```
24+
输入: "a good   example"
25+
输出: "example good a"
26+
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
27+
```
28+
29+
**说明:**
30+
31+
- 无空格字符构成一个单词。
32+
- 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
33+
- 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
34+
35+
**注意:** 本题与主站 151 题相同:https://leetcode-cn.com/problems/reverse-words-in-a-string/
36+
37+
**注意:** 此题对比原题有改动
38+
39+
## 解法
40+
### Python3
41+
```python
42+
class Solution:
43+
def reverseWords(self, s: str) -> str:
44+
if s is None:
45+
return s
46+
return ' '.join(list(filter(lambda x: x != '', s.strip(' ').split(' ')))[::-1])
47+
```
48+
49+
### Java
50+
```java
51+
class Solution {
52+
public String reverseWords(String s) {
53+
if (s == null || s.length() == 0) {
54+
return s;
55+
}
56+
String[] words = s.split("\\s+");
57+
StringBuilder sb = new StringBuilder();
58+
int len = words.length;
59+
for (int i = len - 1; i >= 0; --i) {
60+
if (!"".equals(words[i])) {
61+
sb.append(words[i]).append(" ");
62+
}
63+
}
64+
s = sb.toString();
65+
len = s.length();
66+
return len > 0 ? s.substring(0, len - 1) : "";
67+
}
68+
}
69+
```
70+
71+
### ...
72+
```
73+
74+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public String reverseWords(String s) {
3+
if (s == null || s.length() == 0) {
4+
return s;
5+
}
6+
String[] words = s.split("\\s+");
7+
StringBuilder sb = new StringBuilder();
8+
int len = words.length;
9+
for (int i = len - 1; i >= 0; --i) {
10+
if (!"".equals(words[i])) {
11+
sb.append(words[i]).append(" ");
12+
}
13+
}
14+
s = sb.toString();
15+
len = s.length();
16+
return len > 0 ? s.substring(0, len - 1) : "";
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def reverseWords(self, s: str) -> str:
3+
if s is None:
4+
return s
5+
return ' '.join(list(filter(lambda x: x != '', s.strip(' ').split(' ')))[::-1])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [面试题58 - II. 左旋转字符串](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)
2+
3+
## 题目描述
4+
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
5+
6+
**示例 1:**
7+
8+
```
9+
输入: s = "abcdefg", k = 2
10+
输出: "cdefgab"
11+
```
12+
13+
**示例 2:**
14+
15+
```
16+
输入: s = "lrloseumgh", k = 6
17+
输出: "umghlrlose"
18+
```
19+
20+
**限制:**
21+
22+
- `1 <= k < s.length <= 10000`
23+
24+
## 解法
25+
### Python3
26+
```python
27+
class Solution:
28+
def reverseLeftWords(self, s: str, n: int) -> str:
29+
n %= len(s)
30+
return s[n:] + s[:n]
31+
```
32+
33+
### Java
34+
```java
35+
class Solution {
36+
public String reverseLeftWords(String s, int n) {
37+
int len = s.length();
38+
n %= len;
39+
return s.substring(n, len) + s.substring(0, n);
40+
}
41+
}
42+
```
43+
44+
### ...
45+
```
46+
47+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public String reverseLeftWords(String s, int n) {
3+
int len = s.length();
4+
n %= len;
5+
return s.substring(n, len) + s.substring(0, n);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def reverseLeftWords(self, s: str, n: int) -> str:
3+
n %= len(s)
4+
return s[n:] + s[:n]

0 commit comments

Comments
 (0)