234. 回文链表
非常的简单
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head:
return True
res = []
cur = head
while cur:
res.append(cur.val)
cur = cur.next
return res == res[::-1]
236. 二叉树的最近公共祖先
尝试写了一下回溯,还是有问题。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
def dfs(root, p, q, pth, res):
if root.val == p.val or root.val == q.val:
pth.append(root.val)
res.append(pth)
return
if not root:
return
pth.append(root.val)
if root.left:
dfs(root.left, p, q, pth, res)
pth.pop()
if root.right:
dfs(root.right, p, q, pth, res)
res = []
dfs(root, p, q, [], res)
i, j = 0, 0
while res[0][i] != res[1][j]:
i += 1
j += 1
if i >= len(res[0]):
return res[0][i - 1]
if j >= len(res[1]):
return res[1][j - 1]
下面是答案:看了K神的答案,这道题我之前做过,但是做过不代表就会做。还是要多复习,多练习,多写。不然临场做,一定会出错。
class Solution:
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
if not root or root == p or root == q: return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if not left and not right: return # 1.
if not left: return right # 3.
if not right: return left # 4.
return root # 2. if left and right:
class Solution:
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
if not root or root == p or root == q: return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if not left: return right
if not right: return left
return root
这个return root,我不是很理解。
下面是我根据思路写的:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
def dfs(root, p, q):
if not root:
return
if root.val == p.val or root.val == q.val:
return root
left = dfs(root.left, p, q)
right = dfs(root.right, p, q)
if not left and not right:
return
if left and right:
return root
if left and (not right):
return left
if right and (not left):
return right
return dfs(root, p, q)
238. 除自身以外数组的乘积
依稀记得三角对角线,其他的不记得了。
看着上面的评论写的。
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
left_val = 1
left_nums = [left_val]
for i in range(1, len(nums)):
left_val *= nums[i - 1]
left_nums.append(left_val)
right_val = 1
right_nums = [right_val]
for i in range(len(nums) - 2, -1, -1):
right_val *= nums[i + 1]
right_nums.append(right_val)
res = []
n = len(nums)
for i in range(n):
res.append(left_nums[i] * right_nums[n - i - 1])
return res
下面是K神的代码:
class Solution:
def productExceptSelf(self, nums: [int]) -> [int]:
res, p, q = [1], 1, 1
for i in range(len(nums) - 1): # bottom triangle
p *= nums[i]
res.append(p)
for i in range(len(nums) - 1, 0, -1): # top triangle
q *= nums[i]
res[i - 1] *= q
return res
实在是妙。
239. 滑动窗口最大值
这道题是困难题,我写的方法太简单了,超时了:
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
i = 0
n = len(nums)
res = []
while i + k <= n:
res.append(max(nums[i: i + k]))
i += 1
return res
下面是我之前通过的代码,用了heapq来维护
class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
n = len(nums)
q = [(-nums[i], i) for i in range(k)]
heapq.heapify(q)
print(q)
# 当前q的最大值
ans = [-q[0][0]]
for i in range(k, n):
heapq.heappush(q, (-nums[i], i))
while q[0][1] <= i - k:
heapq.heappop(q)
ans.append(-q[0][0])
return ans
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
//这里我们传入了一个比较器,当两者的值相同时,比较下标的位置,下标大的在前面。
PriorityQueue<int[]> queue = new PriorityQueue<>((p1, p2) -> p1[0] != p2[0] ? p2[0] - p1[0] : p2[1] - p1[1]);
//初始化前K的元素到堆中
for (int i = 0; i < k; i++) {
queue.offer(new int[]{nums[i], i});
}
//有n-k+1个
int[] ans = new int[n - k + 1];
//将第一次答案加入数据
ans[0] = queue.peek()[0];
for (int i = k; i < n; i++) {
//将新元素加入优先队列
queue.offer(new int[]{nums[i], i});
//循环判断当前队首是否在窗口中,窗口的左边界为i-k
while (queue.peek()[1] <= i - k) {
queue.poll();
}
//在窗口中直接赋值即可
ans[i - k + 1] = queue.peek()[0];
}
return ans;
}
回来再看吧,实在不会。
240. 搜索二维矩阵 II