随笔分类 -  基础算法

摘要:1 public class test 2 { 3 public static void main(String[] args) 4 { 5 int[] array = new int[] {13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7}; 6 int max_sum = Inte... 阅读全文
posted @ 2019-05-25 20:48 Huayra 阅读(315) 评论(0) 推荐(0)
摘要:1 //1 使用邻接表 时间复杂度: O(n+e) 2 //递归 3 public void DFS(int v) 4 { 5 System.out.print(this.vexs[v].data + " "); 6 this.visited[v] = true; 7 8 for(ArcNode p = this.vexs[v].firs... 阅读全文
posted @ 2019-05-05 21:40 Huayra 阅读(2870) 评论(0) 推荐(0)
摘要:1 //1 无向图的邻接矩阵实现 时间复杂度:O(n^2) 空间复杂度:O(n^2) 2 class Graph 3 { 4 public int[][] adjacencyMatrix; //邻接矩阵 5 public int vexnum; //边数 6 public int arcnum; //顶点数 7 public char[] ve... 阅读全文
posted @ 2019-05-05 09:30 Huayra 阅读(1072) 评论(0) 推荐(0)
摘要:1 //1 二叉搜索树的判定 2 //method 1 利用中序遍历(最优解) 对于任一结点 若它后一个结点的值大于它的值 则为二叉搜索树 3 //时间复杂度: O(n) 空间复杂度: O(1) 4 public boolean isBST(BiTNode node) 5 { 6 if(node == null) 7 return true; 8 9 ... 阅读全文
posted @ 2019-05-04 20:09 Huayra 阅读(222) 评论(0) 推荐(0)
摘要:1 //1 二叉树的深度(在后序遍历的基础上进行 时间复杂度: O(n)) (即左右子树的最大深度加1) 2 public int getDepth(BiTNode node) 3 { 4 if(node == null) 5 return 0; 6 7 int count1, count2; 8 count1 = getDepth(node.lc... 阅读全文
posted @ 2019-05-04 11:58 Huayra 阅读(322) 评论(0) 推荐(0)
摘要:1 public BiTNode createBiTree(BiTNode parent_node) 2 { 3 Scanner input = new Scanner(System.in); 4 int k = input.nextInt(); 5 if(k == -1) 6 return null; 7 8 BiTNode node ... 阅读全文
posted @ 2019-05-04 11:52 Huayra 阅读(518) 评论(0) 推荐(0)
摘要:1 //二叉树的先序遍历(非递归) 2 public void PreOrderTraverse() 3 { 4 BiTNode p = this.root; 5 Stack stack = new Stack(10000); 6 7 while(!stack.isEmpty || p != null) 8 if(p != null) 9 ... 阅读全文
posted @ 2019-04-22 00:31 Huayra 阅读(230) 评论(0) 推荐(0)
摘要:1 public BiTNode createBiTree() 2 { 3 Scanner input = new Scanner(System.in); 4 int k = input.nextInt(); 5 if(k == -1) 6 return null; 7 8 BiTNode node = new B... 阅读全文
posted @ 2019-04-21 23:11 Huayra 阅读(402) 评论(0) 推荐(0)
摘要:1 class MyQueue 2 { 3 private Stack s1; 4 private Stack s2; 5 6 public MyQueue(int size) 7 { 8 this.s1 = new Stack(size); 9 this.s2 = new Stack(size); 10... 阅读全文
posted @ 2019-04-11 17:08 Huayra 阅读(156) 评论(0) 推荐(0)
摘要:1 class MyQueue 2 { 3 private Stack s1; 4 private Stack s2; 5 6 public MyQueue(int size) 7 { 8 this.s1 = new Stack(size); 9 this.s2 = new Stack(size); 10... 阅读全文
posted @ 2019-04-11 16:56 Huayra 阅读(330) 评论(0) 推荐(0)
摘要:1 class MyStack 2 { 3 private Queue q1; 4 private Queue q2; 5 6 public MyStack(int size) 7 { 8 this.q1 = new Queue(size); 9 this.q2 = new Queue(... 阅读全文
posted @ 2019-04-11 16:12 Huayra 阅读(321) 评论(0) 推荐(0)
摘要:1 class MyStack 2 { 3 private Queue q1; 4 private Queue q2; 5 6 public MyStack(int size) 7 { 8 this.q1 = new Queue(size); 9 this.q2 = new Queue(... 阅读全文
posted @ 2019-04-11 16:03 Huayra 阅读(544) 评论(0) 推荐(0)
摘要:1 class Queue 2 { 3 private int front; 4 private int rear; 5 private int[] a; 6 7 public Queue(int size) 8 { 9 this.front = this.rear = 0; 10 this.... 阅读全文
posted @ 2019-04-11 13:58 Huayra 阅读(122) 评论(0) 推荐(0)
摘要:1 class Stack 2 { 3 private int top; 4 private int[] a; 5 6 public Stack(int size) 7 { 8 this.top = -1; 9 this.a = new int[size]; 10 } 11 12 ... 阅读全文
posted @ 2019-04-11 13:51 Huayra 阅读(300) 评论(0) 推荐(0)
摘要:排序算法的稳定性:若排序后的序列中多个相同元素的相对位置与原序列中相同,即在原序列中 a[i]=a[j] 且 a[i]在a[j]之前,而在排序后的序列中,a[i]仍在a[j]之前,则称该排序算法是稳定的,否则称为不稳定的。 阅读全文
posted @ 2019-04-06 23:36 Huayra 阅读(290) 评论(0) 推荐(0)
摘要:1 //三向切分的快速排序 2 //这种切分方法对于数组中有大量重复元素的情况有比较大的性能提升 3 4 public static void main(String[] args) 5 { 6 Scanner input = new Scanner(System.in); 7 int n = input.nextInt(); 8 int[] a = n... 阅读全文
posted @ 2019-03-29 20:15 Huayra 阅读(304) 评论(0) 推荐(0)
摘要:1 public static void main(String[] args) 2 { 3 Scanner input = new Scanner(System.in); 4 int n = input.nextInt(); 5 int[] a = new int[n]; 6 7 a[0] = 0; //不使用第一个位置 8 for(... 阅读全文
posted @ 2019-03-25 16:52 Huayra 阅读(386) 评论(0) 推荐(0)
摘要:1 public static void main(String[] args) 2 { 3 Scanner input = new Scanner(System.in); 4 int n = input.nextInt(); 5 int[] a = new int[n]; 6 7 ... 阅读全文
posted @ 2019-03-15 22:25 Huayra 阅读(179) 评论(0) 推荐(0)
摘要:1 public static void main(String[] args) 2 { 3 Scanner input = new Scanner(System.in); 4 int n = input.nextInt(); 5 int[] a = new int[n]; 6 7 /... 阅读全文
posted @ 2019-03-11 21:42 Huayra 阅读(286) 评论(0) 推荐(0)
摘要:1 public static void main(String[] args) 2 { 3 Scanner input = new Scanner(System.in); 4 int n = input.nextInt(); 5 int[] a = new int[n]; 6 7 fo... 阅读全文
posted @ 2019-03-10 21:14 Huayra 阅读(330) 评论(0) 推荐(0)