일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- medium
- LeetCode
- slidingwindow
- stratascratch
- Python3
- 파이썬
- gcp
- two-pointer
- codeup
- 알고리즘
- nlp
- 릿코드
- 자연어처리
- 파이썬알고리즘
- 투포인터
- array
- Greedy
- heap
- 코드업
- 생성형AI
- 슬라이딩윈도우
- dfs
- 리트코드
- Python
- 니트코드
- sql코테
- SQL
- 파이썬기초100제
- GenAI
- GenerativeAI
Archives
- Today
- Total
Tech for good
[Leetcode/Greedy] 1903. Largest Odd Number in String 본문
IT/Computer Science
[Leetcode/Greedy] 1903. Largest Odd Number in String
Diana Kang 2025. 5. 24. 22:24class Solution:
def largestOddNumber(self, num: str) -> str:
for i in range(len(num)-1, -1, -1):
if int(num[i]) % 2 == 1:
return ''.join(num[:i+1])
return ""
# 64278
# i = 3
# num = [6, 4, 2, 7, 8]
# num[:i] -> [6, 4, 2]
# num[:i+1] -> [6, 4, 2, 7]
Backward iteration in Python
range function is provided with three arguments(N, -1, -1). Use this method when we need to modify elements or access their indices.
s = "Python"
# - len(s) - 1: Start at the last index
# - -1: Ensures the loop stops after the first character
# - -1: The step value to iterate backwards
for i in range(len(s) - 1, -1, -1):
print(s[i])