In the programmer's career, the algorithm is also a basic course, especially in the interview, many companies will let programmers write some algorithm examples, such as fast sorting, binary tree search and so on.
This article summarizes the 10 most common algorithm types that programmers encounter most often in code interviews, and it takes programmers a lot of effort to really understand the principles of these algorithms.
1.string/array/matrix
In Java, a string is a class that contains a char array and other fields, methods. If there is no IDE AutoComplete code, here's how you should remember:
ToCharArray ()//get char array of a stringarrays.sort () //sort an arrayarrays.tostring (char[] a)//convert to STRINGCH Arat (int x)//get a char at the specific indexlength ()//string lengthlength//array size substring (int beginindex) substr ing (int beginindex, int endIndex) integer.valueof ()//string to Integerstring.valueof ()/integer to String
String/arrays is easy to understand, but problems associated with them often require advanced algorithms to solve, such as dynamic programming, recursion, and so on.
Here are some classic questions that require advanced algorithms to solve:
- Evaluate Reverse Polish Notation
- Longest palindromic Substring
- Word segmentation
- Word Ladder
- Median of Sorted Arrays
- Regular expression Matching
- Merge interval
- Insertion interval
- The Sum of
- 3Sum
- 4Sum
- 3Sum Closest
- String to Integer
- Merging sorted arrays
- Valid parentheses
- Implement STRSTR ()
- Set Matrix Zeroes
- Search Insertion Location
- Longest consecutive Sequence
- Valid palindrome
- Spiral Matrix
- Search for a two-dimensional matrix
- Rotate image
- Triangle
- Distinct subsequences Total
- Maximum Subarray
- To delete a repeating sorted array
- To delete a repeating sorted array 2
- Find the oldest string with no duplicates
- Oldest string containing two unique characters
- Palindrome Partitioning
2. Linked list
Implementing a linked list in Java is straightforward, with each node having a value and then linking it to the next node.
Class Node {int val; Node Next; Node (int x) {val = X;next = null;}}
The more popular two list examples are stacks and queues.
Stacks (Stack)
Class Stack{node top; Public node Peek () {if (top! = null) {return top;} return null; public node pop () {if (top = null) {return null;} Else{node temp = new Node (top.val); top = Top.next;return temp;}} public void push (Node N) {if (n! = null) {N.next = Top;top = N;}}}
Queuing (queue)
Class Queue{node First, Last; public void Enqueue (Node n) {if (first = = null) {first = N;last = First;} Else{last.next = N;last = n;}} public Node dequeue () {if (first = = null) {return null;} Else{node temp = new Node (first.val); first = First.next;return temp;}}}
It is worth mentioning that the Java standard library already contains a class called Stack, the list can also be used as a queue (add () and remove ()). (Linked list Implementation Queue interface) If you need to use stacks or queues to solve problems during an interview, you can work with them directly.
In practice, the algorithms that need to be used in the list are:
- Insert two digits
- Reorder Lists
- Linked list Period
- Copy List with Random Pointer
- Merge two ordered lists
- Merging multiple sorted lists
- Removes a duplicate from the sorted list
- Partition list
- LRU Cache
3. Tree & Heap
The tree here usually refers to a two-fork tree.
Here are some concepts related to the two-fork tree:
- Binary Tree search: For all nodes, the order is: Left children <= current node <= right children;
- Balance vs. Unbalanced: It is an empty tree or its left and right two subtree the absolute value of the difference is not more than 1, and the left and right two subtrees are a balanced binary tree;
- Full two fork tree: all nodes on each layer have two sub-nodes except for the last layer without any subnodes;
- Perfect binary tree (Perfect binary trees): A tree full of two forks, all leaves in the same depth or the same level, and each parent node has two child nodes;
- Complete binary tree: If the depth of the two-fork tree is H, except for the H layer, the nodes of each layer (1~H-1) reach the maximum number, and all the nodes in the H layer are continuously concentrated on the leftmost side, which is the complete binary tree.
Heap is a tree-based data structure, also known as a priority queue (Priorityqueue), in the queue, the scheduler repeatedly extracts the first job in the queue and run, so the actual situation of some shorter tasks will wait a long time to end, or some not short, But the work of importance should also have priority. A heap is a data structure designed to solve this kind of problem.
Some algorithms based on binary tree and heap are listed below:
- Binary tree pre-sequence traversal
- Sequential traversal in binary tree
- Binary Tree post-traversal
- Word Ladder
- Verify Binary Lookup tree
- Flatten the two-prong tree into the list
- Binary Tree path and
- Construction of binary tree from the former and post-order
- Convert an ordered array to a two-fork lookup tree
- Turn an ordered list to a binary lookup tree
- Minimum depth two fork tree
- Binary Tree maximum path and
- Balanced two-pronged tree
This article draws on: Http://www.csdn.net/article/2014-04-10/2819237-Top-10-Algorithms-for-Coding-Interview (..... )
Please take a look at the next article http://blog.csdn.net/u011225629/article/details/48320669
Copyright notice: I feel like I'm doing a good job. I hope you can move your mouse and keyboard for me to order a praise or give me a comment, under the Grateful!_____________________________________________________ __ Welcome reprint, in the hope that you reprint at the same time, add the original address, thank you with
10 most common algorithms for code interviewing