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 164d1c7

Browse files
committed
feat: add solutions to lc problem: No.1324
No.1324.Print Words Vertically
1 parent 5a0f5b2 commit 164d1c7

File tree

6 files changed

+350
-19
lines changed

6 files changed

+350
-19
lines changed

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

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@
4848
<li>题目数据保证两个单词之间只有一个空格。</li>
4949
</ul>
5050

51-
5251
## 解法
5352

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

5657
<!-- tabs:start -->
@@ -60,15 +61,133 @@
6061
<!-- 这里可写当前语言的特殊实现逻辑 -->
6162

6263
```python
63-
64+
class Solution:
65+
def printVertically(self, s: str) -> List[str]:
66+
words = s.split()
67+
m, n = len(words), max(len(word) for word in words)
68+
ans = []
69+
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())
75+
return ans
6476
```
6577

6678
### **Java**
6779

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

7082
```java
83+
class Solution {
84+
public List<String> printVertically(String s) {
85+
String[] words = s.split(" ");
86+
int m = words.length, n = maxLen(words);
87+
List<String> ans = new ArrayList<>();
88+
for (int j = 0; j < n; ++j) {
89+
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) : ' ');
93+
}
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);
111+
}
112+
}
113+
return "";
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<string> printVertically(string s) {
124+
stringstream in(s);
125+
vector<string> words;
126+
string word;
127+
int n = 0;
128+
while (in >> word)
129+
{
130+
words.push_back(word);
131+
n = max(n, (int) word.size());
132+
}
133+
int m = words.size();
134+
vector<string> ans;
135+
for (int j = 0; j < n; ++j)
136+
{
137+
string t = "";
138+
for (int i = 0; i < m; ++i)
139+
{
140+
word = words[i];
141+
t += j < word.size() ? word[j] : ' ';
142+
}
143+
while (t.back() == ' ')
144+
{
145+
t.pop_back();
146+
}
147+
ans.push_back(t);
148+
}
149+
return ans;
150+
}
151+
};
152+
```
71153
154+
### **Go**
155+
156+
```go
157+
func printVertically(s string) []string {
158+
words := strings.Split(s, " ")
159+
m := len(words)
160+
var n int
161+
for _, word := range words {
162+
if n < len(word) {
163+
n = len(word)
164+
}
165+
}
166+
var ans []string
167+
for j := 0; j < n; j++ {
168+
var t []byte
169+
for i := 0; i < m; i++ {
170+
word := words[i]
171+
if j < len(word) {
172+
t = append(t, word[j])
173+
} else {
174+
t = append(t, ' ')
175+
}
176+
}
177+
s = string(t)
178+
ans = append(ans, rstrip(s))
179+
}
180+
return ans
181+
}
182+
183+
func rstrip(s string) string {
184+
for i := len(s) - 1; i >= 0; i-- {
185+
if s[i] != ' ' {
186+
return s[:i+1]
187+
}
188+
}
189+
return s
190+
}
72191
```
73192

74193
### **...**

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

Lines changed: 119 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ Words are returned as a list of strings, complete with&nbsp;spaces when is neces
1010

1111
Each word would be put on only one column and that in one column there will be only one word.</p>
1212

13-
14-
1513
<p>&nbsp;</p>
1614

1715
<p><strong>Example 1:</strong></p>
1816

19-
20-
2117
<pre>
2218

2319
<strong>Input:</strong> s = &quot;HOW ARE YOU&quot;
@@ -34,12 +30,8 @@ Each word would be put on only one column and that in one column there will be o
3430

3531
</pre>
3632

37-
38-
3933
<p><strong>Example 2:</strong></p>
4034

41-
42-
4335
<pre>
4436

4537
<strong>Input:</strong> s = &quot;TO BE OR NOT TO BE&quot;
@@ -56,12 +48,8 @@ Each word would be put on only one column and that in one column there will be o
5648

5749
</pre>
5850

59-
60-
6151
<p><strong>Example 3:</strong></p>
6252

63-
64-
6553
<pre>
6654

