Here are the concepts related to the top 10 algorithms in the programming interview, and I'll use some simple examples to illustrate these concepts. Since the full mastery of these concepts requires more effort, this list is just as an introduction. This article looks at the problem from a Java perspective and includes the following concepts:
1. string
If the IDE does not have the code auto-completion feature, you should keep these methods in mind.
ToCharArray ()//get string corresponding to char array arrays.sort ()//array sort arrays.tostring (char[] a)//array to string charat (int x)// Gets the character at an index length ()//String length//array size
2. Linked list
In Java, the implementation of a linked list is very simple, with each node having a value Val and a link to the next node next.
Class Node {int val; Node Next; Node (int x) {val = x; next = null; }}
List of two notable applications are stack stacks and queue queues.
Stack:
class stack{ node top; public Node peek () {//Views the object at the top of the stack, but does not remove it from the stack. if (top != null) { return top; } return null; } public node pop () {//Removes the object at the top of the stack and returns the object as the value of this function. if (top == null) { return null; }else{ node temp = new node (top.val); top = top.next; return temp; } } public void push (node n) {//the item is pressed onto the top of the stack. if (n != null) { n.next = top; top = n; } } }
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; } } }
3. Tree
The tree here usually refers to a two-fork tree, each of which contains a left child node and a right child node, as follows:
class treenode{int value; TreeNode left; TreeNode right; }
Here are some of the concepts related to trees:
Balance vs. unbalanced: In a balanced binary tree, the depth difference between the left and right subtrees of each node is at most 1 (1 or 0).
Full binary tree with two forks: each node except the leaf node has two children.
Perfect binary tree (Perfect binary trees): is a full two-tree with the following properties: All leaf nodes have the same depth or level, and each parent node must have two children.
Complete binary tree: Two in a tree, each layer is fully filled, except for the last one, and all nodes must be left-leaning as far as they can.
Translator Note: The perfect binary tree is also vaguely called a complete binary tree. An example of a perfect binary tree is an ancestor diagram of a person at a given depth, because everyone must have two birth parents. A complete binary tree can be seen as a perfect binary tree that can have several additional left-leaning leaf nodes. Question: What is the difference between a perfect binary tree and a full two-fork tree? (Reference: http://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html)
4. Figure
Graph-related issues are mainly focused on depth-first search and breadth-depth (breath first search).
The following is an implementation of a simple graph breadth-first search.
1) Define Graphnode
Class graphnode{int val; Graphnode Next; Graphnode[] neighbors; Boolean visited; Graphnode (int x) {val = x; } graphnode (int x, graphnode[] n) {val = x; Neighbors = n; } public String toString () {return ' value: ' + this.val; } }
2) define a queue
class queue{ graphnode first, last; Public void enqueue (graphnode n) { if (first == null) { first = n; last = first; }else{ last.next = n; last = n; } } public graphnode dequeue () { if (first == null) { return null; }else{ graphnode temp = new graphnode (first.val, first.neighbors); first = first.next; return temp; } } }
3) Achieve breadth-first search using queue queues
Public class graphtest { public static void main (String[] args) { GraphNode n1 = new Graphnode (1); graphnode n2 = new graphnode (2); graphnode n3 = New graphnode (3); graphnode n4 = new graphnode (4); graphnode n5 = new graphnode (5); n1.neighbors = new graphnode[]{n2,n3,n5}; n2.neighbors = new graphnode[]{n1,n4}; n3.neighbors = new graphnode[]{n1,n4,n5}; n4.neighbors = new graphnode[]{ n2,n3,n5}; n5.neighbors = new graphnode[]{ N1,n3,n4}; breathfirstsearch (n1, 5); } public static void breathfirstsearch (GraphNode root, int x) { if (root.val == x) system.out.println ("Find in root "); queue queue = new queue (); root.visited = true; queue.enqueue (Root); while (QUEUE.FIRST&NBsp;! = null) { graphnode c = (Graphnode) queue.dequeue (); for (graphnode n: c.neighbors) { if (!n.visited) { system.out.print (n + " "); n.visited = true; if (n.val ==  X) system.out.println ("find " +n); queue.enqueue (n); } } } } }
5. Sorting
Here is the time complexity of the different sorting algorithms, you can go to the wiki to see the basic idea of these algorithms.
In addition, here are some implementations/demos:: counting sort,mergesort, Quicksort, insertionsort.
7 commonly used sorting algorithms for visually intuitive perception
Video: 6 - minute demonstration of the three sorting algorithms
6. recursion vs. Iteration
For programmers, recursion should be an innate thought (a built-in thought), which can be explained by a simple example.
Problem: There are n steps, only 1 or 2 steps at a time, the total number of ways to go.
Step 1: Find the relationship between the top n steps and the front n-1 step steps.
In order to finish the n steps, there are only two methods: Climb 1 steps from the n-1 step steps or climb 2 steps from the n-2 steps. If f (n) is the number of methods that crawl to the nth step, then f (n) = f (n-1) + f (n-2).
Step 2: Make sure that the starting condition is correct.
F (0) = 0;
F (1) = 1;
public static int F (int n) {if (n <= 2) return n; int x = f (n-1) + f (n-2); return x; }
The time complexity of the recursive method is the number of N, because there are many redundant computations, as follows:
F (5)
F (4) + F (3)
F (3) + F (2) + F (2) + F (1)
F (2) + f (1) + f (1) + f (0) + F (1) + f (0) + F (1)
F (1) + f (0) + F (1) + f (1) + f (0) + F (1) + f (0) + F (1)
The direct idea is to translate recursion into iterations:
public static int F (int n) {if (n <= 2) {return n; } int first = 1, second = 2; int third = 0; for (int i = 3; I <= n; i++) {third = first + second; first = second; second = third; } return third; }
For this example, iterations take less time, and you might want to see recursion vs iteration.
7. Dynamic Planning
Dynamic planning is a technique for solving the following nature-class problems:
①. A problem can be solved by the solution of the problem (translator Note: The optimal solution of the problem contains the optimal solution of its sub-problem, that is, the optimal sub-structure properties).
②. The solution of some sub-problems may need to be calculated several times (translator note: That is, the overlapping nature of sub-problems).
③. The solution to the sub-problem is stored in a table so that each sub-problem is calculated only once.
④. Additional space is required to save time.
The stair climbing problem is completely in line with the above four properties, so it can be solved by dynamic programming method.
public static int[] A = new int[100]; public static int F3 (int n) {if (n <= 2) a[n]= N; if (A[n] > 0) return a[n]; else A[n] = F3 (n-1) + F3 (n-2);//store results so only calculate once! return a[n]; }
8. bit Operation
Bitwise operators:
Gets the first bit of the given number n: (I count from 0 and start from the right)
public static Boolean getbit (int num, int i) {int result = num & (1<<i); if (result = = 0) {return false; }else{return true; }
For example, get the 2nd digit of the Number 10:
I=1, n=10
1<<1= 10
1010&10=10
0, so return true;
9. probability problems
Solving probability-related problems usually requires a good plan to understand the problem (formatting the problem), where there is just one simple example of this type of problem:
There are 50 people in a room, so what is the probability that at least two people have the same birthday? (ignoring the fact that leap years, that is, 365 days a year)
The probability of calculating certain things can often be converted to calculate their relative faces first. In this example, we can calculate the probability that everyone's birthdays are different, that is: 365/365 * 364/365 * 363/365 * ... * (365-49)/365, so that at least two people birthday the same probability is the value of this.
public static double caculateprobability (int n) {double x = 1; for (int i=0; i<n; i++) {x *= (365.0-i)/365.0; } Double Pro = Math.Round ((1-x) * 100); return pro/100;
Calculateprobability (50) = 0.97
10. Permutations and combinations
The difference between composition and arrangement is whether the order is critical.
Reference/Recommendation information:
1. Binary Tree
2. Introduction to Dynamic programming
3. utsa Dynamic Programming Slides
4. Birthday Paradox
5. Cracking the Coding interview:150 programming interview Questions and Solutions, Gayle Laakmann McDowell
Article from: http://blog.jobbole.com/52144/
Top 10 algorithms for programming interviews