This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def backspaceCompare(self, S: str, T: str) -> bool: | |
| def get_stack(string: str): | |
| stack = [] | |
| for s in string: | |
| if s != '#': | |
| stack.append(s) | |
| else: | |
| if stack: | |
| stack.pop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def removeDuplicates(self, S: str) -> str: | |
| stack = [] | |
| for char in S: | |
| if (not stack) or (stack[-1]!=char): | |
| stack.append(char) | |
| else: | |
| while stack and stack[-1] == char: | |
| stack.pop() | |
| return ''.join(stack) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def removeDuplicates(self, S: str) -> str: | |
| stack = [] | |
| for char in S: | |
| if (not stack) or (stack[-1]!=char): | |
| stack.append(char) | |
| else: | |
| while stack and stack[-1] == char: | |
| stack.pop() | |
| return ''.join(stack) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def largestRectangleArea(self, heights: List[int]) -> int: | |
| ret = 0 | |
| mono_stack = [] | |
| heights.append(0) | |
| for i, v in enumerate(heights): | |
| while mono_stack and heights[mono_stack[-1]] > v: | |
| height = heights[mono_stack[-1]] | |
| length = i - mono_stack[-1] | |
| ret = max(ret, height * length) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def largestRectangleArea(self, heights: List[int]) -> int: | |
| ret = 0 | |
| mono_stack = [] | |
| heights.append(0) | |
| for i, v in enumerate(heights): | |
| while mono_stack and heights[mono_stack[-1]] > v: | |
| height = heights[mono_stack.pop()] | |
| if mono_stack: | |
| length = i - mono_stack[-1]-1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def trap(self, heights: List[int]) -> int: | |
| stack = [] # a decreasing stack | |
| total = 0 | |
| for i, v in enumerate(heights): | |
| while stack and heights[stack[-1]] < v: | |
| popped_idx = stack.pop() | |
| if not stack: | |
| break | |
| height = min(heights[stack[-1]], v) - heights[popped_idx] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #### example 1 | |
| class Animal(ABC): | |
| @abstractmethod | |
| def move(self): | |
| pass | |
| a = Animal() | |
| # TypeError: Can't instantiate abstract class Animal with abstract methods move | |
| #### example 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from abc import ABC, abstractmethod | |
| ### Example 1 | |
| class Animal(ABC): | |
| @abstractmethod | |
| def move(self): | |
| pass | |
| a = Animal() | |
| # TypeError: Can't instantiate abstract class Animal with abstract methods move | |
| ### Example 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from abc import ABC, abstractmethod | |
| class Animal(ABC): | |
| @abstractmethod | |
| def move(self): | |
| print('Animal moves') | |
| pass | |
| class Cat(Animal): | |
| def move(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from abc import ABC, abstractmethod | |
| class Animal(ABC): | |
| @abstractmethod | |
| def move(self): | |
| print('Animal moves') | |
| class Cat(Animal): | |
| def move(self): | |
| super().move() |