Two Pointers
判断两个变量的条件一定要用小于
Tow Pointers
通常要使用两个变量 lo
, hi
或者 left
, right
来从两个方向遍历数组,循环的结束条件一定是 lo < hi
,不要用 lo != hi
否则如果 lo
和 hi
均为偶数,那么同时操作会导致 hi
小于 lo
以至于循环溢出
void reverse(int[] A, int lo, int hi) {
while (lo != hi) {
int temp = A[lo];
A[lo] = A[hi];
A[hi] = temp;
class TreeNode { | |
int val; | |
TreeNode left; | |
TreeNode right; | |
public TreeNode(int x) { this.val = x; } | |
} | |
public void printIntersection(TreeNode p, TreeNode q) { | |
if (p == null || q == null) { |
- lxml - Pythonic binding for the C libraries libxml2 and libxslt.
- boto - Python interface to Amazon Web Services
- Django - Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
- Fabric - Library and command-line tool for streamlining the use of SSH for application deployment or systems administration task.
- PyMongo - Tools for working with MongoDB, and is the recommended way to work with MongoDB from Python.
- Celery - Task queue to distribute work across threads or machines.
- pytz - pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher.
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from itertools import combinations | |
def naive_lis(seq): | |
'''最长上升子(非下降)序列 Longest Increasing Subsequence,基本解法 | |
首先生成一个序列可能的所有子序列,比如说给定数组[1, 2, 3],那么所有可能的子序列是 | |
[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]。 | |
然后从中挑选满足所有上升的子序列。最后算出其中拥有最大长度的一个。 | |
''' |
public class Backtrack { | |
public static List<List<Object>> backtrack(int[] A) { | |
// Set up a list of list to hold all possible solutions | |
List<List<Object>> result = new LinkedList<List<Object>>(); | |
if (A == null || A.length == 0) { | |
return result; | |
} | |
// As we need to recursively generate every solution, | |
// a variable is needed to store single solution. |
Based on Java 7.
There are two built-in sort()
methods, java.util.Arrays.sort()
and java.util.Collections.sot()
. Typically, we use the first one to sort primitive type and the later one to sort Objects.
For primitive type, the sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
For Objects, the implementation was adapted from Tim Peters's list sort for Python (TimSort). It uses techiques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, J
/** | |
* Problem: Find the nth to the end of the linked list. | |
*/ | |
public class FindNth { | |
/** | |
* Solution: | |
* A simple math - nth to the end equals to: the length - n | |
* from the beginning. | |
* We can first calculate the length of the linked list then print |