Skip to content

Commit 9871e52

Browse files
committed
feat: add solutions to lc problem: No.1859
No.1859.Sorting the Sentence
1 parent 03dbaa6 commit 9871e52

File tree

8 files changed

+203
-57
lines changed

8 files changed

+203
-57
lines changed

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

+78-19
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:字符串分割**
53+
54+
先将字符串 `s` 按照空格分割,得到字符串数组 `words`
55+
56+
遍历字符串数组 `words`,提取 `words[i]` 中最后一位字符,将其转换为数字,得到 `words[i][0:len(words[i])-1]` 的实际位置。
57+
58+
时间复杂度 $O(n)$,其中 $n$ 是字符串长度。
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**
@@ -58,12 +66,12 @@
5866
```python
5967
class Solution:
6068
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)
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]
74+
return ' '.join(ans)
6775
```
6876

6977
### **Java**
@@ -74,13 +82,52 @@ class Solution:
7482
class Solution {
7583
public String sortSentence(String s) {
7684
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);
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);
89+
}
90+
return String.join(" ", ans);
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
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);
109+
}
110+
string ans;
111+
for (auto& w : res) {
112+
ans += w + " ";
81113
}
82-
return String.join(" ", arr);
114+
ans.pop_back();
115+
return ans;
83116
}
117+
};
118+
```
119+
120+
### **Go**
121+
122+
```go
123+
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]
129+
}
130+
return strings.Join(ans, " ")
84131
}
85132
```
86133

@@ -92,18 +139,30 @@ class Solution {
92139
* @return {string}
93140
*/
94141
var sortSentence = function (s) {
95-
let words = s.split(' ');
96-
let n = words.length;
97-
let res = new Array(n);
98-
for (let word of words) {
99-
let key = word.slice(-1);
100-
let val = word.slice(0, -1);
101-
res[parseInt(key) - 1] = val;
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);
102147
}
103-
return res.join(' ');
148+
return ans.join(' ');
104149
};
105150
```
106151

152+
### **TypeScript**
153+
154+
```ts
155+
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);
161+
}
162+
return ans.join(' ');
163+
}
164+
```
165+
107166
### **...**
108167

109168
```

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

+70-19
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@
6161
```python
6262
class Solution:
6363
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)
64+
words = s.split()
65+
ans = [None] * len(words)
66+
for w in words:
67+
i = int(w[-1]) - 1
68+
ans[i] = w[:-1]
69+
return ' '.join(ans)
7070
```
7171

7272
### **Java**
@@ -75,16 +75,55 @@ class Solution:
7575
class Solution {
7676
public String sortSentence(String s) {
7777
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);
78+
String[] ans = new String[words.length];
79+
for (String w : words) {
80+
int i = w.charAt(w.length() - 1) - '1';
81+
ans[i] = w.substring(0, w.length() - 1);
8282
}
83-
return String.join(" ", arr);
83+
return String.join(" ", ans);
8484
}
8585
}
8686
```
8787

88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
string sortSentence(string s) {
94+
istringstream is(s);
95+
string t;
96+
vector<string> words;
97+
while (is >> t) words.push_back(t);
98+
vector<string> res(words.size());
99+
for (auto& w : words) {
100+
int i = w[w.size() - 1] - '1';
101+
res[i] = w.substr(0, w.size() - 1);
102+
}
103+
string ans;
104+
for (auto& w : res) {
105+
ans += w + " ";
106+
}
107+
ans.pop_back();
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
### **Go**
114+
115+
```go
116+
func sortSentence(s string) string {
117+
words := strings.Split(s, " ")
118+
ans := make([]string, len(words))
119+
for _, w := range words {
120+
i := w[len(w)-1] - '1'
121+
ans[i] = w[:len(w)-1]
122+
}
123+
return strings.Join(ans, " ")
124+
}
125+
```
126+
88127
### **JavaScript**
89128

90129
```js
@@ -93,18 +132,30 @@ class Solution {
93132
* @return {string}
94133
*/
95134
var sortSentence = function (s) {
96-
let words = s.split(' ');
97-
let n = words.length;
98-
let res = new Array(n);
99-
for (let word of words) {
100-
let key = word.slice(-1);
101-
let val = word.slice(0, -1);
102-
res[parseInt(key) - 1] = val;
135+
const words = s.split(' ');
136+
const ans = new Array(words.length);
137+
for (const w of words) {
138+
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
139+
ans[i] = w.slice(0, w.length - 1);
103140
}
104-
return res.join(' ');
141+
return ans.join(' ');
105142
};
106143
```
107144

145+
### **TypeScript**
146+
147+
```ts
148+
function sortSentence(s: string): string {
149+
const words = s.split(' ');
150+
const ans = new Array(words.length);
151+
for (const w of words) {
152+
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
153+
ans[i] = w.slice(0, w.length - 1);
154+
}
155+
return ans.join(' ');
156+
}
157+
```
158+
108159
### **...**
109160

110161
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
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]
7+
}
8+
return strings.Join(ans, " ")
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public String sortSentence(String s) {
33
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);
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);
88
}
9-
return String.join(" ", arr);
9+
return String.join(" ", ans);
1010
}
1111
}

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
* @return {string}
44
*/
55
var sortSentence = function (s) {
6-
let words = s.split(' ');
7-
let n = words.length;
8-
let res = new Array(n);
9-
for (let word of words) {
10-
let key = word.slice(-1);
11-
let val = word.slice(0, -1);
12-
res[parseInt(key) - 1] = val;
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);
1311
}
14-
return res.join(' ');
12+
return ans.join(' ');
1513
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
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)
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
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);
7+
}
8+
return ans.join(' ');
9+
}

0 commit comments

Comments
 (0)