101. Symmetric Tree
判断一个二叉树是否对称
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3我的代码:利用深度遍历得到每层按顺序的值列表,再进行判断
# 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 isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
def dfs(node,lv,cache):
if node:
cache.setdefault(lv,[])
cache[lv].append(node.val)
dfs(node.left,lv+1,cache)
dfs(node.right,lv+1,cache)
if not node:
cache.setdefault(lv,[])
cache[lv].append(None)
return cache
temp = dfs(root,0,{})
for i in temp:
if temp[i][:] != temp[i][::-1]:
return False
return True
大神的代码:
def isSymmetric(self, root):
def isSym(L,R):
if not L and not R: return True
if L and R and L.val == R.val:
return isSym(L.left, R.right) and isSym(L.right, R.left)
return False
return isSym(root, root)
简化版:
def isSym(L,R):
if L and R and L.val == R.val:
return isSym(L.left, R.right) and isSym(L.right, R.left)
return L == R
另外有个DFS版:仔细一想这种按层来的用DFS更好
def isSymmetric(self, root):
queue = [root]
while queue:
values = [i.val if i else None for i in queue]
if values != values[::-1]: return False
queue = [child for i in queue if i for child in (i.left, i.right)]
return True
198 . House Robber
给出一个列表,不能连续取两个值,求取出值的最大值
这种动态规划没啥头绪,看了discuss后才懂
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
last,now = 0,0
for i in nums:
last,now = now,max(last+i,now)
return now
118 . Pascal's Triangle
给一个数n,返回n层的Pascal's Triangle
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]我的代码:看了discuss,这个应该是比较好的版本了
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows == 0:
return []
ans = [[1]]
for i in range(numRows-1):
temp =list(map(lambda x,y:x+y,[0]+ans[i],ans[i]+[0]))
ans.append(temp)
return ans
263 . Ugly Number
定义一个数如果只能被2,3,5约除,则这个数为Ugly number(1是Ugly number)
我的代码:想啥写啥了
class Solution(object):
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
if num <= 0:
return False
while num != 1:
if num % 2 == 0:
num /= 2
continue
if num % 3 == 0:
num /= 3
continue
if num % 5 == 0:
num /= 5
continue
return False
return True
大神的代码:如果加上判断正负,在输入负数时可以不用计算那么多
for p in 2, 3, 5:
while num % p == 0 < num:
num /= p
return num == 1
超神代码:
def isUgly(self, num):
return num > 0 == 30**32 % num #这里按顺序来判断,先判断num >0,再判断0 == 30**32 % num
66 . Plus One
将一个数拆分为单个数字组为一个列表,返回这个数+1后的列表
我的代码:
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
if digits[-1] != 9:
return digits[:-1]+[digits[-1]+1]
num = 0
for i in range(len(digits)):
num = num *10 +digits[i]
num += 1
return [int(j) for j in str(num)]
我认为开始做一个尾数是否为9的判断有助于加快算法,随后我意识到可以做一个递归的算法,可能速度更快
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
if digits == []:
return [1]
if digits[-1] != 9:
return digits[:-1]+[digits[-1]+1]
else:
return self.plusOne(digits[:-1])+[0]
确实更快了
198
.
House Robber