classSolution: defnextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: num1_len = len(nums1) num2_len = len(nums2) res_dict = {nums2[index]: -1for index inrange(num2_len)} res_list = [-1] * num1_len data_stack = list() for num in nums2: while data_stack and num > data_stack[-1]: value = data_stack.pop() res_dict[value] = num data_stack.append(num) for index, value inenumerate(nums1): if value in res_dict: res_list[index] = res_dict[value] return res_list
classSolution: @staticmethod deffirst_smaller(heights: list, direction): assert direction in ['left', 'right']
defr_search(heights, res_list): stack = list() for index, height inenumerate(heights): while stack and height < stack[-1][0]: _, mark_index = stack.pop() res_list[mark_index] = index stack.append([height, index]) return res_list
if direction == 'right': res_list = [len(heights)] * len(heights) right_points = r_search(heights, res_list) return right_points
elif direction == 'left': res_list = [len(heights)] * len(heights) left_points = r_search(heights[::-1], res_list) left_points = [len(heights) - value for value in left_points] return left_points[::-1]
classSolution: defevalRPN(self, tokens: List[str]) -> int: res = None stack = [] for token in tokens: if token in ['+', '-', '*', '/']: after = stack.pop() before = stack.pop() if token == '+': res = before + after elif token == '-': res = before - after elif token == '*': res = before * after else: sign = -1if before * after < 0else1 res = abs(before) // abs(after) * sign stack.append(res) else: stack.append(int(token)) return stack[0]