49
49
50
50
<!-- 这里可写通用的实现逻辑 -->
51
51
52
+ ** 方法一:字符串分割**
53
+
54
+ 先将字符串 ` s ` 按照空格分割,得到字符串数组 ` words ` 。
55
+
56
+ 遍历字符串数组 ` words ` ,提取 ` words[i] ` 中最后一位字符,将其转换为数字,得到 ` words[i][0:len(words[i])-1] ` 的实际位置。
57
+
58
+ 时间复杂度 $O(n)$,其中 $n$ 是字符串长度。
59
+
52
60
<!-- tabs:start -->
53
61
54
62
### ** Python3**
58
66
``` python
59
67
class Solution :
60
68
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 )
67
75
```
68
76
69
77
### ** Java**
@@ -74,13 +82,52 @@ class Solution:
74
82
class Solution {
75
83
public String sortSentence (String s ) {
76
84
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 + " ";
81
113
}
82
- return String . join(" " , arr);
114
+ ans.pop_back();
115
+ return ans;
83
116
}
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, " ")
84
131
}
85
132
```
86
133
@@ -92,18 +139,30 @@ class Solution {
92
139
* @return {string}
93
140
*/
94
141
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 );
102
147
}
103
- return res .join (' ' );
148
+ return ans .join (' ' );
104
149
};
105
150
```
106
151
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
+
107
166
### ** ...**
108
167
109
168
```
0 commit comments