[Turn] brush for a month leetcode algorithm successfully accept Ali Baba, beating bytes, Netease and other manufacturers offer

Turn:  https://zhuanlan.zhihu.com/p/90057817

 Foreword

With the advent of the Internet's cold, more and more Internet companies to increase the difficulty of the interview, one of which is to increase the proportion of interview questions among Shredded algorithm. Here that the problem is not deep learning algorithms, this type of machine learning algorithms, but sorting, breadth-first, dynamic programming such as assessment data structure also assess the ability of programming topics. The very title of the URL brush, which leetcode is the most famous.

Brush on topic, I spent a lot of time, a lot of times the pit, summed up what, there are three questions:

  1. Brush off the question always forget the second time when the brush or will not do
  2. Brush title is very slow , even spend the day, often only brush fifty-six
  3. Insisted on not down , always brush it slows down to half, the Second Coming when I brush, brush off questions are front and almost forgotten

He says it is tears, feeling brush the question is really difficult to walk this road, spent a lot of time, but feel no harvest. So lately I've been reflecting on the theme of his brush method, hoping to improve the efficiency and speed of the brush title. When I summarized the following methods later, I obviously feel their problems faster brush from fifty-six one day before the weekend to weekend brush more than fifteen six day rate compared to the previous lift is very obvious.

In this paper, the Java language to describe, to help you sort out what a good data structures and algorithms, and work on with the interview. That summed up a common data structures, and the corresponding implementation in Java, a view summed up the theory and practice step in place.

Common data structures

Array

An array is the same type of data elements arranged in a certain order in the collection, it is a contiguous memory space. Advantage of the array is: the get and set operation time is O (1); the disadvantages are: add and remove the operating time is O (N) is.

When in Java, Array is the array. In addition, ArrayList array Array used as a basis for its implementation, and its general Array, the biggest benefit is that we do not have to consider when adding a cross-border element, the array elements beyond capacity, it will automatically ensure capacity expansion.

Vector and ArrayList compared, the main difference is that more than a thread safety, but efficiency is relatively low. Today java.util.concurrent package offers a number of thread-safe collection classes (such as LinkedBlockingQueue), so do not use the Vector.

List

List is a non-continuous, non-sequential logical order structure, data elements is achieved by the link pointer of the order of the list, a list composed of a series of nodes. Advantage is that list: add and remove the operating time is O (1); a disadvantage is that: the get and set operation time is O (N), and the additional space required storage address points to other data items.

For lookup operation on unsorted arrays and lists time is O (N).

queue

A queue is a special linear form, is special in that it only allows deletion at the front end of the table, while the rear end of insertion, in the table, i.e. the so-called first in first out (FIFO).

In Java, LinkedList realized Deque, can be used as two-way queue (could also be used as a one-way natural queue). Further PriorityQueue achieved with priority queues, i.e. each element of the queue has priority and the element ordered by priority.

Stack

Stack (Stack), also known as the stack, which is a linear form of operation is limited. With the restriction that only permits insertion and deletion operation at one end of the table. This input is called the stack, relatively, and the other end is called the bottom of the stack. It embodies the last in, first out (LIFO) characteristics.

Java, the Stack implements this feature, but also inherited Stack Vector, and therefore have a low efficiency and thread-safe line two properties latest JDK8, the recommendation is implemented with Deque stack.

set

It refers to a collection of abstract or summary of the specific subject having a specific property into a collective, these objects are called elements of the collection, its main features is not repeated element.

In Java, HashSet reflects this data structure, HashSet is built on the basis of MashMap. LinkedHashSet inherited HashSet, HashCode used to determine the position in the set, determining a position using the linked list, so sequential. TreeSet SortedSet implements the interface, a set of sorted (built TreeMap basis), and therefore faster lookup operation (log (N)) than ordinary Hashset; insertion slower (log (N)), due to the maintain orderly.

Hash table

Hash table is also called a hash table, a data structure is based on key access key (Keyvalue), to access the records mapped to a table in a position by adding key value to find the speed, the mapping function called a hash function.

Java HashMap achieved in the hash table, and a thread-safe Hashtable more than it is, but the use of the global lock result in lower performance, it is now generally used to achieve ConcurrentHashMap thread-safe HashMap (similar to the above data structure in the latest of almost java.util.concurrent package has a corresponding high-performance thread-safe class). SortMap TreeMap implement interfaces, it is possible to sort the records kept by key. LinkedHashMap retains the insertion element sequence. WeakHashMap is an improved HashMap, the implementation of its key "weak reference", if a key is no longer referenced by an external, then the key can be recovered GC, without the need for us to manually remove.

tree

