if len(s) < 2: return 0 maxlen = 0 heads = [None] for i in xrange(1, len(s)): if s[i] == '(': head = None else: j = i - 1 if not heads[i - 1] else heads[i - 1] - 1 if s[j] == ')': head = None elif j == 0 or heads[j - 1] == None: head = j else: head = heads[j - 1] if head != None: maxlen = max(maxlen, i - head + 1) heads.append(head) return maxlen if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile arguments = ["()()"] answers = [4] test(Solution2().longestValidParentheses, arguments, answers) testfile = __file__.replace('.py', '.yaml') run_testfile(testfile, Solution2().longestValidParentheses)
else: h = height[it] it -= 1 area = w * h if area > maxarea: maxarea = area return maxarea class Solution5(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ ih, it = 0, len(height) - 1 maxarea = 0 while ih < it: maxarea = max(maxarea, (it - ih) * min(height[it], height[ih])) if height[ih] < height[it]: ih += 1 else: it -= 1 return maxarea if __name__ == "__main__": from leetcodelib import run_testfile testfile = __file__.replace('.py', '.yaml') run_testfile(testfile, Solution5().maxArea)
while True: if len(head) > len(tail): head, tail = tail, head for word in head: for i in xrange(len(word)): for c in 'abcdefghijklmnopqrstuvwxyz': tmp = word[:i] + c + word[i + 1:] if tmp in tail: return count elif tmp in unreached: willadd.add(tmp) unreached.remove(tmp) if not willadd: return 0 head, willadd = willadd, set([]) count += 1 if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile #arguments = [("hit", "cog", ["hot","dot","dog","lot","log","cog"])] #answers = [5] #test(Solution().ladderLength, arguments, answers) testfile = __file__.replace('.py', '.yaml') #arg_names = "beginWord, endWord, wordList" #arguments = [] #answers = [] #update_testfile(testfile, arg_names, arguments, answers, mode='add') run_testfile(testfile, Solution2().ladderLength, inds=[])
if 2*(len(s)-center)-1 <= len(palin): break return palin class Solution4(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ if len(s) <= 1: return s center, i, j = 0, 0, 1 while len(s)-center > (j-i)/2: h = t = center while t < len(s)-1 and s[t+1] == s[t]: t += 1 center = t+1 while h > 0 and t < len(s)-1 and s[h-1] == s[t+1]: h -= 1 t += 1 if t-h+1 > j-i: i, j = h, t+1 return s[i:j] if __name__ == "__main__": from leetcodelib import run_testfile testfile = __file__.replace('.py', '.yaml') run_testfile(testfile, Solution4().longestPalindrome)
def isvalid(board, row, col, n): c = str(n) if board[row][col] != '.': return False for i in xrange(9): if board[row][i] == c: return False if board[i][col] == c: return False if board[3*int(row/3)+i/3][3*int(col/3)+i%3] == c: return False return True def solve(board): for i, j in product(range(9), repeat=2): if board[i][j] == ".": for n in xrange(1, 10): if isvalid(board, i, j, n): board[i][j] = str(n) if solve(board): return True else: board[i][j] = '.' return False return True solve(board) return board if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile testfile = __file__.replace('.py', '.yaml') run_testfile(testfile, Solution().solveSudoku)
class Solution(object): def testfunction(self, a, b): return [a + b[0], a * b[1]] if __name__ == "__main__": from leetcodelib import run_testfile testfile = "testfile2.yaml" arg_names = "a, b" run_testfile(testfile, Solution().testfunction) run_testfile(testfile, Solution().testfunction, 10) run_testfile(testfile, Solution().testfunction, [2, 3]) run_testfile(testfile, Solution().testfunction, 's')
mp = {} for word in words: if word in mp: mp[word] += 1 else: mp[word] = 1 res = [] total_length = length * len(words) for i in xrange(len(s)-total_length+1): counter = dict(mp) valid = True for j in xrange(i, i+total_length, length): w = s[j:j+length] if w in counter and counter[w] > 0: counter[w] -= 1 else: valid = False break if valid: res.append(i) return res if __name__ == "__main__": from leetcodelib import run_testfile testfile = __file__.replace('.py', '.yaml') run_testfile(testfile, Solution1().findSubstring, [3, 2]) #run_testfile(testfile, Solution2().findSubstring, 3) run_testfile(testfile, Solution3().findSubstring)
for j in xrange(n - 1, -1, -1): col_stat = col0 if j == 0 else matrix[0][j] if matrix[i][0] == 0 or col_stat == 0: matrix[i][j] = 0 return matrix if __name__ == "__main__": from leetcodelib import test, update_testfile, run_testfile, load_testfile m1 = [[1, 2, 3], [4, 5, 6]] a1 = [[x for x in row] for row in m1] m2 = [[0, 1, 2], [4, 5, 6]] a2 = [[0, 0, 0], [0, 5, 6]] m3 = [[1, 2, 3], [4, 0, 6]] a3 = [[1, 0, 3], [0, 0, 0]] m4 = [[0, 1, 2], [4, 0, 6]] a4 = [[0, 0, 0], [0, 0, 0]] arguments = [m1, m2, m3, m4] answers = [a1, a2, a3, a4] #test(Solution()., arguments, answers) arg_names = "matrix" testfile = __file__.replace('.py', '.yaml') #update_testfile(testfile, arg_names, arguments, answers, 'generate') run_testfile(testfile, Solution1().setZeroes)