728x90
class Solution:
def countSegments(self, s: str) -> int:
return len([i for i in s.strip().split(' ') if i])
strip은 안전하게 양쪽 끝의 공백을 제거하는 용도로 넣어준 듯.
if i 라는 조건을 통해 whitespace가 아닐 경우만
새로운 list의 원소로 추가.
str.strip
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed.
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Find All Numbers Disappeared in an Array 파이썬(set.difference) (0) | 2021.04.16 |
---|---|
[LeetCode] Arranging Coins 파이썬 (Binary Search, maximize) (0) | 2021.04.16 |
[LeetCode] Fizz Buzz 파이썬 (여러 솔루션) (0) | 2021.04.16 |
[LeetCode] Longest Palindrome 파이썬 (bitwise &, Counter) (0) | 2021.04.16 |
[LeetCode] Sum of Left Leaves 파이썬 (Tree) (0) | 2021.04.15 |
댓글