70. 二叉树的层次遍历 II
给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)
样例
例1:
输入:
{1,2,3}
输出:
[[2,3],[1]]
解释:
1
/
2 3
例2:
输入:
{3,9,20,#,#,15,7}
输出:
[
[15,7],
[9,20],
[3]
]
解释:
3
/ \
9 20
/ \
15 7
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: A tree
@return: buttom-up level order a list of lists of integer
"""
def levelOrderBottom(self, root):
# write your code here
if root is None:
return []
queue = [root]
result = []
while(len(queue)>0):
chlird = []
result.append([n.val for n in queue])
for i in queue:
node = i
if node.left:
chlird.append(node.left)
if node.right:
chlird.append(node.right)
queue = chlird
return list(reversed(result))
822. 相反的顺序存储
给出一个链表,并将链表的值以倒序存储到数组中。
样例
样例1
输入: 1 -> 2 -> 3 -> null
输出: [3,2,1]
样例2
输入: 4 -> 2 -> 1 -> null
输出: [1,2,4]
注意事项
您不能改变原始链表的结构。
ListNode 有两个成员变量:ListNode.val 和 ListNode.next
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: the given linked list
@return: the array that store the values in reverse order
"""
def reverseStore(self, head):
# write your code here
ans = []
self.iteration(head,ans)
return ans
def iteration(self,head,ans):
if head is None:
return None
else:
self.iteration(head.next,ans)
ans.append(head.val)
388. 第k个排列
给定 n 和 k,求n的全排列中字典序第k个排列.
样例
样例 1:
输入: n = 3, k = 4
输出: “231”
解释:
n = 3时, 全排列如下:
“123”, “132”, “213”, “231”, “312”, “321”
样例 2:
输入: n = 1, k = 1
输出: “1”
class Solution:
"""
@param n: n
@param k: the k th permutation
@return: return the k-th permutation
"""
def getPermutation(self, n, k):
# write your code here
B = []
for i in range(1,n+1):
B.append(str(i))
A = ''.join(B)
nums = A
def search(nums,lists,index):
if(len(nums)==len(lists)):
self.result.append(lists)
if len(self.result) == k:
return
for i in range(len(nums)):
if self.flag[i]==1:
continue
if(i!=0 and nums[i]==nums[i-1] and self.flag[i-1]==0):
continue
elif self.flag[i]!=1:
self.flag[i]=1
search(nums,lists+nums[i],0)
self.flag[i]=0
self.result=[]
self.flag=[]
for i in range(len(nums)):
self.flag.append(0)
search(nums,"",0)
print(self.result)
return self.result[k-1]
153. 数字组合 II
给出一组候选数字©和目标数字(T),找出C中所有的组合,使组合中数字的和为T。C中每个数字在每个组合中只能使用一次。
样例
给出一个例子,候选数字集合为[10,1,6,7,2,1,5] 和目标数字 8 ,
解集为:[[1,7],[1,2,5],[2,6],[1,1,6]]
注意事项
所有的数字(包括目标数字)均为正整数。
元素组合(a1, a2, … , ak)必须是非降序(ie, a1 ≤ a2 ≤ … ≤ ak)。
解集不能包含重复的组合。
class Solution:
"""
@param num: Given the candidate numbers
@param target: Given the target number
@return: All the combinations that sum to target
"""
def combinationSum2(self, num, target):
# write your code here
num = sorted(num)
def search(num,lists,index):
for i in range(1,len(num)):
if(len(lists)==i):
if sum(lists)==target:
if lists not in self.result:
self.result.append(lists)
return
for i in range(index,len(num)):
if self.flag[i]==1:
continue
if(i!=0 and num[i]==num[i-1] and self.flag[i-1]==0):
continue
if self.flag[i]!=1 and sum(lists+[num[i]])<=target:
self.flag[i]=1
search(num,lists+[num[i]],i+1)
self.flag[i]=0
self.result=[]
self.flag=[]
for i in range(len(num)):
self.flag.append(0)
search(num,[],0)
return self.result
179. 更新二进制位
给出两个32位的整数N和M,以及两个二进制位的位置i和j。写一个方法来使得N中的第i到j位等于M(M会是N中从第i为开始到第j位的子串)
样例
样例1:
输入: N=(10000000000)2 M=(10101)2 i=2 j=6
输出: N=(10001010100)2
样例 2:
输入: N=(10000000000)2 M=(11111)2 i=2 j=6
输出: N=(10001111100)2
class Solution:
"""
@param n: An integer
@param m: An integer
@param i: A bit position
@param j: A bit position
@return: An integer
"""
def updateBits(self, n, m, i, j):
# write your code here
n &= 0xFFFFFFFF
for k in range(j - i + 1):
n &= ~(1 << k + i)
n |= (m << i) & 0xFFFFFFFF
if n & (1 << 31):
n = -((~n + 1) & 0xFFFFFFFF)
return n
1199. 完美的数
我们定义完美数是一个正 整数,它等于除其自身之外的所有 正 约数的总和。
现在,给定一个整数 n,写一个函数,当它是一个完美的数字时返回true,而当它不是时,返回false。
样例
例子:
输入: 28
输出: True
解释: 28 = 1 + 2 + 4 + 7 + 14
注意事项
输入数字n不会超过100,000,000
class Solution:
"""
@param num: an integer
@return: returns true when it is a perfect number and false when it is not
"""
def checkPerfectNumber(self, num):
# write your code here
if num == 1:
return False
sum = 1
for i in range(2, math.floor(math.sqrt(num)) + 1):
if num % i == 0:
if i * i == num:
sum += i
else:
sum += i + num // i
if sum > num:
return False
return num == sum