35.二分查找:leetcode69

1
2
3
4
5
6
7
8
9
10
11

left, right =0, len(array) - 1
while left <= right:
mid = left + (right - left)/2
if array[mid] == target:
//find the target
break or return result
elif array[mid] < target:
left = mid + 1
else:
right = mid - 1

32.N皇后问题:leetcode51

problem

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space respectively.


30.有效括号生成:leetcode22

problem

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.


29.二叉树的最小深度:leetcode111

problem

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.


29.二叉树的最大深度:leetcode104

problem

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.


25.贪心算法:leetcode122

problem

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).


23.分治:leetcode-169

problem

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

1
2
3
4
5
6
Input: [3,2,3]
Output: 3
Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

21.递归和分治:leetcode-50

Implement pow(x, n).

eg. 2^2 = 2^1 * 2^1 = (2^0 * 2^0 * 2) * (2^0 * 2^0 * 2) = (1 * 1 * 2) * (1 * 1 * 2) = 4

eg. 2^3 = 2^1 * 2^1 * 2 = (2^0 * 2^0 * 2) * (2^0 * 2^0 * 2) * 2 = (1 * 1 * 2) * (1 * 1 * 2) * 2 = 8


19.二叉搜索树_最近公共祖先:leetcode235

problem

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”


19.二叉树_最近公共祖先:leetcode236

problem

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”


Your browser is out-of-date!

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

×