- 博客(323)
- 收藏
- 关注
原创 NC97 字符串出现次数的TopK问题
import java.util.*;public class Solution {/*** return topK string* @param strings string字符串一维数组 strings* @param k int整型 the k* @return string字符串二维数组*/ public String[][] topKstrings (String[] strings, int k) { // write code here i..
2021-09-11 10:32:34
270
原创 最大子矩阵和
package com.leetcode;import java.util.Scanner;public class Main11 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int matrix[][]={{0,-2,-7,0 },{9 ,2 ,-6 ,2},{-4,1,-4,1},{-1,8,0,-2}}; maxSum(matr
2021-09-11 09:57:34
337
原创 验证回文字符串2
class Solution { public boolean validPalindrome(String s) { int left = 0; int right = s.length() - 1; while(left < right) { char l = s.charAt(left); char r = s.charAt(right); if(..
2021-08-08 20:17:35
360
原创 JZ49 把字符串转换成整数-字符串
import java.lang.Math;public class Solution { public int StrToInt(String str) { if(str == null || str.length() == 0) { return 0; } boolean flag = true;//true表示+ false表示- //字符串首位是正 if(str.charAt(0) .
2021-08-07 23:23:23
168
原创 JZ34 第一个只出现一次的字符-字符串
public class Solution { public int FirstNotRepeatingChar(String str) { if(str == null || str.length() == 0) { return -1; } int[] count = new int[1000]; for(int i = 0; i < str.length(); i.
2021-08-07 21:02:34
121
原创 JZ2 替换空格-字符串
一、利用StringBuffer或者StringBuilderimport java.util.*;public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ public String replaceSpace (String s) { .
2021-08-07 17:06:59
128
原创 JZ51 构建乘积数组-数组
令B[i] = l[i] * r[i];l[i] = l[i - 1] * A[i -1]r[i] = A[i + 1] * r[i + 1]import java.util.ArrayList;public class Solution { public int[] multiply(int[] A) { int[] B = new int[A.length]; if(A == null || A.length == 0) { ..
2021-08-07 16:50:01
112
原创 JZ42 和为S的两个数字-数组
一、方法双指针对于一个顺序的数组,两端乘积小,越往中间乘积越大import java.util.ArrayList;public class Solution { public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) { ArrayList<Integer> list = new ArrayList<>(); if(array == null |.
2021-08-07 13:11:43
248
原创 springboot自动装配原理
主启动类所在包下的类容都可以被扫描到比如主程序所在包是boot包,boot包下的类容都可以被扫描到,扫描的前提需要把注解也给加上比如你在boot包外面加了一个类worldApplication,springboot是扫描不到的,加了注解也没用,必须要在boot包下才能扫描的到如果你把想要扫描的类放在任意包下,可以做一下配置而且这些bean都是默认单例的...
2021-07-24 23:41:47
135
原创 JZ31 整数中1出现的次数
public class Solution { public int NumberOf1Between1AndN_Solution(int n){ //base case if(n<1){ return 0; }// if(n<10){// return 1;// } String s=String.valueOf(n); int h.
2021-07-07 00:11:57
88
原创 JZ30 连续子数组的最大和
动态规划dp[i] = dp[i - 1] + array[i] 或者dp[i] = array[i]public class Solution { public int FindGreatestSumOfSubArray(int[] array) { if(array == null || array.length == 0) { return 0; } int[] dp = new int[array.
2021-07-06 00:09:25
78
原创 JZ28 数组中出现次数超过一半的数字
哈希表法import java.util.HashMap;public class Solution { public int MoreThanHalfNum_Solution(int [] array) { if(array == null || array.length == 0) { return 0; } HashMap<Integer, Integer> map = new Hash.
2021-07-04 11:52:05
162
原创 JZ26 二叉搜索树与双向链表
排序二叉树用中序遍历,先来个中序遍历的模板public class Solution { public TreeNode Convert(TreeNode pRootOfTree) { if(pRootOfTree == null) { return null; } Convert(pRootOfTree.left); (这里写对应的操作) Convert(pRootOfTr.
2021-07-04 10:37:16
109
原创 spring事务机制
就是说方法A上有事务注解,方法B上有事务注解,方法A调方法B,这个时候不会方法A开启个事务然后方法B再开启一个事务,而是方法A开启事务后,方法B加入方法A的事务,方法B不会重新开启事务(采用的是默认的事务传播行为)...
2021-07-02 00:33:10
112
原创 mysql意向锁
考虑这个例子:事务A锁住了表中的一行,让这一行只能读,不能写。之后,事务B申请整个表的写锁。如果事务B申请成功,那么理论上它就能修改表中的任意一行,这与A持有的行锁是冲突的。数据库需要避免这种冲突,就是说要让B的申请被阻塞,直到A释放了行锁。数据库要怎么判断这个冲突呢?step1:判断表是否已被其他事务用表锁锁表step2:判断表中的每一行是否已被行锁锁住。注意step2,这样的判断方法效率实在不高,因为需要遍历整个表。于是就有了意向锁。在意向锁存在的情况下,事务A必须先申请表的意向共享锁,成功后再.
2021-06-28 13:50:54
1027
原创 mysql索引B+树
如果非主键索引保存记录,那么非主键一多,记录行也越多,浪费空间,而且进行修改的时候这些记录都会变化,每一个都要修改,如果只保存一份,就不用修改那么多。...
2021-06-27 23:08:21
91
原创 mysql事务隔离级别
一、未提交读在一个事务里,事务a读取了事务b修改了但未提交的记录,只要b修改了,不管b有没有提交事务,a都可以读到最新值二、以提交读在一个事务里,事务a读取了事务b修改了的记录,只有b将事务提交了,a才能读取到最新值,否则是旧值三、可重复读在一个事务里,事务a读取了事务b修改了的记录,即使事务b提交了事务,a还是读取的老数据,读取的数据没变...
2021-06-26 15:27:25
87
原创 MySQL事务的实现
undo和redo日志归属于innodb存储引擎,binlog是归属于mysql的server层的数据从用户态内存空间到内核空间最后到磁盘,一般是数据存到磁盘达到一个阈值,才会批量的异写磁盘,如果没有写磁盘,断电了,会导致数据的丢失...
2021-06-25 09:00:47
77
原创 复杂链表的复制
维护一个map里面有一个映射关系,新链表和老链表节点的一一映射的关系/*public class RandomListNode { int label; RandomListNode next = null; RandomListNode random = null; RandomListNode(int label) { this.label = label; }}*/import java.util.*;public class S.
2021-06-06 09:25:52
61
原创 二叉树中和为某一值的路径
解析:这题是个二叉树问题,需要从上到下遍历每个节点,所以可以采用前序遍历,先可以写个框架出来public void help(TreeNode root,int target) { path.add(root.val); if(root.left != null) { help(root.left, target); } if(root.right != nul.
2021-06-06 00:15:10
58
原创 二叉搜索树的后序遍历序列
import java.lang.*;import java.util.*;public class Solution { public boolean VerifySquenceOfBST(int [] sequence) { if(sequence == null || sequence.length == 0) { return false; } return help(sequence, 0, sequence.l.
2021-06-05 23:02:25
58
原创 从上往下打印二叉树
层序遍历import java.util.*;/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { public ArrayList<Integer> Pri.
2021-06-05 17:40:15
55
原创 栈的压入、弹出序列
创建一个辅助栈,想让数组中的元素一个个进栈,每进去一个就和出栈数组的元素进行对比,相等就出栈import java.util.*;public class Solution { public boolean IsPopOrder(int [] pushA,int [] popA) { Stack<Integer> stack = new Stack<>(); int j = 0; for(int i = 0; i <.
2021-06-05 17:31:18
54
原创 包含min函数的栈
import java.util.Stack;public class Solution { //stack1用来保存普通元素 Stack<Integer> stack1 = new Stack<>(); //stack2用来保存最小的元素,放进去的元素只能比下面的小 Stack<Integer> stack2 = new Stack<>(); public void push(int node) { .
2021-06-05 16:45:33
60
原创 顺时针打印矩阵
import java.util.ArrayList;public class Solution { public ArrayList<Integer> printMatrix(int [][] matrix) { ArrayList<Integer> list = new ArrayList<>(); if(matrix == null || matrix.length == 0) { return li.
2021-06-05 16:19:58
66
原创 一些常用的算法模板总结
树的层序遍历BFSpublic static void treeBFS(TreeNode root) { //如果为空直接返回 if (root == null) return; //队列 Queue<TreeNode> queue = new LinkedList<>(); //首先把根节点加入到队列中 queue.add(root); //如果队列不为空就继续循环 while (!queue.isE
2021-06-04 22:54:07
114
原创 二叉树的镜像
import java.util.*;/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请.
2021-06-04 21:33:32
60
原创 树的子结构
/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) { .
2021-06-04 21:26:31
58
原创 合并两个排序的链表
方法一 递归/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode Merge(ListNode list1,ListNode list2) { if(list1 == null) { .
2021-06-04 21:01:25
64
原创 反转链表问题
方法一 递归/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode ReverseList(ListNode head) { if(head == null || head.next == null) { .
2021-06-04 00:46:03
93
原创 链表中倒数第k个结点
方法一 双指针import java.util.*;/* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 *.
2021-06-04 00:38:45
73
原创 调整数组顺序使奇数位于偶数前面
方法一 两个队列,一个放奇数,一个放偶数import java.util.*;public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型一维数组 * @return int整型一维数组 */ public int[] reOrderArray (int[] array) { //.
2021-06-03 23:55:46
123
原创 数值的整数次方
方法一 递归public class Solution { public double Power(double base, int exponent) { if(base == 0 && exponent != 0) { return 0.0; } if(base != 0 && exponent == 0) { return 1.0; .
2021-06-03 22:21:41
57
原创 二进制中1的个数
采用移位的操作,这里采用移动mark,因为移动n要移动很多位数,效率太低了public class Solution { public int NumberOf1(int n) { int ans = 0; int mark = 1; while(mark != 0) { if((n & mark) != 0) { ans++; } ma.
2021-06-02 23:53:11
72
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人