算法
Keepingrun
这个作者很懒,什么都没留下…
展开
-
Leetcode134
思路:遍历数组,遇到gas[i] >= cost[i] 时,尝试以i节点作为起始点,sumGas记录总油量,到达一个站点,加上gas[i]油量,并减去将要耗费的油量cost[i], 如果sumGas小于0,说明到达不了下一站。public static int canCompleteCircuit(int[] gas, int[] cost) { // 记录遍历的节点个数 int count = 0; int len = gas.length;原创 2021-07-05 14:32:17 · 109 阅读 · 0 评论 -
树、森林与二叉树的转换
原文链接:https://blog.csdn.net/leolinsheng/article/details/1174...转载 2019-08-20 21:09:26 · 205 阅读 · 0 评论 -
类的加载
笔记1.JVM和类当调用java命令运行java程序时,该命令将会启动一个java虚拟机进程,同一个JVM的所有线程、所有变量都处于同一个进程里,都使用该JVM进程的内存区。案例// A.javapublic class A { public static int a=6;} //ATest1.javapublic class ATest1 { public stati...原创 2019-03-08 14:58:08 · 72 阅读 · 0 评论 -
整数拆分
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。(假设 n 不小于 2 且不大于 58。)解:尽量把数拆成3,拆解到只剩2或3或4,则不用再拆解了。比如7=3+4;10=3+3+4class Solution { public int integerBreak(int n) { if(n==2) { return 1; } if(n==3) {原创 2020-10-12 23:13:50 · 120 阅读 · 0 评论 -
反转链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode reverseList...原创 2019-11-23 16:52:12 · 85 阅读 · 0 评论 -
判断数组内的元素累加是否有aim值
public static boolean isSum(int[] arr,int aim,int i,int sum) { if(i==arr.length) { return sum==aim; } return isSum(arr,aim,i+1,sum+arr[i])||isSum(arr,aim,i+1,sum); } public static void main...转载 2019-04-04 22:08:48 · 178 阅读 · 0 评论 -
前缀树
public class Prim { public static class TrieNode{ public int path;//所有字符串经过该节点的次数 public int end;//以该节点结束的字符串 public TrieNode[] nexts;//比如只对字符串的问题,这里表示每个节点有26种方式延伸出去a-z public TrieNode() {...转载 2019-03-30 21:52:44 · 114 阅读 · 0 评论 -
完全二叉树的节点数
public int nodeNum(Node head) { if(head==null) return 0; return bs(head, 1, mostLeftLevel(head, 1));//第三个参数表示总深度 } public static int bs(Node head,int level,int h) { if(level==h) r...转载 2019-03-23 15:24:47 · 494 阅读 · 0 评论 -
判断平衡二叉树、搜索二叉树、完全二叉树
public static int isBalance(Node head) { if(head==null) return 0; int left=isBalance(head.left)+1; int right=isBalance(head.right)+1; if(Math.abs(left-right)>1||left==-1||right==-1) r...原创 2019-03-23 13:46:01 · 136 阅读 · 0 评论 -
母牛的数量
题目:public static int cowNumber(int n) { if(n<1) return 0; if(n==1||n==2||n==3) return n; int pre1=3; int pre2=2; int pre3=1; int res=0; for(int i=4;i<=n;i++) { res=pre3...转载 2019-04-03 23:39:27 · 396 阅读 · 0 评论 -
打印一个字符串的子序列(子序列包括空串)
递归方法,当递归到str.length时停止递归,并打印。public static void printAllSub(char[] str,int i,String res) { if(i==str.length) { System.out.println(res); return; } printAllSub(str, i+1, res);//忽略当前字符 pr...转载 2019-04-03 22:45:05 · 363 阅读 · 0 评论 -
后继节点和前驱节点
后继节点:就是中序遍历排序中相对的后一个每次找后继节点都是先找当前节点的右子树中的最左节点如果没有右子树,则找当前节点的父节点,并且当前节点是父节点的左子树的节点。红圈代表的是当前节点,箭头代表的后继节点,也就是中序遍历中相对当前节点的下一个。public static class Node{ public int value; public Node left; publi...原创 2019-03-23 12:22:37 · 13677 阅读 · 1 评论 -
汉诺塔(递归)
1.把左边的N-1块放在中间2.再把第N块直接挪到右边3.把N-1块借助左边,来挪到右边public static void process(int N,String from,String to,String help) { if(N==1) { System.out.println("move 1 from"+from+" to"+to); }else { proce...转载 2019-04-03 22:21:33 · 82 阅读 · 0 评论 -
KMP算法(笔记)
转载自http://www.ruanyifeng.com/blog/2013/05/Knuth–Morris–Pratt_algorithm.html例如求"ABCDAB"的匹配值1.获取匹配表public static void getMatchTable(int[] nextArr,String str) { if(str==null) return; nextA...原创 2019-04-07 11:54:26 · 92 阅读 · 0 评论 -
字符串的统计字符串
题目描述给定一个字符串str,返回str的统计字符串。例如“aaabbbbcccd”的统计字符串为“a_3_b_4_c_3_d_1”。import java.util.Scanner;public class Main { public static StringBuilder getResult(String str) { StringBuilder resul...原创 2019-08-27 15:40:53 · 1198 阅读 · 0 评论 -
螺旋矩阵
class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> result = new ArrayList<Integer>(); if (matrix == null || matrix.length == 0) ...转载 2019-06-21 23:19:12 · 77 阅读 · 0 评论 -
BFPRT(无序数组中找到第k大的数,或者第k小的数)时间复杂度O(N)(学习笔记)
分组组内排序(每5个一组,组内排序,组间不排序)中位数拿出组成N/5大小的新数组(new_arr)递归调用bfprt(new_arr,new_arr.length/2);,拿到返回值num根据num值划分左中右,来找是否命中第k小的数...原创 2019-04-15 13:02:17 · 1861 阅读 · 0 评论 -
最多的宣讲场次
一些项目要占用一个会议室宣讲,会议室不能同时容纳两个项目的宣讲。 给你每一个项目开始的时间和结束的时间(给你一个数组,里面 是一个个具体的项目),你来安排宣讲的日程,要求会议室进行 的宣讲的场次最多。返回这个最多的宣讲场次。(哪个会议的结束时间最早,就先进行哪个会议)public static class Program{ public int start; public int en...转载 2019-04-01 23:10:45 · 457 阅读 · 0 评论 -
获得的最大钱数
参数1正数数组costs, 参数2正数数组profits ,参数3正数k, 参数4正数Wcosts[i]表示i号项目的花费 profits[i]表示i号项目在扣除花费之后还能挣到的钱(利润) k表示你不能并行、只能串行的最多做k个项目 W表示你初始的资金说明:你每做完一个项目,马上获得的收益,可以支持你去做下一个 项目。输出: 你最后获得的最大钱数。通过一个小根堆和一个大根堆来实现,...转载 2019-04-01 22:30:27 · 176 阅读 · 0 评论 -
Manacher算法
主要分两种情况.情况一:i在R外,暴力扩i在R内,又可以分三种情况,如下(1)这样i的回文半径和i`的一样(2)这样i的回文半径为R-i(3)这样i的回文半径需要暴力扩展才知道public class Manacher { //得到manacher字符串 public static char[] manacherString(String str) { char[] ...转载 2019-04-07 17:49:59 · 616 阅读 · 0 评论 -
金块j分割的最小代价
题目:一块金条切成两半,是需要花费和长度数值一样的铜板的。比如长度为20的 金条,不管切成长度多大的两半,都要花费20个铜板。一群人想整分整块金 条,怎么分最省铜板?例如,给定数组{10,20,30},代表一共三个人,整块金条长度为10+20+30=60. 金条要分成10,20,30三个部分。 如果, 先把长度60的金条分成10和50,花费60 再把长度50的金条分成20和30,花费50 一共花...原创 2019-04-01 21:51:31 · 356 阅读 · 0 评论 -
并查集
public class UnionFind { public static class Node{ int value; } public static class UnionFindSet{ public HashMap<Node, Node> fatherMap; public HashMap<Node, Integer> sizeMap; ...转载 2019-03-26 13:35:35 · 82 阅读 · 0 评论 -
比较器
比较器主要用于对对象的排序,比如一个对象里面有好几个属性,应该按什么属性规律来排序呢?这就要实现Comparator&amp;lt; E &amp;gt;接口public class compare { public static class Student{ String name; int id; int age; public Student(String name,int id,in...转载 2019-03-15 15:08:03 · 200 阅读 · 0 评论 -
链表回文
链表结构public static class Node{ int value; Node next; public Node(int value) { this.value=value; } }方法一:使用一个栈,通过遍历链表存入栈中,栈在弹出的过程中就是链表的逆序,辅助空间o(n) public static boolean isPalindromeLis...原创 2019-03-19 20:29:57 · 163 阅读 · 0 评论 -
小和问题
笔记两个数组再合并的过程中就会产生小和public static int small_Sum(int[] arr) { if(arr==null||arr.length<2) return 0; return mergeSort1(arr,0,arr.length-1); } public static int mergeSort1(int[] arr,int L,i...转载 2019-03-10 23:47:44 · 120 阅读 · 0 评论 -
归并排序
public class Msort { public static void mergeSort(int[] arr) { if(arr==null||arr.length&amp;lt;2) return; sortProcess(arr,0,arr.length-1); } public static void sortProcess(int[] arr,int L,in...转载 2019-03-10 23:05:11 · 72 阅读 · 0 评论 -
大根堆
大根堆调整,具体看代码和注释。public class HeadS { public static void heapSort(int[] arr)//大根堆 { if(arr==null||arr.length&amp;lt;2) return; for(int i=0;i&amp;lt;arr.length;i++)//把数组的每个元素插入堆中 { heapInsert...转载 2019-03-14 17:10:42 · 1543 阅读 · 0 评论 -
复杂度
转载 2019-03-10 15:30:47 · 94 阅读 · 0 评论 -
对数器
import java.util.*;public class LogArithm {//生成长度随机、大小随机的随机数组 public static int[] getRandomArray(int size,int value) { int[] arr=new int[(int)((size+1)*Math.random())];//(size+1)*Math.Random()--...转载 2019-03-10 13:57:04 · 59 阅读 · 0 评论 -
冒泡、选择、插入
先讲一下异或交换两个值:如a=1101 b=1011则异或运算有: 1101^1011--------- 0110 (该值包括的是[只有a拥有的+只有b拥有的],也就是中间的两个的11,左边一个是只有a拥有的,右边一个是b拥有的)a=a^b;//[只有a拥有的+只有b拥有的]b=a^b;//[只有a拥有的+只有b拥有的]^[只有b拥有的+ab共同拥有的]=[只有a拥有的+ab...转载 2019-03-10 12:00:28 · 175 阅读 · 0 评论 -
字符串中数字子串的求和
1.忽略小数点,如’a1.3’ 输出42.“- -3” 偶数个’-’,后面又是数字则代表4,如果是’-3’则代表-3.如’‘Ab-33b- -8’’,输出-25分析:字符串里的字符要么是位于‘0’到‘9’之间,要么是‘-’,要么是其他。private static int getStr_sum(String str) { int AllSum=0,sum=0;//AllSum是最终...原创 2019-03-09 16:47:03 · 345 阅读 · 0 评论 -
二叉树遍历(非递归与递归)
用递归方法能解决的问题,都能用非递归解决,因为递归利用的原理是函数栈来保存信息,我们可以通过数据结构的栈来实现。(以下分析,自己画图效果最佳)Node.javapublic class Node { public int value; public Node left; public Node right; public Node(int value) { this....原创 2019-03-08 20:39:01 · 100 阅读 · 0 评论 -
荷兰国旗问题(链表)
用三个桶,每个桶包括(链表的头和链表的尾),三个桶分别是Less, Equal, More.遍历一次链表把数值放入相应的桶。public class Test_Demo { public static class Node{ int value; Node next; public Node(int value) { this.value=value; } ...原创 2019-03-19 21:55:12 · 381 阅读 · 0 评论 -
相邻两数的最大差值
问题:给定一个数组,求如果排序之后,相邻两数的最大差值,要求时间复杂度O(N),且要求不能用非基于比较的排序。分析:如果数组有N个数,则用N+1个桶,加入有数组{5,78,40,30,95},N+1个桶的目的是排除最大差值是在同一个桶中产生,n+1个桶的话至少有一个空桶,但不能说明最大差值位于空桶的左右两个非空桶的之间的差值。最大差值:相邻两个非空桶,前一个桶的max和后一个桶的min相差...转载 2019-06-24 18:35:34 · 289 阅读 · 0 评论 -
荷兰国旗问题
给定一个数组和一个num数,把小于num的放在数组的左边,等于num的放中间,大于num的放右边。给两个变量,一个(less)指向小于数组的最后一个数,另一个(more)指向大于数组中的第一个数,1.当cur小于num时,cur指向的就和++less指向的交换,同时cur++;2.当cur==num时,cur++;3.当cur大于num时,cur指向的就和–more指向的交换。最后数组返...转载 2019-03-11 21:36:49 · 82 阅读 · 0 评论 -
Z字形打印矩阵
打印例子如下,我们设置两个点分别是A、B。每次A往绿色箭头移动一步,B往蓝色箭头移动一步,所要打印的就是A、B连线之间的元素,但是要区分下方向(斜向上或者斜向下),打印结果为 1,2,5, 9 ,6, 3, 4 ,7, 1 0, 11, 8, 12public class PrintZMatrix { public static void PrintZ(in...原创 2019-03-19 11:07:04 · 1001 阅读 · 0 评论 -
转圈打印矩阵
public class PrintMatrix { public static void PrintMatrixNumber(int[][] arr) { if(arr==null) return; int x1=0; int x2=arr.length-1; int y1=0; int y2=arr[0].length-1; int x=0,y=0; w...原创 2019-03-18 21:39:15 · 121 阅读 · 0 评论 -
猫狗队列
有如下类public static class Pet{ private String type; public Pet(String type) { this.type=type; } public String getType() { return type; } } public static class Cat extends Pet{ pub...转载 2019-03-18 20:20:51 · 117 阅读 · 0 评论 -
单链表中环的入口
public static Node EntryOfLoop(Node head) { if(head==null||head.next==null||head.next.next==null) return null; Node fast=head.next.next; Node slow=head.next; while(fast!=slow) { if(fa...原创 2019-03-20 17:54:43 · 117 阅读 · 0 评论 -
复制含有随机指针节点的链表
做算法题最好就是动笔画,能更加深入理解。【题目】 一种特殊的链表节点类描述如下:public class Node { public int value; public Node next; publicNode rand;public Node(int data) { this.value = data; }}Node类中的value是节点值,next指针和正常单链表中next指针的...原创 2019-03-20 16:36:14 · 104 阅读 · 0 评论