Skip to content

Commit 064f75e

Browse files
authored
Merge branch 'main' into feat/cli
2 parents a620949 + 8c6983b commit 064f75e

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Problem 0020: Valid Parentheses
2+
3+
## Link
4+
https://leetcode.com/problems/valid-parentheses/
5+
6+
## Intuition
7+
Use a stack to ensure every opening bracket has a matching closing bracket in the correct order.
8+
9+
## Approach
10+
Iterate through the string, pushing opening brackets onto a stack and popping to match each closing bracket; return false if a mismatch or leftover remains.
11+
12+
## Complexity
13+
- Time: O(n)
14+
- Space: O(n)
15+
16+
## Notes
17+
- Handle cases where the string starts with a closing bracket or ends with unmatched openings.
18+
- Use a helper function to map each closing bracket to its corresponding opening bracket.
19+
- Works for all three bracket types: (), {}, [].
20+
- Returns false immediately on any mismatch or if the stack is not empty at the end.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from collections import deque
2+
3+
4+
class Solution:
5+
def isValid(self, s: str) -> bool:
6+
result = deque()
7+
8+
for c in s:
9+
if c in "({[":
10+
result.append(c)
11+
else:
12+
if result:
13+
left = result.pop()
14+
if left != self.getLeft(c):
15+
return False
16+
else:
17+
return False
18+
19+
return not result
20+
21+
def getLeft(self, c):
22+
if c == ")":
23+
return "("
24+
elif c == "}":
25+
return "{"
26+
return "["
27+
28+
29+
if __name__ == "__main__":
30+
s = Solution()
31+
print(s.isValid("()")) # Expected: True
32+
print(s.isValid("()[]{}")) # Expected: True
33+
print(s.isValid("(]")) # Expected: False
34+
print(s.isValid("([])")) # Expected: True
35+
print(s.isValid("(()")) # Expected: False
36+
print(s.isValid("(")) # Expected: False
37+
print(s.isValid("]")) # Expected: False
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Problem 0206: Reverse Linked List
2+
3+
## Link
4+
https://leetcode.com/problems/reverse-linked-list/
5+
6+
## Intuition
7+
Reversing a linked list means making each node point to its previous node instead of the next.
8+
9+
## Approach
10+
Use three pointers—previous, current, and next—to iteratively reverse the direction of each node’s pointer until the end of the list.
11+
12+
## Complexity
13+
- Time: O(n)
14+
- Space: O(1)
15+
16+
## Notes
17+
- Handles empty and single-node lists without extra checks.
18+
- Always update all pointers in each loop to avoid losing access to the rest of the list.
19+
- The final head is the last non-null node processed.
20+
- Common mistake: forgetting to update the next pointer before reversing the link.
21+
22+
NeetCode 150: true
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import *
2+
3+
class ListNode:
4+
def __init__(self, val=0, next=None):
5+
self.val = val
6+
self.next = next
7+
8+
class Solution:
9+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
10+
if not head or not head.next:
11+
return head
12+
13+
pre, cur, nxt = None, head, head.next
14+
while cur:
15+
cur.next = pre
16+
17+
pre = cur
18+
cur = nxt
19+
if nxt:
20+
nxt = nxt.next
21+
22+
return pre
23+
24+
if __name__ == "__main__":
25+
def list_to_linkedlist(lst):
26+
dummy = ListNode()
27+
curr = dummy
28+
for val in lst:
29+
curr.next = ListNode(val)
30+
curr = curr.next
31+
return dummy.next
32+
33+
def linkedlist_to_list(node):
34+
result = []
35+
while node:
36+
result.append(node.val)
37+
node = node.next
38+
return result
39+
40+
s = Solution()
41+
if __name__ == "__main__":
42+
s = Solution()
43+
for test in [[1, 2, 3, 4, 5], [1, 2], []]:
44+
ll = list_to_linkedlist(test)
45+
reversed_ll = s.reverseList(ll)
46+
print(linkedlist_to_list(reversed_ll))

0 commit comments

Comments
 (0)