Post

leetcode(리트코드)-1816 Truncate Sentence(python)

leetcode 1816 - Truncate Sentence 문제입니다.

1. 문제

https://leetcode.com/problems/truncate-sentence/


2. Input , Output


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • k로 들어온 수만큼 어절을 리턴합니다.

5. code

코드설명

python

1
2
3
4
5
6
7
class Solution:
    def truncateSentence(self, s: str, k: int) -> str:
        splitlist = s.split()
        res=""
        for i in range(k):
            res+=splitlist[i]+" "
        return res[:-1]

6. 결과 및 후기, 개선점

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

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