From 57878d980d3e95f94d56faa663cde0f8ed2e9207 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Thu, 6 Jun 2024 16:00:26 -0700 Subject: [PATCH] linked list and other array problems done --- .../35_Search_Insert_Position.py | 19 ++++++++++++ .../Arrays_and_Hashing/66_Plus_One.py | 11 +++++++ .../LinkedLists/206.Reverse_Linked_List.py | 29 +++++++++++++++++++ NeetCodeRoadmap/Stack/155_Min_Stack.py | 2 +- NeetCodeRoadmap/Stack/682_Baseball_Game.py | 18 ++++++++++++ fizzbuzz.py | 2 +- 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 NeetCodeRoadmap/Arrays_and_Hashing/35_Search_Insert_Position.py create mode 100644 NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py create mode 100644 NeetCodeRoadmap/LinkedLists/206.Reverse_Linked_List.py create mode 100644 NeetCodeRoadmap/Stack/682_Baseball_Game.py diff --git a/NeetCodeRoadmap/Arrays_and_Hashing/35_Search_Insert_Position.py b/NeetCodeRoadmap/Arrays_and_Hashing/35_Search_Insert_Position.py new file mode 100644 index 0000000..e06b021 --- /dev/null +++ b/NeetCodeRoadmap/Arrays_and_Hashing/35_Search_Insert_Position.py @@ -0,0 +1,19 @@ +def searchInsert(nums, target): + """summary_line + + Keyword arguments: + input: int + Return: int + """ + if not nums: + return 0 + ghost = 0 + + for i in range(len(nums)): + if nums[i] == target: + return i + elif nums[i] < target: + ghost = i+1 + return ghost +# print(searchInsert([1,3,5,6], 5)) +print(searchInsert([1,3,5,6], 2)) \ No newline at end of file diff --git a/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py b/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py new file mode 100644 index 0000000..16a8b58 --- /dev/null +++ b/NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py @@ -0,0 +1,11 @@ +def plusOne(digits): + zeros = 0 + if digits[-1] == 9: + digits[-1] = 1 + digits.append(0) + else: + digits[-1] = digits[-1] + 1 + return digits + + +print(plusOne([1,2,3])) \ No newline at end of file diff --git a/NeetCodeRoadmap/LinkedLists/206.Reverse_Linked_List.py b/NeetCodeRoadmap/LinkedLists/206.Reverse_Linked_List.py new file mode 100644 index 0000000..e5055ca --- /dev/null +++ b/NeetCodeRoadmap/LinkedLists/206.Reverse_Linked_List.py @@ -0,0 +1,29 @@ +def reverseList(head): + """ + :type head: ListNode + :rtype: ListNode + """ + prev,current = None,head + + while current: + nxt = current.next + current.next = prev + print(prev) + prev = current + current = nxt + return prev + +def reverseListRecursive(head): + # Check if head exists + if not head: + return None + + newHead = head + # Check if head has more to iterate through + if head.next: + newHead = self.reverseListRecursive(head.next) + head.next.next = head + head.next = None + return newHead + +reverseList([1,2,3,4,5]) \ No newline at end of file diff --git a/NeetCodeRoadmap/Stack/155_Min_Stack.py b/NeetCodeRoadmap/Stack/155_Min_Stack.py index e03adeb..8ef3bd8 100644 --- a/NeetCodeRoadmap/Stack/155_Min_Stack.py +++ b/NeetCodeRoadmap/Stack/155_Min_Stack.py @@ -18,7 +18,7 @@ class MinStack(object): def top(self): if self.stack: # print(self.stack) - return self.stack[-1][0] + return self.stack[-1][0] return 0 def getMin(self): diff --git a/NeetCodeRoadmap/Stack/682_Baseball_Game.py b/NeetCodeRoadmap/Stack/682_Baseball_Game.py new file mode 100644 index 0000000..75c3c4a --- /dev/null +++ b/NeetCodeRoadmap/Stack/682_Baseball_Game.py @@ -0,0 +1,18 @@ +def calPoints(operations): + score = [] + for i in range(len(operations)): + print(score) + if operations[i].startswith("-"): + score.append(int(operations[i])) + if operations[i].isdigit(): + score.append(int(operations[i])) + if operations[i] == "+": + score.append(score[-1] + score[-2]) + if operations[i] == "D": + score.append(score[-1] * 2) + if operations[i] == "C": + score.pop() + return sum(score) + +# print(calPoints(["5","2","C","D","+"])) +print(calPoints(["5","-2","4","C","D","9","+","+"])) \ No newline at end of file diff --git a/fizzbuzz.py b/fizzbuzz.py index 8d808b1..cae6775 100644 --- a/fizzbuzz.py +++ b/fizzbuzz.py @@ -17,4 +17,4 @@ def fizzBuzz(n): buzz.append(i) return buzz -print(fizzBuzz(3)) \ No newline at end of file +print(fizzBuzz(100)) \ No newline at end of file