LeetCode 1324. 竖直打印单词C#
原题链接
中等
作者:
hpstory
,
2022-08-15 23:33:34
,
所有人可见
,
阅读 100
C# 代码
public class Solution {
public IList<string> PrintVertically(string s) {
string[] words = s.Split(" ");
int maxLength = 0;
foreach (string word in words) maxLength = Math.Max(maxLength, word.Length);
string[] result = new string[maxLength];
for (int i = 0; i < maxLength; i++){
string newWord = string.Empty;
foreach (string word in words){
// 长度不足用空格补上
if (word.Length <= i) newWord += " ";
else newWord += word[i];
}
// 删除右侧多余空格
newWord = newWord.TrimEnd();
result[i] = newWord;
}
return result.ToList();
}
}