Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- GNN
- Data_Structure
- 데이터
- code
- SQL
- Python
- DS
- sort
- 대학생
- 컴퓨터공학
- CNN
- Mesh
- adaptive remeshing
- coding
- meshgraphnet
- Database
- 데이터베이스
- 컴퓨터사이언스
- 코테
- 자료구조
- 오블완
- 개발자
- mysql
- Leet Code
- LeetCode
- db
- 데베
- CS
- 코딩테스트
- 티스토리챌린지
Archives
- Today
- Total
sy1214ei 님의 블로그
[Leet Code] 58. Length of Last Word - Python 본문
Level : Easy
Topics : String
https://leetcode.com/problems/length-of-last-word/description/
Code
class Solution:
def lengthOfLastWord(self, s: str) -> int:
# 1. str의 뒤 공백을 없애줍니다
# 2. str을 뒤에서 부터 돌면서 ' '이 나온다면 break
# 3. return cnt
s = list(s)
for i in range(len(s)-1, -1, -1):
if s[i] == ' ':
s.pop(i)
else:
break
# last word 개수 세기
cnt = 0
for i in range(len(s)-1, -1, -1):
if s[i] == ' ':
return cnt
else:
cnt += 1
return cnt
# Time Complexity : O(n)
# Space Complexity : O(1)

'[Coding]' 카테고리의 다른 글
| [Leet Code] 21. Merge Two Sorted Lists - Python (7) | 2024.11.29 |
|---|---|
| [Leet Code] 645. Set Mismatch - Python (0) | 2024.11.28 |
| [Leet Code] 747. Largest Number At Least Twice of Others - Python (0) | 2024.11.25 |
| [Leet Code] 628. Maximum Product of Three Numbers - Python (0) | 2024.11.25 |
| [Leet Code] 594. Longest Harmonious Subsequence - Python (0) | 2024.11.25 |