LeetCode 刷题记录: 101. Symmetric Tree [Python]

原题

https://leetcode.com/problems/symmetric-tree/

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

思路

递归判断左节点的左边是否等于右节点的右边。

LeetCode 刷题记录: 84. Largest Rectangle in Histogram [Python]

原题

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作为左边界。

LeetCode 刷题记录: 85. Maximal Rectangle [Python]

原题

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则高度叠加,否则高度清零。

LeetCode 刷题记录: 96. Unique Binary Search Trees [Python]

原题

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]种方式。

LeetCode 刷题记录: 98. Validate Binary Search Tree [Python]

原题

https://leetcode.com/problems/validate-binary-search-tree/

Given a binary tree, determine if it is a valid binary search tree (BST).

思路

递归判断子节点:

LeetCode 刷题记录: 79. Word Search [Python]

原题

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。四个方向搜寻即可。

LeetCode 刷题记录: 56. Merge Intervals [Python]

原题

https://leetcode.com/problems/merge-intervals/

Given a collection of intervals, merge all overlapping intervals.

思路

先对数组排序,然后遍历数组。设定两个变量记录左边界和右边界。

LeetCode 刷题记录: 48. Rotate Image [Python]

原题

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轴颠倒。

LeetCode 刷题记录: 55. Jump Game [Python]

原题

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:

LeetCode 刷题记录: 46. Permutations [Python]

原题

https://leetcode.com/problems/permutations/

Given a collection of distinct integers, return all possible permutations.

思路

Backtrack。当temp长度等于所有数字时,添加进结果。否则遍历数字,每个数字添加进temp中,进行下一次迭代。当temp中存在相同数字时跳过。

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×