Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problems: No.1859+ #2129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions solution/1800-1899/1859.Sorting the Sentence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@

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

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

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

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

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。

<!-- tabs:start -->

Expand All @@ -66,11 +68,18 @@
```python
class Solution:
def sortSentence(self, s: str) -> str:
words = s.split()
ans = [None] * len(words)
for w in words:
i = int(w[-1]) - 1
ans[i] = w[:-1]
ws = [(w[:-1], int(w[-1])) for w in s.split()]
ws.sort(key=lambda x: x[1])
return ' '.join(w for w, _ in ws)
```

```python
class Solution:
def sortSentence(self, s: str) -> str:
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return ' '.join(ans)
```

Expand All @@ -81,11 +90,12 @@ class Solution:
```java
class Solution {
public String sortSentence(String s) {
String[] words = s.split(" ");
String[] ans = new String[words.length];
for (String w : words) {
int i = w.charAt(w.length() - 1) - '1';
ans[i] = w.substring(0, w.length() - 1);
String[] ws = s.split(" ");
int n = ws.length;
String[] ans = new String[n];
for (int i = 0; i < n; ++i) {
String w = ws[i];
ans[w.charAt(w.length() - 1) - '1'] = w.substring(0, w.length() - 1);
}
return String.join(" ", ans);
}
Expand All @@ -98,17 +108,18 @@ class Solution {
class Solution {
public:
string sortSentence(string s) {
istringstream is(s);
string t;
vector<string> words;
while (is >> t) words.push_back(t);
vector<string> res(words.size());
for (auto& w : words) {
int i = w[w.size() - 1] - '1';
res[i] = w.substr(0, w.size() - 1);
istringstream iss(s);
string w;
vector<string> ws;
while (iss >> w) {
ws.push_back(w);
}
vector<string> ss(ws.size());
for (auto& w : ws) {
ss[w.back() - '1'] = w.substr(0, w.size() - 1);
}
string ans;
for (auto& w : res) {
for (auto& w : ss) {
ans += w + " ";
}
ans.pop_back();
Expand All @@ -121,11 +132,10 @@ public:

```go
func sortSentence(s string) string {
words := strings.Split(s, " ")
ans := make([]string, len(words))
for _, w := range words {
i := w[len(w)-1] - '1'
ans[i] = w[:len(w)-1]
ws := strings.Split(s, " ")
ans := make([]string, len(ws))
for _, w := range ws {
ans[w[len(w)-1]-'1'] = w[:len(w)-1]
}
return strings.Join(ans, " ")
}
Expand All @@ -139,11 +149,10 @@ func sortSentence(s string) string {
* @return {string}
*/
var sortSentence = function (s) {
const words = s.split(' ');
const ans = new Array(words.length);
for (const w of words) {
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
ans[i] = w.slice(0, w.length - 1);
const ws = s.split(' ');
const ans = Array(ws.length);
for (const w of ws) {
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
}
return ans.join(' ');
};
Expand All @@ -153,11 +162,10 @@ var sortSentence = function (s) {

```ts
function sortSentence(s: string): string {
const words = s.split(' ');
const ans = new Array(words.length);
for (const w of words) {
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
ans[i] = w.slice(0, w.length - 1);
const ws = s.split(' ');
const ans = Array(ws.length);
for (const w of ws) {
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
}
return ans.join(' ');
}
Expand Down
84 changes: 50 additions & 34 deletions solution/1800-1899/1859.Sorting the Sentence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,35 @@

## Solutions

**Solution 1: String Splitting**

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.

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$.

Finally, we join the array $ans$ into a string by spaces, which is the answer.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def sortSentence(self, s: str) -> str:
words = s.split()
ans = [None] * len(words)
for w in words:
i = int(w[-1]) - 1
ans[i] = w[:-1]
ws = [(w[:-1], int(w[-1])) for w in s.split()]
ws.sort(key=lambda x: x[1])
return ' '.join(w for w, _ in ws)
```

```python
class Solution:
def sortSentence(self, s: str) -> str:
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return ' '.join(ans)
```

Expand All @@ -64,11 +81,12 @@ class Solution:
```java
class Solution {
public String sortSentence(String s) {
String[] words = s.split(" ");
String[] ans = new String[words.length];
for (String w : words) {
int i = w.charAt(w.length() - 1) - '1';
ans[i] = w.substring(0, w.length() - 1);
String[] ws = s.split(" ");
int n = ws.length;
String[] ans = new String[n];
for (int i = 0; i < n; ++i) {
String w = ws[i];
ans[w.charAt(w.length() - 1) - '1'] = w.substring(0, w.length() - 1);
}
return String.join(" ", ans);
}
Expand All @@ -81,17 +99,18 @@ class Solution {
class Solution {
public:
string sortSentence(string s) {
istringstream is(s);
string t;
vector<string> words;
while (is >> t) words.push_back(t);
vector<string> res(words.size());
for (auto& w : words) {
int i = w[w.size() - 1] - '1';
res[i] = w.substr(0, w.size() - 1);
istringstream iss(s);
string w;
vector<string> ws;
while (iss >> w) {
ws.push_back(w);
}
vector<string> ss(ws.size());
for (auto& w : ws) {
ss[w.back() - '1'] = w.substr(0, w.size() - 1);
}
string ans;
for (auto& w : res) {
for (auto& w : ss) {
ans += w + " ";
}
ans.pop_back();
Expand All @@ -104,11 +123,10 @@ public:

```go
func sortSentence(s string) string {
words := strings.Split(s, " ")
ans := make([]string, len(words))
for _, w := range words {
i := w[len(w)-1] - '1'
ans[i] = w[:len(w)-1]
ws := strings.Split(s, " ")
ans := make([]string, len(ws))
for _, w := range ws {
ans[w[len(w)-1]-'1'] = w[:len(w)-1]
}
return strings.Join(ans, " ")
}
Expand All @@ -122,11 +140,10 @@ func sortSentence(s string) string {
* @return {string}
*/
var sortSentence = function (s) {
const words = s.split(' ');
const ans = new Array(words.length);
for (const w of words) {
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
ans[i] = w.slice(0, w.length - 1);
const ws = s.split(' ');
const ans = Array(ws.length);
for (const w of ws) {
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
}
return ans.join(' ');
};
Expand All @@ -136,11 +153,10 @@ var sortSentence = function (s) {

```ts
function sortSentence(s: string): string {
const words = s.split(' ');
const ans = new Array(words.length);
for (const w of words) {
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
ans[i] = w.slice(0, w.length - 1);
const ws = s.split(' ');
const ans = Array(ws.length);
for (const w of ws) {
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
}
return ans.join(' ');
}
Expand Down
39 changes: 20 additions & 19 deletions solution/1800-1899/1859.Sorting the Sentence/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
class Solution {
public:
string sortSentence(string s) {
istringstream is(s);
string t;
vector<string> words;
while (is >> t) words.push_back(t);
vector<string> res(words.size());
for (auto& w : words) {
int i = w[w.size() - 1] - '1';
res[i] = w.substr(0, w.size() - 1);
}
string ans;
for (auto& w : res) {
ans += w + " ";
}
ans.pop_back();
return ans;
}
class Solution {
public:
string sortSentence(string s) {
istringstream iss(s);
string w;
vector<string> ws;
while (iss >> w) {
ws.push_back(w);
}
vector<string> ss(ws.size());
for (auto& w : ws) {
ss[w.back() - '1'] = w.substr(0, w.size() - 1);
}
string ans;
for (auto& w : ss) {
ans += w + " ";
}
ans.pop_back();
return ans;
}
};
9 changes: 4 additions & 5 deletions solution/1800-1899/1859.Sorting the Sentence/Solution.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
func sortSentence(s string) string {
words := strings.Split(s, " ")
ans := make([]string, len(words))
for _, w := range words {
i := w[len(w)-1] - '1'
ans[i] = w[:len(w)-1]
ws := strings.Split(s, " ")
ans := make([]string, len(ws))
for _, w := range ws {
ans[w[len(w)-1]-'1'] = w[:len(w)-1]
}
return strings.Join(ans, " ")
}
21 changes: 11 additions & 10 deletions solution/1800-1899/1859.Sorting the Sentence/Solution.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class Solution {
public String sortSentence(String s) {
String[] words = s.split(" ");
String[] ans = new String[words.length];
for (String w : words) {
int i = w.charAt(w.length() - 1) - '1';
ans[i] = w.substring(0, w.length() - 1);
}
return String.join(" ", ans);
}
class Solution {
public String sortSentence(String s) {
String[] ws = s.split(" ");
int n = ws.length;
String[] ans = new String[n];
for (int i = 0; i < n; ++i) {
String w = ws[i];
ans[w.charAt(w.length() - 1) - '1'] = w.substring(0, w.length() - 1);
}
return String.join(" ", ans);
}
}
9 changes: 4 additions & 5 deletions solution/1800-1899/1859.Sorting the Sentence/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* @return {string}
*/
var sortSentence = function (s) {
const words = s.split(' ');
const ans = new Array(words.length);
for (const w of words) {
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
ans[i] = w.slice(0, w.length - 1);
const ws = s.split(' ');
const ans = Array(ws.length);
for (const w of ws) {
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
}
return ans.join(' ');
};
15 changes: 7 additions & 8 deletions solution/1800-1899/1859.Sorting the Sentence/Solution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Solution:
def sortSentence(self, s: str) -> str:
words = s.split()
ans = [None] * len(words)
for w in words:
i = int(w[-1]) - 1
ans[i] = w[:-1]
return ' '.join(ans)
class Solution:
def sortSentence(self, s: str) -> str:
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return " ".join(ans)
9 changes: 4 additions & 5 deletions solution/1800-1899/1859.Sorting the Sentence/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
function sortSentence(s: string): string {
const words = s.split(' ');
const ans = new Array(words.length);
for (const w of words) {
const i = w.charCodeAt(w.length - 1) - '1'.charCodeAt(0);
ans[i] = w.slice(0, w.length - 1);
const ws = s.split(' ');
const ans = Array(ws.length);
for (const w of ws) {
ans[w.charCodeAt(w.length - 1) - '1'.charCodeAt(0)] = w.slice(0, -1);
}
return ans.join(' ');
}
Loading