leetcode73.Set Matrix Zeroes
题目要求
1 | Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. |
当遇到数组中的值时,即将该值所在的行和列全部改为0。
1 | Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. |
当遇到数组中的值时,即将该值所在的行和列全部改为0。
1 | Given a collection of integers that might contain duplicates, nums, return all possible subsets. |
可以先参考关于SubsetI的这篇博客再看接下来的内容。
这里的区别在于假设输入的数组存在重复值,则找到所有不重复的子集。
1 | Given an array of non-negative integers, you are initially positioned at the first index of the array. |
对于类似体型Jump Game I,请参考这篇博客。
这题相对于I,差别在于已知一定可以到达终点,找到一条最短路径所需要的步数。
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
从树中找到所有符合条件的从根节点到叶节点路径,条件即为树上所有节点值的和等于目标值。
Path Sum I可以参考这篇博客
1 | Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. |
1 | Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. |
1 | The largest rectangle is shown in the shaded area, which has area = 10 unit. |
即找到图中可以组合而成的面积最大的矩形。
这里我首先想到的是leetcode 42 Trapping Rain Water,可以参考我的这篇博客。
因为在42题中,如果我找到了当前矩形左右的最近最高矩形即可。而在本题中,同样是需要找到该矩形左右的最高矩形,但不同的是,一旦我在左右搜寻的过程中遇到了一个比当前矩形矮的矩形,遍历即结束。所以两道题目的要求还是存在区别的。
given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
判断一个树是否是二叉查找树。二叉查找树即满足当前节点左子树的值均小于当前节点的值,右子树的值均大于当前节点的值。