Tree (Tree) comprising n (n> 0) is a finite set of nodes, wherein:

  • Each element called nodes (node)
  • There is a particular node is called the root node or the root (root)
  • Remaining data elements other than the root node is divided into m (m≥0) disjoint binding T1, T2, ...... Tm-1, wherein each set Ti (1 <= i <= m) is itself a tree, the tree is referred to the original subtree (subtree)

This tree data structure in the computer world in a wide range of applications, such as operating system used in the red-black trees, B + tree database uses the syntax tree compiler, memory management uses a heap (also a tree in nature), information theory the Huffman coding, etc., etc., in Java TreeSet and TreeMap uses a tree to sort (binary search to improve the retrieval speed), but generally requires the programmer to define a tree of class and implement relevant property, and no ready-made API.

The following use Java to achieve a variety of common trees.

Binary Tree

Is a basic and important binary tree data structure, each node at most two sub-tree, binary tree has left and right sub-tree, the i-th layer at most 2 (i-1) th node (i starting with 1) ; binary tree of depth k at most 2 (k) -1) nodes, any binary tree, if it is the terminal number of nodes n0, the number of nodes of degree 2 is n2, then n0 = n2 + 1.

Binary tree properties:

  • In a non-null binary tree, the total number of nodes of the i-layer is not more than 2 ^ (i-1), i> = 1;
  • H binary tree depth up to 2 ^ h-1 nodes (h> = 1), a minimum of nodes h;
  • For an arbitrary binary tree, if it points to the leaf node N0, and the total number of nodes of degree 2 is N2, then N0 = N2 + 1;
  • Complete binary tree of n nodes having a depth of log2 (n + 1);
  • There are N nodes in the binary tree each node if the stored sequence, the following relationship between junction points: If I is the node number if I> 1, then the parent node number is I / 2; if 2I <= N, is the son of the left (i.e., left subtree root) 2I is numbered; if 2I> N, no son left; if 2I + 1 <= N, it is the right son node No. 2I + 1; if 2I + 1> N, the right son no.
  • Given N nodes, can be constructed of different binary H (N), where H (N) N is the number of items of Cattleya, h (n) = C (2 * n, n) / (n + 1) .
  • Has a branch point i, I is the sum of the lengths of all the branch points of roads, J is the sum of the length of the road leaves J = I + 2i.

Full binary tree, complete binary tree

Full Binary: except the last one without any child node, all the nodes on each layer has two child nodes;

Complete binary tree: Assuming that binary tree depth is h, h except the first layer, the other layers (1 ~ (h-1) layer) reached a maximum number of nodes, the h layer all nodes are continuously concentrated in far left, this is the complete binary tree;

Full binary tree complete binary tree is a special case.

Binary search tree

Binary search tree, also known as the tree is a binary sort (Binary Sort Tree) or binary search tree. Binary sort tree or an empty tree or a binary tree having the following properties:

  • If the left subtree is not empty, then the value of the left sub-tree, all the nodes are less than the value of the root;
  • If the right subtree is not empty, the value of the right sub-tree, all nodes are equal to or greater than the value of the root;
  • Left and right subtrees are also binary sort tree;
  • There is no equivalent key node.

Binary search tree properties: Find tree in order traversal of a binary, you can get an ordered sequence. The complexity of a binary search tree of time: it, like binary search, insertion and lookup time complexity are O (logn), but in the worst case there will still be O (n) time complexity. The reason is that insert and delete elements when the tree is not balanced. We seek is still a good time complexity in the worst case, this is a balanced binary tree design in mind.

Balanced binary tree

Balanced binary tree is also known as an AVL tree, having the following properties: it is an absolute value of an empty tree or a left and right subtrees height difference of not more than 1, and left and right sub-trees are a balanced binary tree. Its appearance is to solve the imbalance binary search tree search efficiency degenerate linear problem, because when you delete and insert it maintains the balance of the tree, makes finding time to keep in O (logn), binary search tree is more stable than .

stack

Heap is a complete binary tree, in this tree, the parent of all the nodes are greater than or equal to meet its child nodes called big heap root stack, all parent nodes that satisfy the stack less child nodes petit root stack. Although heap is a tree, but usually stored in an array, the parent-child relationship between the parent and the child node is determined by the array index.

Use heap: heap sort, priority queue. Furthermore, since the price adjustment is small, but also for real-time and change the type of sorting.

At last

Read, read, found in order to summarize the place is a very big project, the road ahead will be long Come, happiness and earth.

I personally think that, as a technical person is necessary to maintain the attitude of lifelong learning, so learning ability become the core competitiveness in order not to be eliminated by the times, and efficient time use makes you more good, so here I am these part of the algorithm takes two months to collect books brush questions, give people in need, I hope this information can help you

"Algorithm fun."

"algorithm"

"Algorithm Brush title Leetcode Edition"

发布了900 篇原创文章 · 获赞 387 · 访问量 279万+

Guess you like

Origin blog.csdn.net/kingmax54212008/article/details/104082498