일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- dfs
- 투포인터
- stratascratch
- array
- heap
- 생성형AI
- nlp
- two-pointer
- GenAI
- Python3
- SQL
- codeup
- GenerativeAI
- 자연어처리
- 슬라이딩윈도우
- 알고리즘
- 코드업
- 니트코드
- 릿코드
- 파이썬기초100제
- 리트코드
- gcp
- sql코테
- Python
- 파이썬알고리즘
- LeetCode
- Greedy
- Stack
- slidingwindow
- 파이썬
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])