CSDN总结的面试中的十大算法

简介:

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 string

String/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.树&堆

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

 

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

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

 

 

4.Graph 

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

 

第一步,定义一个GraphNode

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
     }
}

第二步,定义一个队列

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
         }  
     }
}

第三步,使用队列进行宽度优先搜索

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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使用了不同的假设,所有,它们不是一般的排序方法。 

下面是这些算法的具体实例,另外,你还可以阅读: 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; 

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

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

 

 

 

1
2
3
4
f( 5 )
f( 4 ) + f( 3 )
f( 3 ) + f( 2 ) + f( 2 ) + f( 1 )
f( 2 ) + f( 1 ) + f( 2 ) + f( 2 ) + f( 1 )

该递归可以很简单地转换为迭代。 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.动态规划

动态规划主要用来解决如下技术问题:

 

  • 通过较小的子例来解决一个实例;
  • 对于一个较小的实例,可能需要许多个解决方案;
  • 把较小实例的解决方案存储在一个表中,一旦遇上,就很容易解决;
  • 附加空间用来节省时间。

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
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开始,然后向右开始)

 

1
2
3
4
5
6
7
8
9
public  static  boolean  getBit( int  num, int  i){
     int  result = num & ( 1 <<i);
  
     if (result == 0 ){
         return  false ;
     } else {
         return  true ;
     }
}

例如,获取10的第二位:

 

 

 

1
2
3
4
i= 1 , n= 10
1 << 1 = 10
1010 & 10 = 10
10  is not 0 , so return  true ;

典型的位算法:

 

 

 

9.概率

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

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

算法:

 

1
2
3
4
5
6
7
8
9
10
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.组合和排列

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

例1:

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

例2:

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

基于它们的一些常见算法

 本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/3860346.html,如需转载请自行联系原作者

相关文章
|
1月前
|
开发框架 算法 搜索推荐
C# .NET面试系列九:常见的算法
#### 1. 求质数 ```c# // 判断一个数是否为质数的方法 public static bool IsPrime(int number) { if (number < 2) { return false; } for (int i = 2; i <= Math.Sqrt(number); i++) { if (number % i == 0) { return false; } } return true; } class Progr
58 1
|
16天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
32 0
|
1月前
|
算法
覃超老师 算法面试通关40讲
无论是阿里巴巴、腾讯、百度这些国内一线互联网企业,还是 Google、Facebook、Airbnb 等硅谷知名互联网公司,在招聘工程师的过程中,对算法和数据结构能力的考察都是重中之重。本课程以帮助求职者在短时间内掌握面试中最常见的算法与数据结构相关知识点,学会面试中高频算法题目的分析思路,同时给大家从面试官的角度来分析算法题的解答技巧,从而更有效地提升求职者的面试通过率。
15 3
覃超老师 算法面试通关40讲
|
1月前
|
存储 算法
【数据结构与算法】【腾讯阿里链表面试题】算法题--链表易懂版讲解
【数据结构与算法】【腾讯阿里链表面试题】算法题--链表易懂版讲解
|
1月前
|
存储 机器学习/深度学习 算法
python常用算法,新手必会,面试必出
python常用算法,新手必会,面试必出
37 0
|
1月前
|
存储 算法 Java
【数据结构与算法】2、链表(简单模拟 Java 中的 LinkedList 集合,反转链表面试题)
【数据结构与算法】2、链表(简单模拟 Java 中的 LinkedList 集合,反转链表面试题)
43 0
|
2月前
|
机器学习/深度学习 算法 Java
递归算法还有哪些是你不知道的----【探讨Java经典遍历问题和面试题】
递归算法还有哪些是你不知道的----【探讨Java经典遍历问题和面试题】
32 1
|
3月前
|
存储 缓存 算法
数据结构与算法面试题:实现一个 LRU 缓存,支持如下操作:获取值、更新值、删除键值对和插入键值对
数据结构与算法面试题:实现一个 LRU 缓存,支持如下操作:获取值、更新值、删除键值对和插入键值对
28 0
|
3月前
|
存储 算法 Java
数据结构和算法面试题:给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
数据结构和算法面试题:给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
42 0
|
3月前
|
算法 Java C++
数据结构与算法面试题:实现一个函数 fill(int[] a, int n, int v),使其将大小为 n 的数组 a 填满为 v。
数据结构与算法面试题:实现一个函数 fill(int[] a, int n, int v),使其将大小为 n 的数组 a 填满为 v。
16 0