Was able to do this from one list where, get the compliment and check from i+1 index forward within the list to see if the value exists in there or not
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i, num in enumerate(nums):
compliment = target - num
if compliment in nums[i+1:]:
idx = nums.index(compliment, i+1)
out = [i, idx]
return out
return None
I created a dictionary with matching brackets and created a stack using collections deque
iterated through the string of brackets and if it was opening I added it to the stack, then if it is closing, I checked the last bracket added to the stack and made sure the closing bracket matched
If it matched, I popped from the deque since it is LIFO
If it didn’t match, then I returned False
And once the stack is empty at the end of the loop I returned True
from collections import deque
class Solution:
def isValid(self, s: str) -> bool:
stack = deque()
opening_brackets = {'[' : ']', '{' : '}', '(' : ')'}
if len(s) == 1:
return False
for bracket in s:
#check if it is an opening bracket
if bracket in opening_brackets.keys():
#add it to the stack
stack.append(bracket)
else:
#check if the top of the stack is the closer to it
if not stack or opening_brackets[stack[-1]] != bracket:
return False
stack.pop()
if stack:
return False
return True
Used list comprehension to remove the element from within the list itself. The [:] specifies that it is does in place
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
#iterate through nums
nums[:] = [num for num in nums if num != val]
return len(nums)