摘要:面试也是一门学问,在面试之前做好充分的准备则是成功的必须条件,而程序员在代码面试时,常会遇到编写算法的相关问题,比如排序、二叉树遍历等等。在程序员的职业生涯中,算法亦算是一门基础课程,尤其是在面试的时候,很多公司都会让程序员编写一些算法实例,例如快速排序、二叉树查找等等。本文总结了程序员在代码面试中最常遇到的10大算法类型,想要真正了解这些算法的原理,还需程序员们花些功夫。1.String/Array/Matrix在java中,String是一个包含char数组和其它字段、方法的类。如果没有IDE自动完成代码,下面这个方法大家应该记住:
Java code?12345678910 | toCharArray() //get char array of a String Arrays.sort() //sort an array Arrays.toString( char [] a) //convert to string charAt( int x) //get a char at the specific index length() //string length length //array size substring( int beginIndex) substring( int beginIndex, int endIndex) Integer.valueOf() //string to integer String.valueOf()/integer to string |
123456789 | class Node { int val; Node next; Node( int x) { val = x; next = null ; } } |
12345678910111213141516171819202122232425262728 | 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; } } } |
1234567891011121314151617181920212223 | 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; } } } |
12345 | class TreeNode{ int value; TreeNode left; TreeNode right; } |
12345678910111213141516171819 | 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; } } |
1234567891011121314151617181920212223 | 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; } } } |
1234567891011121314151617181920212223242526272829303132333435363738394041 | 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 != 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); } } } } } |
克隆Graph
5.排序不同排序算法的时间复杂度,大家可以到wiki上查看它们的基本思想。BinSort、Radix Sort和CountSort使用了不同的假设,所有,它们不是一般的排序方法。 下面是这些算法的具体实例,另外,你还可以阅读:Java开发者在实际操作中是如何排序的。归并排序快速排序插入排序6.递归和迭代下面通过一个例子来说明什么是递归。问题:这里有n个台阶,每次能爬1或2节,请问有多少种爬法?步骤1:查找n和n-1之间的关系为了获得n,这里有两种方法:一个是从第一节台阶到n-1或者从2到n-2。如果f(n)种爬法刚好是爬到n节,那么f(n)=f(n-1)+f(n-2)。 步骤2:确保开始条件是正确的f(0) = 0; f(1) = 1;
12345 | public static int f( int n){ if (n <= 2 ) return n; int x = f(n- 1 ) + f(n- 2 ); return x; } |
1234 | f( 5 ) f( 4 ) + f( 3 ) f( 3 ) + f( 2 ) + f( 2 ) + f( 1 ) f( 2 ) + f( 1 ) + f( 2 ) + f( 2 ) + f( 1 ) |
1234567891011121314151617 | 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; } |
123456789101112 | 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]; } |
123456789 | public static boolean getBit( int num, int i){ int result = num & ( 1 <<i); if (result == 0 ){ return false ; } else { return true ; } } |
1234 | i= 1 , n= 10 1 << 1 = 10 1010 & 10 = 10 10 is not 0 , so return true ; |
12345678910 | 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 ; } |
排列顺序
rreference : http://bbs.csdn.net/topics/390768965
新闻热点
疑难解答