This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def addBinary(self, a: str, b: str) -> str: | |
| return "{0:b}".format(int(a, 2) + int(b, 2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |