linked list and other array problems done

This commit is contained in:
Jacob Delgado 2024-06-06 16:00:26 -07:00
parent 270211e273
commit 57878d980d
6 changed files with 79 additions and 2 deletions

View File

@ -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))

View File

@ -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]))

View File

@ -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])

View File

@ -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):

View File

@ -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","+","+"]))

View File

@ -17,4 +17,4 @@ def fizzBuzz(n):
buzz.append(i)
return buzz
print(fizzBuzz(3))
print(fizzBuzz(100))