56/leetcode

/75/75-01矩阵.py
# Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.

# To flip an image horizontally means that each row of the image will be reversed.

# For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].

# To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0.

# For example, inverting [0, 1, 1] results in [1, 0, 0].

# Example 1:

# Input: [[1,1,0],[1,0,1],[0,0,0]]
# Output: [[1,0,0],[0,1,0],[1,1,1]]
# Explanation: First reverse each row, then invert it.
# Example 2:

# Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
# Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
# Explanation: First reverse each row, then invert it.

# Note:
# 1 <= A.length = A[0].length <= 20
# 0 <= A[i][j] <= 1

class Solution(object):
def flipAndInvertImage(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
for i in range(len(A)):
A[i] = A[i][::-1]
for j in range(len(A[0])):
if A[i][j] == 0:
A[i][j] = 1
else:
A[i][j] = 0
return A

/113/113_pathSum3.py
# Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

# For example:
# Given binary tree [3,9,20,null,null,15,7],
# 3
# / \
# 9 20
# / \
# 15 7
# return its level order traversal as:
# [
# [3],
# [9,20],
# [15,7]
# ]

# 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 levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
res = []
if not root:
return res
queue = [root]
while queue:
cur = []
next = []
for node in queue:
cur.append(node.val)
if node.left:
next.append(node.left)
if node.right:
next.append(node.right)
res.append(cur)
queue = next
return res

/97/97-interleavingString.py
# Given two strings s and t, determine if they are isomorphic.

# Two strings are isomorphic if the characters in s can be replaced to get t.

# All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

# For example,
# Given "egg", "add", return true.

# Given "foo", "bar", return false.

# Given "paper", "title", return true.

# Note:
# You may assume both s and t have the same length.

class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
s_dict = {}
t_dict = {}
for i in range(len(s)):
if s[i] in s_dict and s_dict[s[i]] != t[i]:
return False
if t[i] in t_dict and t_dict[t[i]] != s[i]:
return False
s_dict[s[i]] = t[i]
t_dict[t[i]] = s[i]
return True

/115/115_distinctSubsequences.py
# Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

# Example 1:

# Input: "babad"
# Output: "bab"
# Note: "aba" is also a valid answer.
# Example 2:

# Input: "cbbd"
# Output: "bb"
# Example 3:

# Input: "a"
# Output: "a"
# Example 4:

# Input: "ac"
# Output: "a"
# Note:

# There is at least one palindromic substring in the string
# You cannot modify the input string.

class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
s_list = list(s)
if len(s) == 1:
return s
if len(s) == 2:
if s[0] ==