Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b75a9f

Browse files
committed
feat: add solutions to lc problem: No.1451
No.1451.Rearrange Words in a Sentence
1 parent fb4f089 commit 6b75a9f

File tree

6 files changed

+161
-2
lines changed

6 files changed

+161
-2
lines changed

‎solution/1400-1499/1451.Rearrange Words in a Sentence/README.md‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,83 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:排序**
62+
63+
`text` 按空格切分为字符串数组 `words`,并将 `words[0]` 转为小写。然后对 `words` 进行排序(这里需要确保长度相同的情况下,相对顺序保持不变,因此是一种稳定排序)。
64+
65+
排完序后,对 `words[0]` 首字母转为大写。最后将 `words` 所有字符串用空格拼接成一个字符串。
66+
67+
时间复杂度 $O(n\log n)$。
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
6472

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

6775
```python
68-
76+
class Solution:
77+
def arrangeWords(self, text: str) -> str:
78+
words = text.split()
79+
words[0] = words[0].lower()
80+
words.sort(key=len)
81+
words[0] = words[0].title()
82+
return " ".join(words)
6983
```
7084

7185
### **Java**
7286

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

7589
```java
90+
class Solution {
91+
public String arrangeWords(String text) {
92+
String[] words = text.split(" ");
93+
words[0] = words[0].toLowerCase();
94+
Arrays.sort(words, Comparator.comparingInt(String::length));
95+
words[0] = words[0].substring(0, 1).toUpperCase() + words[0].substring(1);
96+
return String.join(" ", words);
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
string arrangeWords(string text) {
107+
vector<string> words;
108+
stringstream ss(text);
109+
string t;
110+
while (ss >> t) {
111+
words.push_back(t);
112+
}
113+
words[0][0] = tolower(words[0][0]);
114+
stable_sort(words.begin(), words.end(), [](const string& a, const string& b) {
115+
return a.size() < b.size();
116+
});
117+
string ans = "";
118+
for (auto& s : words) {
119+
ans += s + " ";
120+
}
121+
ans.pop_back();
122+
ans[0] = toupper(ans[0]);
123+
return ans;
124+
}
125+
};
126+
```
127+
128+
### **Go**
76129
130+
```go
131+
func arrangeWords(text string) string {
132+
words := strings.Split(text, " ")
133+
words[0] = strings.ToLower(words[0])
134+
sort.SliceStable(words, func(i, j int) bool { return len(words[i]) < len(words[j]) })
135+
words[0] = strings.Title(words[0])
136+
return strings.Join(words, " ")
137+
}
77138
```
78139

79140
### **...**

‎solution/1400-1499/1451.Rearrange Words in a Sentence/README_EN.md‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,66 @@ Output is ordered by length and the new first word starts with capital letter.
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def arrangeWords(self, text: str) -> str:
65+
words = text.split()
66+
words[0] = words[0].lower()
67+
words.sort(key=len)
68+
words[0] = words[0].title()
69+
return " ".join(words)
6470
```
6571

6672
### **Java**
6773

6874
```java
75+
class Solution {
76+
public String arrangeWords(String text) {
77+
String[] words = text.split(" ");
78+
words[0] = words[0].toLowerCase();
79+
Arrays.sort(words, Comparator.comparingInt(String::length));
80+
words[0] = words[0].substring(0, 1).toUpperCase() + words[0].substring(1);
81+
return String.join(" ", words);
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
string arrangeWords(string text) {
92+
vector<string> words;
93+
stringstream ss(text);
94+
string t;
95+
while (ss >> t) {
96+
words.push_back(t);
97+
}
98+
words[0][0] = tolower(words[0][0]);
99+
stable_sort(words.begin(), words.end(), [](const string& a, const string& b) {
100+
return a.size() < b.size();
101+
});
102+
string ans = "";
103+
for (auto& s : words) {
104+
ans += s + " ";
105+
}
106+
ans.pop_back();
107+
ans[0] = toupper(ans[0]);
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
### **Go**
69114
115+
```go
116+
func arrangeWords(text string) string {
117+
words := strings.Split(text, " ")
118+
words[0] = strings.ToLower(words[0])
119+
sort.SliceStable(words, func(i, j int) bool { return len(words[i]) < len(words[j]) })
120+
words[0] = strings.Title(words[0])
121+
return strings.Join(words, " ")
122+
}
70123
```
71124

72125
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
string arrangeWords(string text) {
4+
vector<string> words;
5+
stringstream ss(text);
6+
string t;
7+
while (ss >> t) {
8+
words.push_back(t);
9+
}
10+
words[0][0] = tolower(words[0][0]);
11+
stable_sort(words.begin(), words.end(), [](const string& a, const string& b) {
12+
return a.size() < b.size();
13+
});
14+
string ans = "";
15+
for (auto& s : words) {
16+
ans += s + " ";
17+
}
18+
ans.pop_back();
19+
ans[0] = toupper(ans[0]);
20+
return ans;
21+
}
22+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func arrangeWords(text string) string {
2+
words := strings.Split(text, " ")
3+
words[0] = strings.ToLower(words[0])
4+
sort.SliceStable(words, func(i, j int) bool { return len(words[i]) < len(words[j]) })
5+
words[0] = strings.Title(words[0])
6+
return strings.Join(words, " ")
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public String arrangeWords(String text) {
3+
String[] words = text.split(" ");
4+
words[0] = words[0].toLowerCase();
5+
Arrays.sort(words, Comparator.comparingInt(String::length));
6+
words[0] = words[0].substring(0, 1).toUpperCase() + words[0].substring(1);
7+
return String.join(" ", words);
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def arrangeWords(self, text: str) -> str:
3+
words = text.split()
4+
words[0] = words[0].lower()
5+
words.sort(key=len)
6+
words[0] = words[0].title()
7+
return " ".join(words)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /