loading请求处理中...

程序开发不可不知的10大算法,你知道几种?

2021-12-01 10:21:29 阅读 11977次 标签: 作者: 陈友达
  如果你在程序开发道路上只想当个码农可以不用看以下文字,如果你认为程序开发只是做做web从数据库里捞数据等,那更不用看以下的文字了。

1.String/Array/Matrix

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

  1. toCharArray() //get char array of a String  
  2. Arrays.sort()  //sort an array  
  3. Arrays.toString(char[] a) //convert to string  
  4. charAt(int x) //get a char at the specific index  
  5. length() //string length  
  6. length //array size   
  7. substring(int beginIndex)   
  8. substring(int beginIndex, int endIndex)  
  9. Integer.valueOf()//string to integer  
  10. 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中实现链表是非常简单的,每个节点都有一个值,然后把它链接到下一个节点。
  1. class Node {  
  2.     int val;  
  3.     Node next;  
  4.    
  5.     Node(int x) {  
  6.         val = x;  
  7.         next = null;  
  8.     }  
  9. }
  比较流行的两个链表例子就是栈和队列。

  1. 栈(Stack)
  2. class Stack{  
  3.     Node top;   
  4.    
  5.     public Node peek(){  
  6.         if(top != null){  
  7.             return top;  
  8.         }  
  9.    
  10.         return null;  
  11.     }  
  12.    
  13.     public Node pop(){  
  14.         if(top == null){  
  15.             return null;  
  16.         }else{  
  17.             Node temp = new Node(top.val);  
  18.             top = top.next;  
  19.             return temp;      
  20.         }  
  21.     }  
  22.    
  23.     public void push(Node n){  
  24.         if(n != null){  
  25.             n.next = top;  
  26.             top = n;  
  27.         }  
  28.     }  
  29. }
  30. 队列(Queue)
  31. class Queue{
  32. Node first, last;
  33.  
  34. public void enqueue(Node n){
  35. if(first == null){
  36. first = n;
  37. last = first;
  38. }else{
  39. last.next = n;
  40. last = n;
  41. }
  42. }
  43.  
  44. public Node dequeue(){
  45. if(first == null){
  46. return null;
  47. }else{
  48. Node temp = new Node(first.val);
  49. first = first.next;
  50. return temp;
  51. }
  52. }
  53. }
  值得一提的是,Java标准库中已经包含一个叫做Stack的类,链表也可以作为一个队列使用(add()和remove())。(链表实现队列接口)如果你在面试过程中,需要用到栈或队列解决问题时,你可以直接使用它们。

  在实际中,需要用到链表的算法有:
  • 插入两个数字
  • 重新排序列表
  • 链表周期
  • Copy List with Random Pointer
  • 合并两个有序列表
  • 合并多个排序列表
  • 从排序列表中删除重复的
  • 分区列表
  • LRU缓存
3.树&堆

  这里的树通常是指二叉树。
  1. class TreeNode{
  2. int value;
  3. TreeNode left;
  4. TreeNode right;
  5. }

  下面是一些与二叉树有关的概念:
  • 二叉树搜索:对于所有节点,顺序是:left children <= current node <= right children;
  • 平衡vs.非平衡:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树;
  • 满二叉树:除最后一层无任何子节点外,每一层上的所有结点都有两个子结点;
  • 完美二叉树(Perfect Binary Tree):一个满二叉树,所有叶子都在同一个深度或同一级,并且每个父节点都有两个子节点;
  • 完全二叉树:若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。
  堆(Heap)是一个基于树的数据结构,也可以称为优先队列( PriorityQueue),在队列中,调度程序反复提取队列中第一个作业并运行,因而实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短小,但具有重要性的作业,同样应当具有优先权。堆即为解决此类问题设计的一种数据结构。

  下面列出一些基于二叉树和堆的算法:
  • 二叉树前序遍历
  • 二叉树中序遍历
  • 二叉树后序遍历
  • 字梯
  • 验证二叉查找树
  • 把二叉树变平放到链表里
  • 二叉树路径和
  • 从前序和后序构建二叉树
  • 把有序数组转换为二叉查找树
  • 把有序列表转为二叉查找树
  • 最小深度二叉树
  • 二叉树最大路径和
  • 平衡二叉树

4.Graph

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

程序开发不可不知的10大算法,你知道几种?


  第一步,定义一个GraphNode

  1. class GraphNode{ 
  2. int val;
  3. GraphNode next;
  4. GraphNode[] neighbors;
  5. boolean visited;
  6.  
  7. GraphNode(int x) {
  8. val = x;
  9. }
  10.  
  11. GraphNode(int x, GraphNode[] n){
  12. val = x;
  13. neighbors = n;
  14. }
  15.  
  16. public String toString(){
  17. return "value: "+ this.val; 
  18. }
  19. }

第二步,定义一个队列

  1. class Queue{
  2. GraphNode first, last;
  3.  
  4. public void enqueue(GraphNode n){
  5. if(first == null){
  6. first = n;
  7. last = first;
  8. }else{
  9. last.next = n;
  10. last = n;
  11. }
  12. }
  13.  
  14. public GraphNode dequeue(){
  15. if(first == null){
  16. return null;
  17. }else{
  18. GraphNode temp = new GraphNode(first.val, first.neighbors);
  19. first = first.next;
  20. return temp;
  21. }
  22. }
  23. }
第三步,使用队列进行宽度优先搜索
  1. public class GraphTest {
  2.  
  3. public static void main(String[] args) {
  4. GraphNode n1 = new GraphNode(1); 
  5. GraphNode n2 = new GraphNode(2); 
  6. GraphNode n3 = new GraphNode(3); 
  7. GraphNode n4 = new GraphNode(4); 
  8. GraphNode n5 = new GraphNode(5); 
  9.  
  10. n1.neighbors = new GraphNode[]{n2,n3,n5};
  11. n2.neighbors = new GraphNode[]{n1,n4};
  12. n3.neighbors = new GraphNode[]{n1,n4,n5};
  13. n4.neighbors = new GraphNode[]{n2,n3,n5};
  14. n5.neighbors = new GraphNode[]{n1,n3,n4};
  15.  
  16. breathFirstSearch(n1, 5);
  17. }
  18.  
  19. public static void breathFirstSearch(GraphNode root, int x){
  20. if(root.val == x)
  21. System.out.println("find in root");
  22.  
  23. Queue queue = new Queue();
  24. root.visited = true;
  25. queue.enqueue(root);
  26.  
  27. while(queue.first != null){
  28. GraphNode c = (GraphNode) queue.dequeue();
  29. for(GraphNode n: c.neighbors){
  30.  
  31. if(!n.visited){
  32. System.out.print(n + " ");
  33. n.visited = true;
  34. if(n.val == x)
  35. System.out.println("Find "+n);
  36. queue.enqueue(n);
  37. }
  38. }
  39. }
  40. }
  41. }
  输出结果:
  • value: 2 value: 3 value: 5 Find value: 5 
  • value: 4
  实际中,基于Graph需要经常用到的算法:

●克隆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:确保开始条件是正确的

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

  递归方法的时间复杂度指数为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)

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

  1. public static int f(int n) {
  2.  
  3. if (n <= 2){
  4. return n;
  5. }
  6.  
  7. int first = 1, second = 2;
  8. int third = 0;
  9.  
  10. for (int i = 3; i <= n; i++) {
  11. third = first + second;
  12. first = second;
  13. second = third;
  14. }
  15.  
  16. return third;
  17. }
  在这个例子中,迭代花费的时间要少些。

7.动态规划

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

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

  上面所列的爬台阶问题完全符合这四个属性,因此,可以使用动态规划来解决:
  1. public static int[] A = new int[100];
  2.  
  3. public static int f3(int n) {
  4. if (n <= 2)
  5. A[n]= n;
  6.  
  7. if(A[n] > 0)
  8. return A[n];
  9. else
  10. A[n] = f3(n-1) + f3(n-2);//store results so only calculate once!
  11. return A[n];
  12. }

  一些基于动态规划的算法:

  • 编辑距离
  • 最长回文子串
  • 单词分割
  • 最大的子数组

8.位操作

  位操作符:

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

  1. public static boolean getBit(int num, int i){
  2. int result = num & (1<
  3.  
  4. if(result == 0){
  5. return false;
  6. }else{
  7. return true;
  8. }
  9. }
例如,获取10的第二位:

  • i=1, n=10
  • 1<<1= 10
  • 1010&10=10
  • 10 is not 0, so return true;
  典型的位算法:
  • Find Single Number
  • Maximum Binary Gap
9.概率

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

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

  算法:

  1. public static double caculateProbability(int n){
  2. double x = 1; 
  3.  
  4. for(int i=0; i
  5. x *=  (365.0-i)/365.0;
  6. }
  7.  
  8. double pro = Math.round((1-x) * 100);
  9. return pro/100;
  10. }
  结果:
  • calculateProbability(50) = 0.97


10.组合和排列

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


  例1:

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

  例2:

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

  基于它们的一些常见算法
  • 排列
  • 排列2
  • 排列顺序
程序开发不可不知的10大算法,你知道几种?

公司推荐

成为一品威客服务商,百万订单等您来有奖注册中

留言( 展开评论

快速发任务

价格是多少?怎样找到合适的人才?

官方顾问免费为您解答