LeetCode 1324 豎直列印單詞

2021-10-02 07:24:36 字數 2590 閱讀 5615

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

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:

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]))

else:

results =

for i in range(maxlen):

temp =

for word in new_list:

result = ''.join(temp)

new_results =

for word in results:

new_word = word.rstrip() # 去掉右側空格

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):

else:

rr =

for x in r:

return rr

1324 豎直列印單詞(模擬)

1.問題描述 給你乙個字串 s。請你按照單詞在 s 中的出現順序將它們全部豎直返回。單詞應該以字串列表的形式返回,必要時用空格補位,但輸出尾部的空格需要刪除 不允許尾隨空格 每個單詞只能放在一列上,每一列中也只能有乙個單詞。示例 1 輸入 s how are you 輸出 hay oro weu 解...

20190907 (leetcode習題)打家劫舍

遞迴思路 從第乙個開始偷,第n個房子處偷得的最大的總錢數要麼是第n 1個房子處偷得的總錢數,要麼是第n 2個房子處偷的總錢數 當前房子存放的錢數。class solution if result index 0 第index個房子處偷得的最大的總錢數要麼是上乙個房子處取得的最大的總錢數,要麼是上上乙...

LeetCode 803 打磚塊 困難

題目 803.打磚塊 有乙個 m x n 的二元網格,其中 1 表示磚塊,0 表示空白。磚塊 穩定 不會掉落 的前提是 一塊磚直接連線到網格的頂部,或者 至少有一塊相鄰 4 個方向之一 磚塊 穩定 不會掉落時 給你乙個陣列 hits 這是需要依次消除磚塊的位置。每當消除 hits i rowi,co...