随笔分类 - 基础算法
这里是记录基础算法的学习和收获
【基础算法】- 个人认为最快的 Fibonacci 程序
摘要:public class Fibonacci { private static Map map = new HashMap(); static{ map.put(0L, 1L); map.put(1L, 1L); } public static void main(String[] arg...
阅读全文
【基础算法】- 打印所有子集合
摘要:import java.util.ArrayList;import java.util.List;public class SubSetCount { public static void main(String[] args) { String test = new String("ABCD...
阅读全文
【数据结构】- 二叉树基础操作
摘要:import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.Queue;import java.util.Stack;import java.util.Vector;import java.util.concurrent.LinkedBlockingQueue;//2叉树常用操作练习public class BinaryTreeTest { //TEST public static void main(String[] args) { BinaryTree tre...
阅读全文
【基础算法】- 2路归并排序
摘要:import java.util.Arrays;public class MergeSort { public static void main(String[] args) { MergeSort sort = new MergeSort(); int[] a = {2,4,3,1,0,9,5,6,3,7}; int[] r = sort.sort(a); for(int i = 0 ; i 0 ? a.length - i : b.length - j; boolean sign = a.length - i > 0 ? true : false; if(size > 0...
阅读全文
【基础算法】- 全排列
摘要:public class AllSortAlgorithm { public static void main(String[] args) { char[] array = {'a','b','c'}; allsort(array,0,array.length); } private static void swap(char[] array , int i , int j){ if(i != j){ array[i]^=array[j];array[j]^=array[i];array[i]^=array[j]; } } private st
阅读全文
【基础算法】- 2分查找
摘要:public class BinarySort { public static void main(String[] args) { int[] array = {1,2,4,6,8,12,15,23}; System.out.println(binarysort(array,16)); } private static int binarysort(int[] array,int target){ int low = 0; int high = array.length - 1; while(low <= high){ int middle = ( high +...
阅读全文
【基础算法】- 堆排序
摘要:public class HeapSort { public static void main(String[] args) { HeapSort sort = new HeapSort(); int[] a = {2,4,3,1,0,9,5,6,3,7}; sort.sort(a); for(int i = 0 ; i = 0 ; i--){ maxHeapify(a,i,a.length); } } private void sort(int[] a){ buildMaxHeap(a); for(int i = a.length - 1 ; i >= 0 ; i...
阅读全文
【算法题】- 求和等于K子数组
摘要:一整数(有正有负)数组,用尽量少的时间计算数组中和为某个整数的所有子数组 1 public class SumK { 2 3 public static void main(String[] args) { 4 5 int[] array = {4,5,2,4,7,1,8,-3,6,3,2,6,1,4,-6,7,-4,2,-1,8,5,2,7,4,3}; 6 int k = 11; 7 Map set = new HashMap(); 8 int[] sum = new int[array.length...
阅读全文
【练习题】 青蛙回家问题
摘要:package datastruct;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class DPTest { //动态规划练习题 public static void main(String[] args) { //青蛙回家问题 frogBackHome(); } //青蛙可以一次跳一步二步三步,它不喜欢碰石子,石子在某几点上(会列出N点有石子)问要怎么以碰到最少的石子为代价家对岸 private st...
阅读全文
【模式匹配】— 求比数组所有比自身元素小且在它之前离它最近的元素位置,时间复杂度O(n).
摘要:package basic;public class NextGen { /** * 实际上就是迭代正向子结构Next数组的变形 */ public static void main(String[] args) { int[] c = {13,43,4,65,12,34,12,5,90,123,54,72,99,22,18,7,9,1,58,93}; int[] a = new int[c.length]; a[0] = -1; int i = -1; for(int j = 1 ; j < c.length ; j++){ i = j - 1; if(c[j] >...
阅读全文
【动态规划】— 最长公共子序列
摘要:package base;import java.util.LinkedList;import java.util.List;//O(mn)动态规划求最长公共子序列 public class MaxSeq { private static String str = "aebae"; //为了方便直接用自身和自身的逆序 m=n private int strSize = str.length(); public static void main(String[] args) { MaxSeq pr = new MaxSeq(); ...
阅读全文
【字符串处理】— 最大子序列和
摘要:package base;//最大子序列和,包括全负数情况。 思想是依次遍历数组,如何本次的和大于上次的和 则本次大,并且如果本次和为0 则//开始指针移动到i++位置public class MaxSubSeq { public static void main(String[] args) { MaxSubSeq mss = new MaxSubSeq(); System.out.println("The max is = " + mss.getMaxSubSeq(new int[]{-1,-3,-21,-3,-5,-2})); } ...
阅读全文
【模式匹配】— KMP变形
摘要:求STR中所有的匹配sub位置,和基本KMP略有不同package base;//O(m+n) 时间复杂度 KMP主要是求Next数组 public class KMP { public static int[] next(String sub) { int[] a = new int[sub.length()]; char[] c = sub.toCharArray(); int length=sub.length(); int i,j; a[0] = -1; i = 0; for(j=1;j...
阅读全文
【模式匹配】— KMP算法
摘要:KMP算法的关键是求next数组,在一些问题中会有一些变形。package base;//O(m+n) 时间复杂度 KMP主要是求Next数组 public class KMP { public static int[] next(String sub) { int[] a = new int[sub.length()]; char[] c = sub.toCharArray(); int length=sub.length(); int i,j; a[0] = -1; i = 0; for(j...
阅读全文
【基础排序】— 快速排序
摘要:一些大学学过的基础在这里总结一下package base.sort;import java.util.Arrays;import java.util.Random;//O(nlogn) 快速排序public class QuickSort { public static void main(String[] args) { QuickSort sort = new QuickSort(); int[] a = new int[32]; Random rand = new Random(); for (int i = ...
阅读全文
浙公网安备 33010602011771号