Skip to content

Instantly share code, notes, and snippets.

class Solution:
def addBinary(self, a: str, b: str) -> str:
return "{0:b}".format(int(a, 2) + int(b, 2))
class Solution:
def addBinary(self, a: str, b: str) -> str:
sum = int(a) + int(b)
sum = [int(x) for x in str(sum)]
carry = 0
for index in range(len(sum)-1, -1, -1):
sum[index] += carry
if sum[index] >= 2:
carry = 1
sum[index] -= 2
class Solution:
def addBinary(self, a: str, b: str) -> str:
sum = int(a) + int(b)
sum = list(str(sum))
hasCarry = False
for index in range(len(sum)-1, -1, -1):
if hasCarry:
if sum[index] == "2":
sum[index] = "1"
hasCarry = True
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head: return True
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head: return True
reverseListNode = ListNode(head.val)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
node = root
nodeCount = 0
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
zeros = [0] * len(matrix[0])
cols = []
rows = []
for row in range(0, len(matrix)):
for col in range(0, len(matrix[row])):
if matrix[row][col] == 0:
cols.append(col)
rows.append(row)
class Solution:
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
d1 = sum(distance[start: destination]) if destination > start else sum(distance[destination: start])
return min(sum(distance) - d1, d1)
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
isHorizontal = coordinates[0][1] == coordinates[1][1] # y equal
isVertical = coordinates[0][0] == coordinates[1][0] # x equal
m = 0
if not isHorizontal and not isVertical:
m = (coordinates[0][1] - coordinates[1][1]) / (coordinates[0][0] - coordinates[1][0])
for index in range(2, len(coordinates)):
if isVertical:
if coordinates[0][0] != coordinates[index][0]:
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
results = []
def dfs(nums, startIndex, currentNums, res):
if len(nums)-1 == startIndex:
# print("-", currentNums, startIndex)
res += [currentNums + [nums[startIndex]] ]
res += [currentNums]
return