Skip to content

Commit 14161d1

Browse files
committed
feat: add solutions to lc problem: No.1324
No.1324.Print Words Vertically
1 parent ea52847 commit 14161d1

File tree

6 files changed

+131
-189
lines changed

6 files changed

+131
-189
lines changed

solution/1300-1399/1324.Print Words Vertically/README.md

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@
5050

5151
## 解法
5252

53-
先将字符串 s 按空格切分,然后直接模拟即可。
54-
5553
<!-- 这里可写通用的实现逻辑 -->
5654

55+
**方法一:模拟**
56+
57+
我们先将字符串 $s$ 按空格分割成单词数组 $words$,然后遍历单词数组,找出最长的单词长度 $n$。
58+
59+
接下来我们从第 $1$ 到第 $n$ 个字符,分别从单词数组中取出对应的字符,如果当前单词长度不足,则用空格补齐,放到一个字符串 $t$ 中。最后将 $t$ 去掉末尾的空格,加入答案数组中即可。
60+
61+
时间复杂度 $O(m)$,空间复杂度 $O(m)$。其中 $m$ 为字符串 $s$ 的长度。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
@@ -64,14 +70,13 @@
6470
class Solution:
6571
def printVertically(self, s: str) -> List[str]:
6672
words = s.split()
67-
m, n = len(words), max(len(word) for word in words)
73+
n = max(len(w) for w in words)
6874
ans = []
6975
for j in range(n):
70-
t = []
71-
for i in range(m):
72-
word = words[i]
73-
t.append(word[j] if j < len(word) else ' ')
74-
ans.append(''.join(t).rstrip())
76+
t = [w[j] if j < len(w) else ' ' for w in words]
77+
while t[-1] == ' ':
78+
t.pop()
79+
ans.append(''.join(t))
7580
return ans
7681
```
7782

@@ -83,34 +88,22 @@ class Solution:
8388
class Solution {
8489
public List<String> printVertically(String s) {
8590
String[] words = s.split(" ");
86-
int m = words.length, n = maxLen(words);
91+
int n = 0;
92+
for (var w : words) {
93+
n = Math.max(n, w.length());
94+
}
8795
List<String> ans = new ArrayList<>();
8896
for (int j = 0; j < n; ++j) {
8997
StringBuilder t = new StringBuilder();
90-
for (int i = 0; i < m; ++i) {
91-
String word = words[i];
92-
t.append(j < word.length() ? word.charAt(j) : ' ');
98+
for (var w : words) {
99+
t.append(j < w.length() ? w.charAt(j) : ' ');
93100
}
94-
ans.add(rstrip(t));
95-
}
96-
return ans;
97-
}
98-
99-
private int maxLen(String[] words) {
100-
int ans = 0;
101-
for (String word : words) {
102-
ans = Math.max(ans, word.length());
103-
}
104-
return ans;
105-
}
106-
107-
private String rstrip(StringBuilder s) {
108-
for (int i = s.length() - 1; i >= 0; --i) {
109-
if (s.charAt(i) != ' ') {
110-
return s.substring(0, i + 1);
101+
while (t.length() > 0 && t.charAt(t.length() - 1) == ' ') {
102+
t.deleteCharAt(t.length() - 1);
111103
}
104+
ans.add(t.toString());
112105
}
113-
return "";
106+
return ans;
114107
}
115108
}
116109
```
@@ -121,26 +114,24 @@ class Solution {
121114
class Solution {
122115
public:
123116
vector<string> printVertically(string s) {
124-
stringstream in(s);
117+
stringstream ss(s);
125118
vector<string> words;
126119
string word;
127120
int n = 0;
128-
while (in >> word) {
129-
words.push_back(word);
130-
n = max(n, (int)word.size());
121+
while (ss >> word) {
122+
words.emplace_back(word);
123+
n = max(n, (int) word.size());
131124
}
132-
int m = words.size();
133125
vector<string> ans;
134126
for (int j = 0; j < n; ++j) {
135-
string t = "";
136-
for (int i = 0; i < m; ++i) {
137-
word = words[i];
138-
t += j < word.size() ? word[j] : ' ';
127+
string t;
128+
for (auto& w : words) {
129+
t += j < w.size() ? w[j] : ' ';
139130
}
140-
while (t.back() == ' ') {
131+
while (t.size() && t.back() == ' ') {
141132
t.pop_back();
142133
}
143-
ans.push_back(t);
134+
ans.emplace_back(t);
144135
}
145136
return ans;
146137
}
@@ -150,39 +141,34 @@ public:
150141
### **Go**
151142
152143
```go
153-
func printVertically(s string) []string {
144+
func printVertically(s string) (ans []string) {
154145
words := strings.Split(s, " ")
155-
m := len(words)
156-
var n int
157-
for _, word := range words {
158-
if n < len(word) {
159-
n = len(word)
160-
}
146+
n := 0
147+
for _, w := range words {
148+
n = max(n, len(w))
161149
}
162-
var ans []string
163150
for j := 0; j < n; j++ {
164-
var t []byte
165-
for i := 0; i < m; i++ {
166-
word := words[i]
167-
if j < len(word) {
168-
t = append(t, word[j])
151+
t := []byte{}
152+
for _, w := range words {
153+
if j < len(w) {
154+
t = append(t, w[j])
169155
} else {
170156
t = append(t, ' ')
171157
}
172158
}
173-
s = string(t)
174-
ans = append(ans, rstrip(s))
159+
for len(t) > 0 && t[len(t)-1] == ' ' {
160+
t = t[:len(t)-1]
161+
}
162+
ans = append(ans, string(t))
175163
}
176-
return ans
164+
return
177165
}
178166
179-
func rstrip(s string) string {
180-
for i := len(s) - 1; i >= 0; i-- {
181-
if s[i] != ' ' {
182-
return s[:i+1]
183-
}
167+
func max(a, b int) int {
168+
if a > b {
169+
return a
184170
}
185-
return s
171+
return b
186172
}
187173
```
188174

solution/1300-1399/1324.Print Words Vertically/README_EN.md

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ Each word would be put on only one column and that in one column there will be o
6363
<p><strong>Constraints:</strong></p>
6464

6565
<ul>
66-
6766
<li><code>1 &lt;= s.length &lt;= 200</code></li>
68-
6967
<li><code>s</code>&nbsp;contains only upper case English letters.</li>
70-
7168
<li>It&#39;s guaranteed that there is only one&nbsp;space between 2 words.</li>
72-
7369
</ul>
7470

7571
## Solutions
@@ -82,14 +78,13 @@ Each word would be put on only one column and that in one column there will be o
8278
class Solution:
8379
def printVertically(self, s: str) -> List[str]:
8480
words = s.split()
85-
m, n = len(words), max(len(word) for word in words)
81+
n = max(len(w) for w in words)
8682
ans = []
8783
for j in range(n):
88-
t = []
89-
for i in range(m):
90-
word = words[i]
91-
t.append(word[j] if j < len(word) else ' ')
92-
ans.append(''.join(t).rstrip())
84+
t = [w[j] if j < len(w) else ' ' for w in words]
85+
while t[-1] == ' ':
86+
t.pop()
87+
ans.append(''.join(t))
9388
return ans
9489
```
9590

@@ -99,34 +94,22 @@ class Solution:
9994
class Solution {
10095
public List<String> printVertically(String s) {
10196
String[] words = s.split(" ");
102-
int m = words.length, n = maxLen(words);
97+
int n = 0;
98+
for (var w : words) {
99+
n = Math.max(n, w.length());
100+
}
103101
List<String> ans = new ArrayList<>();
104102
for (int j = 0; j < n; ++j) {
105103
StringBuilder t = new StringBuilder();
106-
for (int i = 0; i < m; ++i) {
107-
String word = words[i];
108-
t.append(j < word.length() ? word.charAt(j) : ' ');
104+
for (var w : words) {
105+
t.append(j < w.length() ? w.charAt(j) : ' ');
109106
}
110-
ans.add(rstrip(t));
111-
}
112-
return ans;
113-
}
114-
115-
private int maxLen(String[] words) {
116-
int ans = 0;
117-
for (String word : words) {
118-
ans = Math.max(ans, word.length());
119-
}
120-
return ans;
121-
}
122-
123-
private String rstrip(StringBuilder s) {
124-
for (int i = s.length() - 1; i >= 0; --i) {
125-
if (s.charAt(i) != ' ') {
126-
return s.substring(0, i + 1);
107+
while (t.length() > 0 && t.charAt(t.length() - 1) == ' ') {
108+
t.deleteCharAt(t.length() - 1);
127109
}
110+
ans.add(t.toString());
128111
}
129-
return "";
112+
return ans;
130113
}
131114
}
132115
```
@@ -137,26 +120,24 @@ class Solution {
137120
class Solution {
138121
public:
139122
vector<string> printVertically(string s) {
140-
stringstream in(s);
123+
stringstream ss(s);
141124
vector<string> words;
142125
string word;
143126
int n = 0;
144-
while (in >> word) {
145-
words.push_back(word);
146-
n = max(n, (int)word.size());
127+
while (ss >> word) {
128+
words.emplace_back(word);
129+
n = max(n, (int) word.size());
147130
}
148-
int m = words.size();
149131
vector<string> ans;
150132
for (int j = 0; j < n; ++j) {
151-
string t = "";
152-
for (int i = 0; i < m; ++i) {
153-
word = words[i];
154-
t += j < word.size() ? word[j] : ' ';
133+
string t;
134+
for (auto& w : words) {
135+
t += j < w.size() ? w[j] : ' ';
155136
}
156-
while (t.back() == ' ') {
137+
while (t.size() && t.back() == ' ') {
157138
t.pop_back();
158139
}
159-
ans.push_back(t);
140+
ans.emplace_back(t);
160141
}
161142
return ans;
162143
}
@@ -166,39 +147,34 @@ public:
166147
### **Go**
167148
168149
```go
169-
func printVertically(s string) []string {
150+
func printVertically(s string) (ans []string) {
170151
words := strings.Split(s, " ")
171-
m := len(words)
172-
var n int
173-
for _, word := range words {
174-
if n < len(word) {
175-
n = len(word)
176-
}
152+
n := 0
153+
for _, w := range words {
154+
n = max(n, len(w))
177155
}
178-
var ans []string
179156
for j := 0; j < n; j++ {
180-
var t []byte
181-
for i := 0; i < m; i++ {
182-
word := words[i]
183-
if j < len(word) {
184-
t = append(t, word[j])
157+
t := []byte{}
158+
for _, w := range words {
159+
if j < len(w) {
160+
t = append(t, w[j])
185161
} else {
186162
t = append(t, ' ')
187163
}
188164
}
189-
s = string(t)
190-
ans = append(ans, rstrip(s))
165+
for len(t) > 0 && t[len(t)-1] == ' ' {
166+
t = t[:len(t)-1]
167+
}
168+
ans = append(ans, string(t))
191169
}
192-
return ans
170+
return
193171
}
194172
195-
func rstrip(s string) string {
196-
for i := len(s) - 1; i >= 0; i-- {
197-
if s[i] != ' ' {
198-
return s[:i+1]
199-
}
173+
func max(a, b int) int {
174+
if a > b {
175+
return a
200176
}
201-
return s
177+
return b
202178
}
203179
```
204180

solution/1300-1399/1324.Print Words Vertically/Solution.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
class Solution {
22
public:
33
vector<string> printVertically(string s) {
4-
stringstream in(s);
4+
stringstream ss(s);
55
vector<string> words;
66
string word;
77
int n = 0;
8-
while (in >> word) {
9-
words.push_back(word);
8+
while (ss >> word) {
9+
words.emplace_back(word);
1010
n = max(n, (int) word.size());
1111
}
12-
int m = words.size();
1312
vector<string> ans;
1413
for (int j = 0; j < n; ++j) {
15-
string t = "";
16-
for (int i = 0; i < m; ++i) {
17-
word = words[i];
18-
t += j < word.size() ? word[j] : ' ';
14+
string t;
15+
for (auto& w : words) {
16+
t += j < w.size() ? w[j] : ' ';
1917
}
20-
while (t.back() == ' ') {
18+
while (t.size() && t.back() == ' ') {
2119
t.pop_back();
2220
}
23-
ans.push_back(t);
21+
ans.emplace_back(t);
2422
}
2523
return ans;
2624
}

0 commit comments

Comments
 (0)