LeetCode 1324. 竖直打印单词

Table of Contents

中文版:

英文版:


中文版:

给你一个字符串 s。请你按照单词在 s 中的出现顺序将它们全部竖直返回。
单词应该以字符串列表的形式返回,必要时用空格补位,但输出尾部的空格需要删除(不允许尾随空格)。
每个单词只能放在一列上,每一列中也只能有一个单词。

示例 1:

输入:s = "HOW ARE YOU"
输出:["HAY","ORO","WEU"]
解释:每个单词都应该竖直打印。 
 "HAY"
 "ORO"
 "WEU"
示例 2:

输入:s = "TO BE OR NOT TO BE"
输出:["TBONTB","OEROOE","   T"]
解释:题目允许使用空格补位,但不允许输出末尾出现空格。
"TBONTB"
"OEROOE"
"   T"
示例 3:

输入:s = "CONTEST IS COMING"
输出:["CIC","OSO","N M","T I","E N","S G","T"]
 

提示:

1 <= s.length <= 200
s 仅含大写英文字母。
题目数据保证两个单词之间只有一个空格。

英文版:

5316. Print Words Vertically

Given a string s. Return all the words vertically in the same order in which they appear in s.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.

Example 1:

Input: s = "HOW ARE YOU"
Output: ["HAY","ORO","WEU"]
Explanation: Each word is printed vertically. 
 "HAY"
 "ORO"
 "WEU"

Example 2:

Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE","   T"]
Explanation: Trailing spaces is not allowed. 
"TBONTB"
"OEROOE"
"   T"

Example 3:

Input: s = "CONTEST IS COMING"
Output: ["CIC","OSO","N M","T I","E N","S G","T"]

Constraints:

  • 1 <= s.length <= 200
  • s contains only upper case English letters.
  • It's guaranteed that there is only one space between 2 words.

My answer:

class Solution:
    def printVertically(self, s: str) -> List[str]:
        word_list = s.split(' ')
       
        
#       找到最长单词的长度
        maxlen = len(word_list[0])
        flag_word = word_list[0] # 标记最长的单词
        index = 0
        for i in range(len(word_list)):
            if len(word_list[i]) > maxlen:
                maxlen = len(word_list[i])
                flag_word = word_list[i]
                index = i
        
#        给最长的单词前边的单词 补充空格在后面
        new_list = []
        for i in range(len(word_list)):
            if word_list[i] != flag_word and i != index:
                new_word = word_list[i] + ' '*(maxlen-len(word_list[i]))
                new_list.append(new_word)

            else:
                new_list.append(word_list[i])
      
        
        
        results = []
        for i in range(maxlen):
            temp = []
            for word in new_list:
                temp.append(word[i])

            result = ''.join(temp)      
            results.append(result)
        
        new_results = []
        for word in results:
            new_word = word.rstrip() # 去掉右侧空格
            new_results.append(new_word)
            
        return new_results

以下是大神代码:

class Solution:
    def printVertically(self, s: str) -> List[str]:
        s = s.split(' ')
        max_l = 0
        for w in s:
            max_l = max(max_l, len(w))
        r = [[] for i in range(max_l)]
        for w in s:
            for i in range(max_l):
                if i < len(w):
                    r[i].append(w[i])
                else:
                    r[i].append(' ')
        rr = []
        for x in r:
            rr.append(''.join(x).rstrip())
        return rr

作者:Amir
链接:https://leetcode-cn.com/circle/article/6Su4LC/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
发布了112 篇原创文章 · 获赞 41 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u011675334/article/details/104045100