
算法
wzng
再懒也不能停止运动
展开
-
如何把一个生成1-7的随机数的函数re(),重新构造成生成1-10随机数的函数re1()
class Test{ public static int re(){ return new Random().nextInt(7)+1; } public static int re1(){ int i=re()-1; int j=re()-1; i=i*7+j;//i的范围0-48 /...原创 2019-10-24 23:14:58 · 361 阅读 · 0 评论 -
java计算正整数二进制数中的1出现的次数
第一种,通过 Integer.bitCount(index) 方法public int[] countBits(int num) { int[] memo = new int[num + 1]; int index = 0; while (index <= num) { memo[index] = Integer.bitCount(index);...原创 2019-08-12 10:47:24 · 433 阅读 · 0 评论 -
动态规划求解两个字符串的最长公共子序列
最简单的递归便宜方程为LCS(m,n):求s1[0...m] 和s2[0...n]的最长公共子序列changdu偏移状态:s1[m] ==s2[n]?1+LCS(m-1,n-1):max(LCS(m-1,n),LCS(m,n-1))详细代码: public class Wigglemaxlength { private char[] s1; p...原创 2019-08-03 15:57:01 · 1299 阅读 · 0 评论 -
java 基于ArrayList实现一个最大堆结构
public class MaxHeap<E extends Comparable<E>> { private ArrayList<E> data; private MaxHeap(int capacity) { data = new ArrayList<>(capacity); } privat...原创 2019-07-17 10:39:17 · 501 阅读 · 0 评论 -
java的PriorityQueue放入HashMap的键值对,并按照HashMap的value值进行最大优先队列排列
下面的方法是将nums里的元素频率最高的前k个元素存放到List里返回public List<Integer> top(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : nums) { int count = m...原创 2019-07-16 20:33:03 · 4603 阅读 · 1 评论 -
二叉树的前序,中序,后序遍历(迭代)
二叉树类public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}前序:public class Solution { public List<Integer> preorde...原创 2019-07-15 20:10:41 · 181 阅读 · 0 评论 -
二叉树的前序,中序,后序遍历(递归)
二叉树类public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}前序:public class Solution { private List<Integer> list=n...原创 2019-07-15 19:20:53 · 332 阅读 · 0 评论 -
java计算一个已知前序和后序遍历的二叉树中中序遍历有几种可能性
public class TreeTraversal { public int intree(String preTree, String postTree) { if(preTree.length()==1){ return 1; } int count=0; for (int i = 0; ...原创 2019-04-02 15:13:02 · 650 阅读 · 0 评论 -
java快速排序来合并取值区间
现有5个取值区间[1,2],[4,6],[3,6],[9,10],[8,17]将这5个取值区间去掉重复的部分,合并起来为[1,2],[3,6],[8,17]创建区间类@Datapublic class Interval { private Integer left; private Integer right; public Interva...原创 2019-04-03 15:38:49 · 644 阅读 · 0 评论 -
java使用递归实现int数组累加
第一种public class Sum { public static int summ(int[] input) { if (input.length == 0 || input == null) { throw new IndexOutOfBoundsException("IndexOutOfBoundsException "); ...原创 2019-04-19 15:26:23 · 2106 阅读 · 0 评论 -
java实现一个并查集
public class TreeUnionFindPathCompressionRecur { //使用数组构建一棵指向父亲点的树 private int[] parent; //此时的rank并不反映节点的实际深度值 //路径压缩后不需要再维护rank private int[] rank; public TreeUnionFindPath...原创 2019-05-24 17:20:01 · 258 阅读 · 0 评论 -
java对一组不按顺序排列的整数数组,如何当满足等于某个整数时满足条件
int[] ints = new int[]{1, 9, 4, 6, 2, 5};int i=0;int a = ints[i];while (a!=4&&i<ints.length-1){ System.out.println(a); a=ints[i++];}System.out.println(a);ints 是一组不按顺序排列的整数数组...原创 2019-05-27 11:18:19 · 351 阅读 · 0 评论 -
java的双重for循环,如何排除i==j
for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if(i==j){ continue; } System.out.println("i="+i+" j="+j); }}使用continue,当i==j时跳出执行j++...原创 2019-05-29 16:23:58 · 1542 阅读 · 0 评论 -
java创建链表,递归和非递归对比
使用递归的创建class ListNode { int val; ListNode next; ListNode(int x) { val = x; } ListNode(int[] input) { if (input == null || input.length == 0) { ...原创 2019-04-18 14:41:56 · 364 阅读 · 0 评论