Add leetcode 20 task solution
This commit is contained in:
parent
dd04d90473
commit
d05290973f
2 changed files with 75 additions and 0 deletions
17
leetcode/dynamic_arrays/1929_concatenation_of_array.py
Normal file
17
leetcode/dynamic_arrays/1929_concatenation_of_array.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Link: https://leetcode.com/problems/concatenation-of-array/
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def getConcatenation(self, nums: List[int]) -> List[int]:
|
||||
# ans = [] * (len(nums) * 2)
|
||||
ans = nums + nums
|
||||
return ans
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
nums = [2, 5, 1, 3, 4, 7]
|
||||
sol = Solution
|
||||
print(nums)
|
||||
print(sol().getConcatenation(nums))
|
||||
print("-" * 60)
|
58
leetcode/stacks/20_valid_parentheses.py
Normal file
58
leetcode/stacks/20_valid_parentheses.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
# Link: https://leetcode.com/problems/valid-parentheses/description/
|
||||
|
||||
|
||||
class Solution:
|
||||
def isValid(self, s: str) -> bool:
|
||||
c = [] # Checker stack
|
||||
m = {
|
||||
")": "(",
|
||||
"}": "{",
|
||||
"]": "[",
|
||||
}
|
||||
cb = m.keys()
|
||||
op = m.values()
|
||||
for char in s:
|
||||
if len(c) == 0:
|
||||
c.append(char)
|
||||
continue
|
||||
if char in op:
|
||||
c.append(char)
|
||||
continue
|
||||
if char in cb:
|
||||
if len(c) > 0 and c[-1] == m[char]:
|
||||
c.pop()
|
||||
continue
|
||||
if len(c) > 0 and c[-1] != m[char]:
|
||||
return False
|
||||
if len(c) == 0:
|
||||
return False
|
||||
print(f"c = {c} (len={len(c)})")
|
||||
return False if len(c) > 0 else True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
tt = "()[]{}"
|
||||
sol = Solution
|
||||
print(f"Initial: {tt}")
|
||||
print(sol().isValid(tt))
|
||||
print("-" * 60)
|
||||
tt = "()"
|
||||
print(f"Initial: {tt}")
|
||||
print(sol().isValid(tt))
|
||||
print("-" * 60)
|
||||
tt = "())"
|
||||
print(f"Initial: {tt}")
|
||||
print(sol().isValid(tt))
|
||||
print("-" * 60)
|
||||
tt = "([])"
|
||||
print(f"Initial: {tt}")
|
||||
print(sol().isValid(tt))
|
||||
print("-" * 60)
|
||||
tt = "("
|
||||
print(f"Initial: {tt}")
|
||||
print(sol().isValid(tt))
|
||||
print("-" * 60)
|
||||
tt = "(])"
|
||||
print(f"Initial: {tt}")
|
||||
print(sol().isValid(tt))
|
||||
print("-" * 60)
|
Loading…
Reference in a new issue