This file contains 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
def rotateImage(a): | |
w = len(a) | |
h = w | |
img =[0]*h | |
for col in range(h): | |
img_row = [0]*w | |
for row in range(w): | |
img_row[h-row-1] = a[row][col] | |
img[col] = img_row |
This file contains 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
def sudoku2(grid): | |
# test row | |
tab = [[False for col in range(9)] for row in range(9)] | |
cols = [[False for col in range(9)] for row in range(9)] | |
rows = [[False for col in range(9)] for row in range(9)] | |
for r in range(9): | |
for c in range(9): | |
if grid[r][c] == ".": | |
continue |
This file contains 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
def decrypt(word, dictionary): | |
lst =[] | |
for c in word: | |
lst.append(dictionary[c]) | |
return ''.join(lst) | |
def isCryptSolution(crypt, solution): | |
dict = {} | |
for r in solution: | |
dict[r[0]] = r[1] |
This file contains 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
# Singly-linked lists are already defined with this interface: | |
# class ListNode(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.next = None | |
# | |
def removeKFromList(l, k): | |
if l == None: | |
return l | |
while l != None and l.value == k: |
This file contains 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
# Singly-linked lists are already defined with this interface: | |
# class ListNode(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.next = None | |
# | |
def addTwoHugeNumbers(a, b): | |
lst_a = {} | |
lst_b = {} | |
i=0 |
This file contains 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
# Singly-linked lists are already defined with this interface: | |
# class ListNode(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.next = None | |
# | |
def mergeTwoLinkedLists(l1, l2): | |
if l1 == None and l2 == None: | |
return None |
This file contains 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(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.next = None | |
# | |
def rearrangeLastN(l, n): | |
if n == 0: | |
return l | |
front, back = l, l |
This file contains 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
def reverseList(head, tail): | |
prev = None | |
while prev != tail: | |
prev, prev.next, head = head, prev, head.next | |
return prev | |
def reverseNodesInKGroups(l, k): | |
if k < 2: | |
return l | |
This file contains 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
# | |
# Binary trees are already defined with this interface: | |
# class Tree(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.left = None | |
# self.right = None | |
def isEqual(left, right): | |
if left == None and right == None: |
This file contains 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
# | |
# Binary trees are already defined with this interface: | |
# class Tree(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.left = None | |
# self.right = None | |
def kthSmallestInBST(t, k): | |
def inorder(r): | |
return inorder(r.left) + [r.value] + inorder(r.right) if r else [] |
OlderNewer