diff --git a/141-240525-pass/main.py b/141-240525-pass/main.py new file mode 100644 index 0000000..0c3146c --- /dev/null +++ b/141-240525-pass/main.py @@ -0,0 +1,21 @@ +# Definition for singly-linked list. +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + cnt = 0 + if head == None: return False + while cnt < 1e4+10: + if head.next == None: + return False + head = head.next + cnt += 1 + return True \ No newline at end of file diff --git a/20-240526-pass/main.py b/20-240526-pass/main.py new file mode 100644 index 0000000..2b490ec --- /dev/null +++ b/20-240526-pass/main.py @@ -0,0 +1,42 @@ +class Solution: + def isValid(self, s: str) -> bool: + def rcg_type(ch) -> int: + if ch[0] =='(': + return 1 + if ch[0] ==')': + return 2 + if ch[0] =='{': + return 3 + if ch[0] =='}': + return 4 + if ch[0] =='[': + return 5 + if ch[0] ==']': + return 6 + stack = [] + top_idx = -1 + def is_empty(): + return top_idx == -1 + def set_ele(stack: list, idx: int, ele: int): + if len(stack) <= idx: + stack.append(ele) + else: + stack[idx] = ele + for ch in s: + tp = rcg_type(ch) + if tp % 2 == 1: + top_idx += 1 + set_ele(stack, top_idx, tp) + else: + if is_empty(): return False + top_ele = stack[top_idx] + if tp - top_ele != 1: return False + top_idx -= 1 + if not is_empty(): return False + return True + +sol = Solution() +print(sol.isValid("()")) +print(sol.isValid("(){}[]")) +print(sol.isValid("(]")) +print(sol.isValid("[(])"))