linked list and other array problems done
This commit is contained in:
parent
270211e273
commit
57878d980d
@ -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))
|
||||||
11
NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py
Normal file
11
NeetCodeRoadmap/Arrays_and_Hashing/66_Plus_One.py
Normal 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]))
|
||||||
29
NeetCodeRoadmap/LinkedLists/206.Reverse_Linked_List.py
Normal file
29
NeetCodeRoadmap/LinkedLists/206.Reverse_Linked_List.py
Normal 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])
|
||||||
18
NeetCodeRoadmap/Stack/682_Baseball_Game.py
Normal file
18
NeetCodeRoadmap/Stack/682_Baseball_Game.py
Normal 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","+","+"]))
|
||||||
@ -17,4 +17,4 @@ def fizzBuzz(n):
|
|||||||
buzz.append(i)
|
buzz.append(i)
|
||||||
return buzz
|
return buzz
|
||||||
|
|
||||||
print(fizzBuzz(3))
|
print(fizzBuzz(100))
|
||||||
Loading…
Reference in New Issue
Block a user