0%

题目

原题页面:https://leetcode.com/problems/unique-binary-search-trees/
本文地址:</unique-binary-search-trees/>
题目类型:Tree, Dynamic Programming
难度评价:Medium
类似题目:(M) Unique Binary Search Trees II

Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?

For example,
Given n = 3, there are a total of 5 unique BST’s.

1
2
3
4
5
1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
阅读全文 »

题目

原题页面:https://leetcode.com/problems/two-sum/
本文地址:</two-sum/>
题目类型:Array, Hash Table
难度评价:Medium
类似题目:(M) 3Sum, (M) 4Sum, (M) Two Sum II - Input array is sorted, (E) Two Sum III - Data structure design

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

阅读全文 »

题目

原题页面:https://leetcode.com/problems/trapping-rain-water/
本文地址:</trapping-rain-water/>
题目类型:Array, Stack, Two Pointers
难度评价:Hard
类似题目:(M) Container With Most Water, (M) Product of Array Except Self

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

rainwatertrap

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. **Thanks Marcos** for contributing this image!

阅读全文 »

题目

原题页面:https://leetcode.com/problems/symmetric-tree/
本文地址:</symmetric-tree/>
题目类型:Tree, Depth-first Search
难度评价:Easy

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

For example, this binary tree is symmetric:

1
2
3
4
5
    1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:

1
2
3
4
5
  1
/ \
2 2
\ \
3 3

Note:
Bonus points if you could solve it both recursively and iteratively.

confused what "{1,#,2,3}" means?

OJ’s Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.

Here’s an example:

1
2
3
4
5
6
7
  1
/ \
2 3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

阅读全文 »

题目

原题页面:https://leetcode.com/problems/sum-root-to-leaf-numbers/
本文地址:</sum-root-to-leaf-numbers/>
题目类型:Tree, Depth-first Search
难度评价:Medium
类似题目:(E) Path Sum, (H) Binary Tree Maximum Path Sum

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

1
2
3
  1
/ \
2 3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

阅读全文 »

题目

原题页面:https://leetcode.com/problems/set-matrix-zeroes/
本文地址:</set-matrix-zeroes/>
题目类型:Array
难度评价:Medium
类似题目:(M) Game of Life

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

阅读全文 »

题目

原题页面:https://leetcode.com/problems/pascals-triangle-ii/
本文地址:</pascals-triangle-ii/>
题目类型:Array
难度评价:Easy
类似题目:(E) Pascal’s Triangle

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

阅读全文 »