Skip to content

Commit be10b43

Browse files
authored
feat: add solutions to lc problems: No.1859+ (doocs#2129)
1 parent 7920f29 commit be10b43

File tree

19 files changed

+693
-237
lines changed

19 files changed

+693
-237
lines changed

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

+45-37
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@
5151

5252
**方法一:字符串分割**
5353

54-
先将字符串 `s` 按照空格分割,得到字符串数组 `words`
54+
我们先将字符串 $s$ 按照空格分割,得到字符串数组 $words$。然后,我们创建一个长度为 $|words|$ 的字符串数组 $ans$,用于存放答案
5555

56-
遍历字符串数组 `words`,提取 `words[i]` 中最后一位字符,将其转换为数字,得到 `words[i][0:len(words[i])-1]` 的实际位置
56+
接下来,遍历字符串数组 $words$ 中的每个字符串 $w$,找到 $w$ 的最后一个字符表示的位置 $i$,然后将 $w$ 的前 $|w|-1$ 个字符作为新的字符串 $w'$,将 $w'$ 放在数组 $ans$ 的第 $i$ 个位置
5757

58-
时间复杂度 $O(n)$,其中 $n$ 是字符串长度。
58+
最后,将数组 $ans$ 按照空格连接成字符串,即为答案。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
5961

6062
<!-- tabs:start -->
6163

@@ -66,11 +68,18 @@
6668
```python
6769
class Solution:
6870
def sortSentence(self, s: str) -> str:
69-
words = s.split()
70-
ans = [None] * len(words)
71-
for w in words:
72-
i = int(w[-1]) - 1
73-
ans[i] = w[:-1]
71+
ws = [(w[:-1], int(w[-1])) for w in s.split()]
72+
ws.sort(key=lambda x: x[1])
73+
return ' '.join(w for w, _ in ws)
74+
```
75+
76+
```python
77+
class Solution:
78+
def sortSentence(self, s: str) -> str:
79+
ws = s.split()
80+
ans = [None] * len(ws)
81+
for w in ws:
82+
ans[int(w[-1]) - 1] = w[:-1]
7483
return ' '.join(ans)
7584
```
7685

@@ -81,11 +90,12 @@ class Solution:
8190
```java
8291
class Solution {
8392
public String sortSentence(String s) {
84-
String[] words = s.split(" ");
85-
String[] ans = new String[words.length];
86-
for (String w : words) {
87-
int i = w.charAt(w.length() - 1) - '1';
88-
ans[i] = w.substring(0, w.length() - 1);
93+
String[] ws = s.split(" ");
94+
int n = ws.length;
95+
String[] ans = new String[n];
96+
for (int i = 0; i < n; ++i) {
97+
String w = ws[i];
98+
ans[w.charAt(w.length() - 1) - '1'] = w.substring(0, w.length() - 1);
8999
}
90100
return String.join(" ", ans);
91101
}
@@ -98,17 +108,18 @@ class Solution {
98108
class Solution {
99109
public:
100110
string sortSentence(string s) {
101-
istringstream is(s);
102-
string t;
103-
vector<string> words;
104-
while (is >> t) words.push_back(t);
105-
vector<string> res(words.size());
106-
for (auto& w : words) {
107-
int i = w[w.size() - 1] - '1';
108-
res[i] = w.substr(0, w.size() - 1);
111+
istringstream iss(s);
112+
string w;
113+
vector<string> ws;
114+
while (iss >> w) {
115+
ws.push_back(w);
116+
}
117+
vector<string> ss(ws.size());
118+
for (auto& w : ws) {
119+
ss[w.back() - '1'] = w.substr(0, w.size() - 1);
109120
}
110121
string ans;
111-
for (auto& w : res) {
122+
for (auto& w : ss) {
112123
ans += w + " ";
113124
}
114125
ans.pop_back();
@@ -121,11 +132,10 @@ public:
121132
122133
```go
123134
func sortSentence(s string) string {
124-
words := strings.Split(s, " ")
125-
ans := make([]string, len(words))
126-
for _, w := range words {
127-
i := w[len(w)-1] - '1'
128-
ans[i] = w[:len(w)-1]
135+
ws := strings.Split(s, " ")
136+
ans := make([]string, len(ws))
137+
for _, w := range ws {
138+
ans[w[len(w)-1]-'1'] = w[:len(w)-1]
129139
}
130140
return strings.Join(ans, " ")
131141
}
@@ -139,11 +149,10 @@ func sortSentence(s string) string {
139149
* @return {string}
140150
*/
141151
var sortSentence = function (s) {
142-
const words = s.split(' ');
143-
const ans = new Array(words.length);
144-
for (const w of words) {
145-
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
146-
ans[i] = w.slice(0, w.length - 1);
152+
const ws = s.split(' ');
153+
const ans = Array(ws.length);
154+
for (const w of ws) {
155+
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
147156
}
148157
return ans.join(' ');
149158
};
@@ -153,11 +162,10 @@ var sortSentence = function (s) {
153162

154163
```ts
155164
function sortSentence(s: string): string {
156-
const words = s.split(' ');
157-
const ans = new Array(words.length);
158-
for (const w of words) {
159-
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
160-
ans[i] = w.slice(0, w.length - 1);
165+
const ws = s.split(' ');
166+
const ans = Array(ws.length);
167+
for (const w of ws) {
168+
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
161169
}
162170
return ans.join(' ');
163171
}

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

+50-34
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,35 @@
4444

4545
## Solutions
4646

47+
**Solution 1: String Splitting**
48+
49+
First, we split the string $s$ by spaces to get the string array $words$. Then, we create a string array $ans$ of length $|words|$ to store the answer.
50+
51+
Next, we iterate over each string $w$ in the string array $words$, find the position $i$ represented by the last character of $w$, then take the first $|w|-1$ characters of $w$ as the new string $w'$, and place $w'$ in the $i$th position of the array $ans$.
52+
53+
Finally, we join the array $ans$ into a string by spaces, which is the answer.
54+
55+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
56+
4757
<!-- tabs:start -->
4858

4959
### **Python3**
5060

5161
```python
5262
class Solution:
5363
def sortSentence(self, s: str) -> str:
54-
words = s.split()
55-
ans = [None] * len(words)
56-
for w in words:
57-
i = int(w[-1]) - 1
58-
ans[i] = w[:-1]
64+
ws = [(w[:-1], int(w[-1])) for w in s.split()]
65+
ws.sort(key=lambda x: x[1])
66+
return ' '.join(w for w, _ in ws)
67+
```
68+
69+
```python
70+
class Solution:
71+
def sortSentence(self, s: str) -> str:
72+
ws = s.split()
73+
ans = [None] * len(ws)
74+
for w in ws:
75+
ans[int(w[-1]) - 1] = w[:-1]
5976
return ' '.join(ans)
6077
```
6178

@@ -64,11 +81,12 @@ class Solution:
6481
```java
6582
class Solution {
6683
public String sortSentence(String s) {
67-
String[] words = s.split(" ");
68-
String[] ans = new String[words.length];
69-
for (String w : words) {
70-
int i = w.charAt(w.length() - 1) - '1';
71-
ans[i] = w.substring(0, w.length() - 1);
84+
String[] ws = s.split(" ");
85+
int n = ws.length;
86+
String[] ans = new String[n];
87+
for (int i = 0; i < n; ++i) {
88+
String w = ws[i];
89+
ans[w.charAt(w.length() - 1) - '1'] = w.substring(0, w.length() - 1);
7290
}
7391
return String.join(" ", ans);
7492
}
@@ -81,17 +99,18 @@ class Solution {
8199
class Solution {
82100
public:
83101
string sortSentence(string s) {
84-
istringstream is(s);
85-
string t;
86-
vector<string> words;
87-
while (is >> t) words.push_back(t);
88-
vector<string> res(words.size());
89-
for (auto& w : words) {
90-
int i = w[w.size() - 1] - '1';
91-
res[i] = w.substr(0, w.size() - 1);
102+
istringstream iss(s);
103+
string w;
104+
vector<string> ws;
105+
while (iss >> w) {
106+
ws.push_back(w);
107+
}
108+
vector<string> ss(ws.size());
109+
for (auto& w : ws) {
110+
ss[w.back() - '1'] = w.substr(0, w.size() - 1);
92111
}
93112
string ans;
94-
for (auto& w : res) {
113+
for (auto& w : ss) {
95114
ans += w + " ";
96115
}
97116
ans.pop_back();
@@ -104,11 +123,10 @@ public:
104123
105124
```go
106125
func sortSentence(s string) string {
107-
words := strings.Split(s, " ")
108-
ans := make([]string, len(words))
109-
for _, w := range words {
110-
i := w[len(w)-1] - '1'
111-
ans[i] = w[:len(w)-1]
126+
ws := strings.Split(s, " ")
127+
ans := make([]string, len(ws))
128+
for _, w := range ws {
129+
ans[w[len(w)-1]-'1'] = w[:len(w)-1]
112130
}
113131
return strings.Join(ans, " ")
114132
}
@@ -122,11 +140,10 @@ func sortSentence(s string) string {
122140
* @return {string}
123141
*/
124142
var sortSentence = function (s) {
125-
const words = s.split(' ');
126-
const ans = new Array(words.length);
127-
for (const w of words) {
128-
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
129-
ans[i] = w.slice(0, w.length - 1);
143+
const ws = s.split(' ');
144+
const ans = Array(ws.length);
145+
for (const w of ws) {
146+
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
130147
}
131148
return ans.join(' ');
132149
};
@@ -136,11 +153,10 @@ var sortSentence = function (s) {
136153

137154
```ts
138155
function sortSentence(s: string): string {
139-
const words = s.split(' ');
140-
const ans = new Array(words.length);
141-
for (const w of words) {
142-
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
143-
ans[i] = w.slice(0, w.length - 1);
156+
const ws = s.split(' ');
157+
const ans = Array(ws.length);
158+
for (const w of ws) {
159+
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
144160
}
145161
return ans.join(' ');
146162
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
class Solution {
2-
public:
3-
string sortSentence(string s) {
4-
istringstream is(s);
5-
string t;
6-
vector<string> words;
7-
while (is >> t) words.push_back(t);
8-
vector<string> res(words.size());
9-
for (auto& w : words) {
10-
int i = w[w.size() - 1] - '1';
11-
res[i] = w.substr(0, w.size() - 1);
12-
}
13-
string ans;
14-
for (auto& w : res) {
15-
ans += w + " ";
16-
}
17-
ans.pop_back();
18-
return ans;
19-
}
1+
class Solution {
2+
public:
3+
string sortSentence(string s) {
4+
istringstream iss(s);
5+
string w;
6+
vector<string> ws;
7+
while (iss >> w) {
8+
ws.push_back(w);
9+
}
10+
vector<string> ss(ws.size());
11+
for (auto& w : ws) {
12+
ss[w.back() - '1'] = w.substr(0, w.size() - 1);
13+
}
14+
string ans;
15+
for (auto& w : ss) {
16+
ans += w + " ";
17+
}
18+
ans.pop_back();
19+
return ans;
20+
}
2021
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
func sortSentence(s string) string {
2-
words := strings.Split(s, " ")
3-
ans := make([]string, len(words))
4-
for _, w := range words {
5-
i := w[len(w)-1] - '1'
6-
ans[i] = w[:len(w)-1]
2+
ws := strings.Split(s, " ")
3+
ans := make([]string, len(ws))
4+
for _, w := range ws {
5+
ans[w[len(w)-1]-'1'] = w[:len(w)-1]
76
}
87
return strings.Join(ans, " ")
98
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
class Solution {
2-
public String sortSentence(String s) {
3-
String[] words = s.split(" ");
4-
String[] ans = new String[words.length];
5-
for (String w : words) {
6-
int i = w.charAt(w.length() - 1) - '1';
7-
ans[i] = w.substring(0, w.length() - 1);
8-
}
9-
return String.join(" ", ans);
10-
}
1+
class Solution {
2+
public String sortSentence(String s) {
3+
String[] ws = s.split(" ");
4+
int n = ws.length;
5+
String[] ans = new String[n];
6+
for (int i = 0; i < n; ++i) {
7+
String w = ws[i];
8+
ans[w.charAt(w.length() - 1) - '1'] = w.substring(0, w.length() - 1);
9+
}
10+
return String.join(" ", ans);
11+
}
1112
}

solution/1800-1899/1859.Sorting the Sentence/Solution.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* @return {string}
44
*/
55
var sortSentence = function (s) {
6-
const words = s.split(' ');
7-
const ans = new Array(words.length);
8-
for (const w of words) {
9-
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
10-
ans[i] = w.slice(0, w.length - 1);
6+
const ws = s.split(' ');
7+
const ans = Array(ws.length);
8+
for (const w of ws) {
9+
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
1110
}
1211
return ans.join(' ');
1312
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
class Solution:
2-
def sortSentence(self, s: str) -> str:
3-
words = s.split()
4-
ans = [None] * len(words)
5-
for w in words:
6-
i = int(w[-1]) - 1
7-
ans[i] = w[:-1]
8-
return ' '.join(ans)
1+
class Solution:
2+
def sortSentence(self, s: str) -> str:
3+
ws = s.split()
4+
ans = [None] * len(ws)
5+
for w in ws:
6+
ans[int(w[-1]) - 1] = w[:-1]
7+
return " ".join(ans)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
function sortSentence(s: string): string {
2-
const words = s.split(' ');
3-
const ans = new Array(words.length);
4-
for (const w of words) {
5-
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
6-
ans[i] = w.slice(0, w.length - 1);
2+
const ws = s.split(' ');
3+
const ans = Array(ws.length);
4+
for (const w of ws) {
5+
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
76
}
87
return ans.join(' ');
98
}

0 commit comments

Comments
 (0)