0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LeetCodeを1日1題解いてみる (day50)

Posted at

50日目に取り組んだ問題はこちら!
本日がこのシリーズのラストになります!また、別の勉強を始める予定です!

151. Reverse Words in a String

問題文

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

僕の回答

class Solution:
    def reverseWords(self, s: str) -> str:
        s_arr = s.split()
        result = " ".join(s_arr[::-1]) 
        return result

より効率の良い回答例

特になし

学んだこと

  • text.split(): 文字列textを空白で区切り、リストを返す
  • " ".join(array): リストの要素を空白で区切り一つの文字列にする

コメント

以前使ったことのあるメソッドのみで実装できたのですぐに完成できた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?