IT (117) 썸네일형 리스트형 [Leetcode] 2000. Reverse Prefix of Word https://leetcode.com/problems/reverse-prefix-of-wordclass Solution: def reversePrefix(self, word: str, ch: str) -> str: index = word.find(ch) if index == -1: return word return word[index::-1] + word[index+1:] [Leetcode] 404. Sum of Left Leaves https://leetcode.com/problems/sum-of-left-leaves # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int: result = 0 if not root: return result if root.left: if not root.left.left and not root.left.right: result += .. [Leetcode] 3114. Latest Time You Can Obtain After Replacing Characters https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters class Solution: def findLatestTime(self, s: str) -> str: result = '' for i, c in enumerate(s): if c == '?' and i == 0: if s[1] [Leetcode] 3110. Score of a String https://leetcode.com/problems/score-of-a-string class Solution: def scoreOfString(self, s: str) -> int: sum = 0 for i in range(len(s)-1): sum += abs(ord(s[i]) - ord(s[i+1])) return sum [Leetcode] 2073. Time Needed to Buy Tickets https://leetcode.com/problems/time-needed-to-buy-tickets/ class Solution: def timeRequiredToBuy(self, tickets: List[int], k: int) -> int: seconds = 0 while tickets[k] > 0: for i in range(len(tickets)): if tickets[i] > 0: tickets[i] -= 1 seconds += 1 if tickets[k] == 0: break return seconds [Leetcode] 1700. Number of Students Unable to Eat Lunch https://leetcode.com/problems/number-of-students-unable-to-eat-lunch class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: count = 0 while students: if students[0] == sandwiches[0]: students.pop(0) sandwiches.pop(0) count = 0 else: students.append(students.pop(0)) count += 1 if count == len(students): return count return 0 [Leetcode] 678. Valid Parenthesis String https://leetcode.com/problems/valid-parenthesis-string class Solution: def checkValidString(self, s: str) -> bool: stack = [] star = [] for i, c in enumerate(s): if c == '(': stack.append(i) elif c == '*': star.append(i) elif c == ')': if stack: stack.pop() elif star: star.pop() else: return False while stack and star: if stack[-1] > star[-1]: return False stack.pop() star.pop() if stack: return.. [Leetcode] 1544. Make The String Great https://leetcode.com/problems/make-the-string-great class Solution: def makeGood(self, s: str) -> str: stack = [] for c in s: if stack and abs(ord(c) - ord(stack[-1])) == 32 : stack.pop() else: stack.append(c) return ''.join(stack) 이전 1 2 3 4 ··· 15 다음