原题
https://leetcode.com/problems/symmetric-tree/
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
思路
递归判断左节点的左边是否等于右节点的右边。
https://leetcode.com/problems/symmetric-tree/
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
递归判断左节点的左边是否等于右节点的右边。
https://leetcode.com/problems/largest-rectangle-in-histogram/
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.
在height后面加上一个0作为右边界,记录高度堆栈stack加上一个-1作为左边界。
https://leetcode.com/problems/maximal-rectangle/
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
大致思路和 84. Largest Rectangle in Histogram 相同,对于每个row判断如果为1则高度叠加,否则高度清零。
https://leetcode.com/problems/unique-binary-search-trees/
Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?
对于n个值,以i为根,则可以将[1,i-1]放入左边,[i+1,n]放入右边,因此有[i-1]*[i-j]种方式。
https://leetcode.com/problems/validate-binary-search-tree/
Given a binary tree, determine if it is a valid binary search tree (BST).
递归判断子节点:
https://leetcode.com/problems/word-search/
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
DFS。四个方向搜寻即可。
https://leetcode.com/problems/merge-intervals/
Given a collection of intervals, merge all overlapping intervals.
先对数组排序,然后遍历数组。设定两个变量记录左边界和右边界。
https://leetcode.com/problems/rotate-image/
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
先转置再x轴颠倒。
https://leetcode.com/problems/jump-game/
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
设定一个变量reachable记录当前能够达到的最远值。然后遍历nums:
https://leetcode.com/problems/permutations/
Given a collection of distinct integers, return all possible permutations.
Backtrack。当temp长度等于所有数字时,添加进结果。否则遍历数字,每个数字添加进temp中,进行下一次迭代。当temp中存在相同数字时跳过。
Update your browser to view this website correctly. Update my browser now