尊龙凯时官方app下载
14
点赞
3
评论
5
转载
收藏

代码面试最常用的10大算法-尊龙凯时官方app下载

摘要:面试也是一门学问,在面试之前做好充分的准备则是成功的必须条件,而程序员在代码面试时,常会遇到编写算法的相关问题,比如排序、二叉树遍历等等。

在程序员的职业生涯中,算法亦算是一门基础课程,尤其是在面试的时候,很多公司都会让程序员编写一些算法实例,例如快速排序、二叉树查找等等。

本文总结了程序员在代码面试中最常遇到的10大算法类型,想要真正了解这些算法的原理,还需程序员们花些功夫。

1.string/array/matrix

在java中,string是一个包含char数组和其它字段、方法的类。如果没有ide自动完成代码,下面这个方法大家应该记住: 

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 stringstring/arrays很容易理解,但与它们有关的问题常常需要高级的算法去解决,例如动态编程、递归等。

下面列出一些需要高级算法才能解决的经典问题:

    2.链表

    在java中实现链表是非常简单的,每个节点都有一个值,然后把它链接到下一个节点。 

    class node { int val; node next; node(int x) { val = x; next = null; } }

    比较流行的两个链表例子就是栈和队列。

    栈(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; } } }队列(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; } } }

    值得一提的是,java标准库中已经包含一个叫做stack的类,链表也可以作为一个队列使用(add()和remove())。(链表实现队列接口)如果你在面试过程中,需要用到栈或队列解决问题时,你可以直接使用它们。

    在实际中,需要用到链表的算法有:

      3.树&堆

      这里的树通常是指二叉树。

      class treenode{ int value; treenode left; treenode right; } 下面是一些与二叉树有关的概念:
      • 二叉树搜索:对于所有节点,顺序是:left children <= current node <= right children;
      • 平衡vs.非平衡:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树;
      • 满二叉树:除最后一层无任何子节点外,每一层上的所有结点都有两个子结点;
      • 完美二叉树(perfect binary tree):一个满二叉树,所有叶子都在同一个深度或同一级,并且每个父节点都有两个子节点;
      • 完全二叉树:若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。

      堆(heap)是一个基于树的数据结构,也可以称为优先队列( ),在队列中,调度程序反复提取队列中第一个作业并运行,因而实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短小,但具有重要性的作业,同样应当具有优先权。堆即为解决此类问题设计的一种数据结构。

      下面列出一些基于二叉树和堆的算法:

        4.graph 

        与graph相关的问题主要集中在深度优先搜索和宽度优先搜索。深度优先搜索非常简单,你可以从根节点开始循环整个邻居节点。下面是一个非常简单的宽度优先搜索例子,核心是用队列去存储节点。


        第一步,定义一个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; } }

        第二步,定义一个队列

        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; } } }第三步,使用队列进行宽度优先搜索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); } } } } }输出结果:
        value: 2 value: 3 value: 5 find value: 5 
        value: 4

        实际中,基于graph需要经常用到的算法:

          5.排序

          不同排序算法的时间复杂度,大家可以到wiki上查看它们的基本思想。


          binsort、radix sort和countsort使用了不同的假设,所有,它们不是一般的排序方法。 

          下面是这些算法的具体实例,另外,你还可以阅读: 。

            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; 

            public static int f(int n){ if(n <= 2) return n; int x = f(n-1) f(n-2); return x; }

            递归方法的时间复杂度指数为n,这里会有很多冗余计算。

            f(5) f(4) f(3) f(3) f(2) f(2) f(1) f(2) f(1) f(2) f(2) f(1)该递归可以很简单地转换为迭代。 
            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; }

            在这个例子中,迭代花费的时间要少些。关于迭代和递归,你可以去 看看。

            7.动态编程

            动态编程主要用来解决如下技术问题:

            • an instance is solved using the solutions for smaller instances;
            • 对于一个较小的实例,可能需要许多个尊龙凯时官方app下载的解决方案;
            • 把较小实例的尊龙凯时官方app下载的解决方案存储在一个表中,一旦遇上,就很容易解决;
            • 附加空间用来节省时间。

            上面所列的爬台阶问题完全符合这四个属性,因此,可以使用动态编程来解决: 

            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.位操作

              位操作符:


              从一个给定的数n中找位i(i从0开始,然后向右开始)

              public static boolean getbit(int num, int i){ int result = num & (1<

              例如,获取10的第二位:

              i=1, n=10 1<<1= 10 1010&10=10 10 is not 0, so return true;典型的位算法:

                9.概率

                通常要解决概率相关问题,都需要很好地格式化问题,下面提供一个简单的例子: 

                有50个人在一个房间,那么有两个人是同一天生日的可能性有多大?(忽略闰年,即一年有365天)

                算法:

                public static double caculateprobability(int n){ double x = 1; for(int i=0; i结果:
                calculateprobability(50) = 0.97

                10.组合和排列

                组合和排列的主要差别在于顺序是否重要。

                例1:

                1、2、3、4、5这5个数字,输出不同的顺序,其中4不可以排在第三位,3和5不能相邻,请问有多少种组合?

                例2:

                有5个香蕉、4个梨、3个苹果,假设每种水果都是一样的,请问有多少种不同的组合?

                基于它们的一些常见算法

                  来自:

                  声明:本内容系学者网用户个人学术动态分享,不代表平台立场。

                  评论 3

                  默认头像
                  2018-04-03 19:25
                  nice article. understood the topic very well. thanks for sharing. cheers, http://www.flowerbrackets.com/how-to-implement-quick-sort-in-java-program/
                  李亚文 2014-04-11 21:25
                  csdn....
                  回复 李亚文 2018-04-03 19:27
                  csdn - chinese software developer network
                  广东药科大学 医药信息工程学院
                  近期热门动态
                  学者网历年尊龙凯时官方app下载首页界面
                  11390 2015-01-09 20:55:45
                  学者网协办“人本计算”国际会议(hcc2018)征文通知
                  11019 2018-03-15 21:20:27
                  scholat research 2015年度报告
                  10843 2016-01-26 13:38:01
                  中国科学院朱嘉奇博士应邀访问学者网并作报告
                  8648 2014-12-18 22:45:43
                  7878 2017-06-21 09:28:18
                  scholat.com 学者网
                  免责声明 | 关于尊龙凯时官方app下载 | 联系尊龙凯时官方app下载
                  联系尊龙凯时官方app下载:
                  网站地图