Post

leetcode(리트코드)-1859 Sorting the Sentence(python)

leetcode 1859 - Sorting the Sentence 문제입니다.

1. 문제

https://leetcode.com/problems/sorting-the-sentence/


2. Input , Output


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • input으로 문자열이 들어옵니다.
  • 띄어쓰기를 기준으로 각 문자열의 끝에 번호가 주어집니다.
  • 해당 번호 순서대로 문자열을 재배치하여 리턴합니다.

5. code

코드설명

python

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def sortSentence(self, s: str) -> str:
        stsp = s.split(" ")
        resli = [""] * len(stsp)
        res = ""
        for i in range(len(stsp)):    
            resli[int(stsp[i][-1])-1] = stsp[i][:-1]
        for i in range(len(resli)):
            res += resli[i] + " "
        return res[:-1]
                

6. 결과 및 후기, 개선점

필요시 c++로 짜드리겠습니다.

This post is licensed under CC BY 4.0 by the author.