leetcode
leetcode copied to clipboard
[Suggestion] Python solution for 155. Min Stack - Readability adjustment
The current solution is:
class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val: int) -> None:
self.stack.append(val)
val = min(val, self.minStack[-1] if self.minStack else val)
self.minStack.append(val)
def pop(self) -> None:
self.stack.pop()
self.minStack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1]
within push, I think the current line is a little bit misleading as it's currently written as
val = min(val, self.minStack[-1] if self.minStack else val)
and writing it as
val = min(val, self.minStack[-1]) if self.minStack else val
is more readable to me.
Correct me if I'm wrong, thanks.
The other version may indeed be slightly more readable to some due to its straightforward structure.
True, your version is more readable.