代码面试最常用的10大算法

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

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

本文总结了程序员在代码面试中最常遇到的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 string

String/arrays很容易理解,但与它们有关的问题常常需要高级的算法去解决,例如动态编程、递归等。
下面列出一些需要高级算法才能解决的经典问题:

Evaluate Reverse Polish Notation
Longest Palindromic Substring
单词分割
字梯
Median of Two Sorted Arrays
正则表达式匹配
合并间隔
插入间隔
Two Sum
3Sum
4Sum
3Sum Closest
String to Integer
合并排序数组
Valid Parentheses
实现strStr()
Set Matrix Zeroes
搜索插入位置
Longest Consecutive Sequence
Valid Palindrome
螺旋矩阵
搜索一个二维矩阵
旋转图像
三角形
Distinct Subsequences Total
Maximum Subarray
删除重复的排序数组
删除重复的排序数组2
查找没有重复的最长子串
包含两个独特字符的最长子串
Palindrome Partitioning

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())。(链表实现队列接口)如果你在面试过程中,需要用到栈或队列解决问题时,你可以直接使用它们。

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

插入两个数字
重新排序列表
链表周期
Copy List with Random Pointer
合并两个有序列表
合并多个排序列表
从排序列表中删除重复的
分区列表
LRU缓存

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

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

二叉树前序遍历
二叉树中序遍历
二叉树后序遍历
字梯
验证二叉查找树
把二叉树变平放到链表里
二叉树路径和
从前序和后序构建二叉树
把有序数组转换为二叉查找树
把有序列表转为二叉查找树
最小深度二叉树
二叉树最大路径和
平衡二叉树

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需要经常用到的算法:

克隆Graph
...全文
73207 293 打赏 收藏 转发到动态 举报
写回复
用AI写文章
293 条回复
切换为时间正序
请发表友善的回复…
发表回复
it_New_Person 2015-09-15
  • 打赏
  • 举报
回复
mark
艺俊 2015-08-15
  • 打赏
  • 举报
回复
mark~~~~~~
yuanmouren1hao 2015-08-09
  • 打赏
  • 举报
回复
站在巨人的肩膀上》。。
youhebuke52011 2015-08-07
  • 打赏
  • 举报
回复
超级赞 感谢楼主分享 666
小强就是我 2015-01-05
  • 打赏
  • 举报
回复
很好很强大!顶起!
nevergu 2014-08-28
  • 打赏
  • 举报
回复
好帖子,慢慢研究
javaMyHeart 2014-08-28
  • 打赏
  • 举报
回复
多谢分享,好贴么么哒
豆沙团子 2014-08-28
  • 打赏
  • 举报
回复
mark!楼主辛苦了~
acheng0609 2014-08-27
  • 打赏
  • 举报
回复
只看懂了一点
Coder_D 2014-08-25
  • 打赏
  • 举报
回复
马住,有空好好学习
tianyi_1993 2014-08-24
  • 打赏
  • 举报
回复
楼主真好!收藏!
火柴棍的坚强 2014-08-24
  • 打赏
  • 举报
回复
好东西
hello涩郎 2014-08-24
  • 打赏
  • 举报
回复
MARK一下,有空来看看。。。
LJ_KB 2014-08-22
  • 打赏
  • 举报
回复
楼主NB,其实算法牛逼的人我觉得都很吊
little丶Sunshine 2014-08-22
  • 打赏
  • 举报
回复
perfect~~~
  • 打赏
  • 举报
回复
感謝分享
diypyh 2014-05-28
  • 打赏
  • 举报
回复
好帖子,收藏!
一切是我的 2014-05-28
  • 打赏
  • 举报
回复
好帖!收藏了,有空就看看。
haorengoodman 2014-05-16
  • 打赏
  • 举报
回复
收藏,消化
zhangxunian89 2014-05-16
  • 打赏
  • 举报
回复
感谢楼主,楼主好人
加载更多回复(245)

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