Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 212_1023.py
Last active October 23, 2017 14:15
[212. Word Search II] #leetcode
class TrieNode:
def __init__(self):
self.next = [None] * 26
self.word = None
class Solution:
def findWords(self, board, words):
"""
:type board: List[List[str]]
:type words: List[str]
@cixuuz
cixuuz / 79_1023.py
Created October 23, 2017 13:31
[79. Word Search] #leetcode
class Solution:
def findWords(self, board, words):
"""
:type board: List[List[str]]
:type words: List[str]
:rtype: List[str]
"""
for i in range(len(board)):
for j in range(len(board[0])):
@cixuuz
cixuuz / 592_1022.py
Created October 22, 2017 21:11
[592. Fraction Addition and Subtraction] #leetcode
import re
class Solution(object):
def fractionAddition(self, expression):
"""
:type expression: str
:rtype: str
"""
signs = list()
if expression[0] != "-":
@cixuuz
cixuuz / 598_1022.py
Created October 22, 2017 20:20
[598. Range Addition II] #leetcode
class Solution(object):
def maxCount(self, m, n, ops):
"""
:type m: int
:type n: int
:type ops: List[List[int]]
:rtype: int
"""
for i, j in ops:
m = min(m, i)
@cixuuz
cixuuz / 518_1019.py
Created October 19, 2017 19:15
[518. Coin Change 2] #leetcode
class Solution(object):
def change(self, amount, coins):
"""
:type amount: int
:type coins: List[int]
:rtype: int
"""
# dp placeholder
dp = [0] * (amount+1)
dp[0] = 1
@cixuuz
cixuuz / 322_1019.py
Created October 19, 2017 18:45
[322. Coin Change] #leetcode
class Solution(object):
# O(n) O(n)
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
# corner case
if not coins:
@cixuuz
cixuuz / 42_1019.py
Created October 19, 2017 15:42
[42. Trapping Rain Water] #leetcode
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
# return placeholder
res = 0
# corner case
@cixuuz
cixuuz / 341_1017.py
Last active October 17, 2017 22:07
[341. Flatten Nested List Iterator] #leetcode
class NestedIterator(object):
def __init__(self, nestedList):
"""
Initialize your data structure here.
:type nestedList: List[NestedInteger]
"""
def gen(nestedList):
for x in nestedList:
if x.isInteger():
@cixuuz
cixuuz / 33_1017.py
Created October 17, 2017 14:30
[33. Search in Rotated Sorted Array] #leetcode
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
# corner case: input is None or empty list
if not nums:
return -1
@cixuuz
cixuuz / 301_1016_n*2^n.py
Created October 16, 2017 21:03
[301. Remove Invalid Parentheses] #leetcode
# BFS
class Solution(object):
def removeInvalidParentheses(self, s):
"""
:type s: str
:rtype: List[str]
"""
level = {s}
while True:
valid = filter(self.is_valid, level)