6755
<strong>Input:</strong> s = &quot;CONTEST IS COMING&quot;
@@ -70,14 +58,10 @@ Each word would be put on only one column and that in one column there will be o
7058

7159
</pre>
7260

73-
74-
7561
<p>&nbsp;</p>
7662

7763
<p><strong>Constraints:</strong></p>
7864

79-
80-
8165
<ul>
8266
<li><code>1 &lt;= s.length &lt;= 200</code></li>
8367
<li><code>s</code>&nbsp;contains only upper case English letters.</li>
@@ -91,13 +75,131 @@ Each word would be put on only one column and that in one column there will be o
9175
### **Python3**
9276

9377
```python
94-
78+
class Solution:
79+
def printVertically(self, s: str) -> List[str]:
80+
words = s.split()
81+
m, n = len(words), max(len(word) for word in words)
82+
ans = []
83+
for j in range(n):
84+
t = []
85+
for i in range(m):
86+
word = words[i]
87+
t.append(word[j] if j < len(word) else ' ')
88+
ans.append(''.join(t).rstrip())
89+
return ans
9590
```
9691

9792
### **Java**
9893

9994
```java
95+
class Solution {
96+
public List<String> printVertically(String s) {
97+
String[] words = s.split(" ");
98+
int m = words.length, n = maxLen(words);
99+
List<String> ans = new ArrayList<>();
100+
for (int j = 0; j < n; ++j) {
101+
StringBuilder t = new StringBuilder();
102+
for (int i = 0; i < m; ++i) {
103+
String word = words[i];
104+
t.append(j < word.length() ? word.charAt(j) : ' ');
105+
}
106+
ans.add(rstrip(t));
107+
}
108+
return ans;
109+
}
110+
111+
private int maxLen(String[] words) {
112+
int ans = 0;
113+
for (String word : words) {
114+
ans = Math.max(ans, word.length());
115+
}
116+
return ans;
117+
}
118+
119+
private String rstrip(StringBuilder s) {
120+
for (int i = s.length() - 1; i >= 0; --i) {
121+
if (s.charAt(i) != ' ') {
122+
return s.substring(0, i + 1);
123+
}
124+
}
125+
return "";
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
vector<string> printVertically(string s) {
136+
stringstream in(s);
137+
vector<string> words;
138+
string word;
139+
int n = 0;
140+
while (in >> word)
141+
{
142+
words.push_back(word);
143+
n = max(n, (int) word.size());
144+
}
145+
int m = words.size();
146+
vector<string> ans;
147+
for (int j = 0; j < n; ++j)
148+
{
149+
string t = "";
150+
for (int i = 0; i < m; ++i)
151+
{
152+
word = words[i];
153+
t += j < word.size() ? word[j] : ' ';
154+
}
155+
while (t.back() == ' ')
156+
{
157+
t.pop_back();
158+
}
159+
ans.push_back(t);
160+
}
161+
return ans;
162+
}
163+
};
164+
```
100165
166+
### **Go**
167+
168+
```go
169+
func printVertically(s string) []string {
170+
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+
}
177+
}
178+
var ans []string
179+
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])
185+
} else {
186+
t = append(t, ' ')
187+
}
188+
}
189+
s = string(t)
190+
ans = append(ans, rstrip(s))
191+
}
192+
return ans
193+
}
194+
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+
}
200+
}
201+
return s
202+
}
101203
```
102204

103205
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<string> printVertically(string s) {
4+
stringstream in(s);
5+
vector<string> words;
6+
string word;
7+
int n = 0;
8+
while (in >> word)
9+
{
10+
words.push_back(word);
11+
n = max(n, (int) word.size());
12+
}
13+
int m = words.size();
14+
vector<string> ans;
15+
for (int j = 0; j < n; ++j)
16+
{
17+
string t = "";
18+
for (int i = 0; i < m; ++i)
19+
{
20+
word = words[i];
21+
t += j < word.size() ? word[j] : ' ';
22+
}
23+
while (t.back() == ' ')
24+
{
25+
t.pop_back();
26+
}
27+
ans.push_back(t);
28+
}
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
(0)

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