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 result(o): | |
return eval(o) | |
def get_all_options(num, tail, target): | |
if len(num) == 0: | |
return {} | |
if len(num) == 1: | |
return {int(num): [ num ] } | |
last_dict = get_all_options(num[:-1], num[-1] + tail, target) | |
curr_dict = {} |
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
class Solution(object): | |
def frequencySort(self, s): | |
""" | |
:type s: str | |
:rtype: str | |
""" | |
freq_d = {} | |
for c in s: | |
freq_d[c] = freq_d.get(c, 0) + 1 | |
c_in_order = sorted(freq_d.keys(), key=lambda k: -freq_d[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
class Solution(object): | |
def addString(self, prefix, s, k, chars): | |
print 'AddString({},{},{},{})'.format(prefix, s, k, chars) | |
if len(prefix) == len(s): | |
return prefix | |
for (c,n) in chars.items(): | |
# print prefix[-k+1:] | |
if c not in prefix[-k+1:] and n > 0: | |
chars[c] -= 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
class Solution(object): | |
def lengthLongestPath(self, input): | |
""" | |
:type input: str | |
:rtype: int | |
""" | |
inputs = input.split('\n') | |
file_parts = [] | |
max_file_length = 0 | |
last_pos = -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
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution(object): | |
def compareChild(self, root, child): | |
passRoot, notPassRoot = self.longestPassRootOrNot(child) |
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
class Solution(object): | |
ds_count = {1: 0, 2: 0, 3: 1, 4:5} | |
def get_ds_count(self, N): | |
if N in self.ds_count: | |
return self.ds_count[N] | |
self.ds_count[N] = self.get_ds_count(N-1)*2 + (N-2)*(N-1)/2 | |
print 'F({}) = {} + {} '.format(N, self.get_ds_count(N-1)*2, (N-2)*(N-1)/2) | |
return self.ds_count[N] | |
def numberOfArithmeticSlices(self, A): | |
""" |
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
class Solution(object): | |
def nextPermutation(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: void Do not return anything, modify nums in-place instead. | |
""" | |
if len(nums) == 1: return | |
for i in range(len(nums)-2, -1, -1): | |
if nums[i] < nums[i+1]: | |
m = min((n, -(j+i+1)) for j,n in enumerate(nums[i+1:]) if n > nums[i]) |
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
class Solution(object): | |
def anagramMappings(self, A, B): | |
""" | |
:type A: List[int] | |
:type B: List[int] | |
:rtype: List[int] | |
""" | |
char_set = {c: i for i,c in enumerate(B)} | |
return [char_set[c] for c in A] |
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 a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution(object): | |
LCA = None | |
def lowestCommonAncestor(self, root, p, q): |
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 a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution(object): | |
def lowestCommonAncestor(self, root, p, q): | |
""" |