自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(119)
  • 收藏
  • 关注

转载 Java8新特性——Stream

什么是Stream?Stream(流)是一个来自数据源的元素队列并支持聚合操作元素是特定类型的对象,形成一个队列。 Java中的Stream并不会存储元素,而是按需计算。数据源 流的来源。 可以是集合,数组,I/O channel, 产生器generator 等。 聚合操作 类似SQL语句一样的操作, 比如filter, map, reduce, find, match, sorted等。和以前的Collection操作不同, Stream操作还有两个基础的特征:Pipelin

2021-02-24 14:30:16 164

原创 a15 三数之和 垃圾解法

public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new LinkedList<>(); if (nums.length <3)return res; Arrays.sort(nums); Integer preI=null ,preJ=null,preX=null;

2021-02-21 10:51:15 184

原创 leetcode 257 二叉树的所有路径

List<String> res = new LinkedList<>(); public List<String> binaryTreePaths(TreeNode root) { dfs(root,""); return res; } void dfs(TreeNode root,String path){ if (root != null ) { StringBui..

2021-02-01 19:53:40 161

原创 为什么要用红黑树

什么是红黑树

2021-01-28 22:18:34 397

原创 数据库的事务隔离级别 知识总结

事务的四大特性?学习数据库的时候常常会接触到事务, ACID等概念,那么到底什么是数据库的事务,数据库事务又具有哪些特点,和ACID有怎样的关系,事务的隔离级别又是做什么的呢?。事务及其四大特性?事务(Transaction):访问并可能更新数据库中各种数据项的一个程序执行单元(unit),它通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用户程序的执行所引起。当在数据库中更改数据成功时,在事务中更改的数据便会提交,不再改变。否则,事务就取消或者回滚,更改无效。举个例子来说,张

2021-01-28 20:29:46 126

原创 数据库的索引 看这一篇就够了

深入浅出数据库索引数据库索引原理深入数据库索引1. 什么是数据库索引?MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构。数据库查询是数据库的最主要功能之一,我们都希望查询数据的速度能尽可能的快,因此数据库系统的设计者会从查询算法的角度进行优化。????:也可以这样理解**:**索引的作用就相当于目录的作用。打个比方: 我们在查字典的时候,如果没有目录,那我们就只能一页一页的去找我们需要查的那个字,速度很慢。如果有目录了,我们只需要先去目录里查找字的位置,然

2021-01-24 17:34:47 1008

原创 Final修饰的String真的不能修改吗

通过反射可以修改public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { final String rs = "aaaaaa"; Field value = rs.getClass().getDeclaredField("value"); value.setAccessible(true); char[] item

2021-01-24 10:48:52 610 1

原创 Java创建线程的几种方式

第一种:继承Thread首先继承Thread方法,重写Thread的run()方法在main()方法里创建一个MyThread对象,调用该对象的start()方法,start()方法会通过对系统底层的一系列操作,创建出一个相应的线程,与当前线程并发执行。如果直接调用run()方法,程序将执行完run()方法后才会执行main()方法中后面的代码,这样就是单线程执行而不是多线程并发执行了。public class MyThread extends Thread { @Override p

2021-01-23 09:15:53 173

原创 Java中各种数据结构的常用方法和遍历方式,用处

List????:ArrayList ArrayList<Integer> list = new ArrayList<>(); System.out.println("常用方法============"); list.clear();//清空ArrayList list.add(1);//添加元素 list.add(2); list.add(3); System.out.

2021-01-20 20:12:24 255 2

原创 leetcode 1609 奇偶树

public boolean isEvenOddTree(TreeNode root) { if (root == null ) return true; LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(root); boolean res = true; boolean level = true; flag :...

2021-01-03 20:47:13 171

原创 AQS和ReentrantLock的关系

ReentrantLock主要由li大师编写的可重入锁,可以说当时是为了替代synchronized而诞生的,因为synchronized当时是一个重量级锁,消耗性能,可能造成性能的急剧降低,随着后来synchronized的不断的优化,性能逐渐超过ReentrantLock,而ReentrantLock,ReentrantReadWriteLock等锁底层都是基于AQS来实现的。lock.unlock() 需要写在finally块中。synchronized是可以作用与方法,类和代码块,是一个自动加锁

2021-01-03 18:17:46 730 1

原创 2021-01-01 ThreadLocal

不能轻易去锁,导致线程排队,变慢了,导致down机@RestControllerpublic class StatController { static Integer c= 0; synchronized void _add() throws InterruptedException { Thread.sleep(100); c++; } @RequestMapping("/stat") public Integer stat()

2021-01-02 22:37:32 99 1

原创 leetcode 0403 特定深度节点链表

给定一棵二叉树,设计一个算法,创建含有某一深度上所有节点的链表(比如,若一棵树的深度为 D,则会创建出 D 个链表)。返回一个包含所有深度的链表的数组。 public ListNode[] listOfDepth(TreeNode tree) {//整体思路是层序遍历 int flag =0 ; ListNode[] res = new ListNode[dfs(tree)]; if (tree == null) return res;

2021-01-02 14:37:58 110

原创 leetcode 0405 检查二叉搜索树

Integer res = null; boolean flag =true; public boolean isValidBST(TreeNode root) { if (root == null) return true; isValidBST(root.left); if (res == null) res = root.val; else if (root.val <= res) flag = false;

2021-01-02 14:31:53 52

原创 leetcode 0408 首个共同祖先

TreeNode res = null;//超时 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; if (dfs(root,p) && dfs(root,q)){ if ( (dfs(root.left,p) || dfs(root.left,q)) &&a

2021-01-02 13:56:51 68

原创 leetcode 0410 检查子树

检查子树。你有两棵非常大的二叉树:T1,有几万个节点;T2,有几万个节点。设计一个算法,判断 T2 是否为 T1 的子树。如果 T1 有这么一个节点 n,其子树与 T2 一模一样,则 T2 为 T1 的子树,也就是说,从节点 n 处把树砍断,得到的树与 T2 完全相同。boolean flag = false; public boolean checkSubTree(TreeNode t1, TreeNode t2) { if (t1 ==null && t2 =

2021-01-02 10:36:54 70

原创 leetcode 0412 求和路径

路径只能是从根节点指向叶子节点的方向public int pathSum(TreeNode root, int sum) { if (root == null) return 0; return dfs(root,sum) + pathSum(root.left,sum) + pathSum(root.right,sum); } int dfs(TreeNode root,int sum){ if (root == null) retur

2021-01-02 09:44:54 81

原创 leetcode 671 二叉树中第二大的数

int res =-1; public int findSecondMinimumValue(TreeNode root) { if (root == null) { return res; } //如果存在子树并且值不相等,那么较大的值就有可能是第二小的 if (root.left != null && root.left.val != root.right.val) { //获取左右子树中将较

2021-01-01 18:38:53 140

原创 leetcode 100 相同的树

public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; else if (p == null || q == null) return false; else if (p.val != q.val) return false; else return isSameTree(p.

2021-01-01 18:20:10 68

原创 leetcode 101 对称二叉树

public boolean isSymmetric(TreeNode root) { if (root == null) return true; return check(root.left,root.right); } boolean check(TreeNode left,TreeNode right){ if (left == null && right == null) return true; i

2021-01-01 18:19:41 85

原创 leetcode 104 二叉树的最大深度

public int maxDepth(TreeNode root) { if (root == null) return 0; if (root.left == null && root.right == null){ return 1; } return Math.max(maxDepth(root.left)+1,maxDepth(root.right)+1); }

2021-01-01 17:49:44 70

原创 leetcode 107 二叉树的层序遍历

public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> res = new LinkedList<>(); if (root == null) return res; LinkedList<TreeNode> queue = new LinkedList&lt...

2021-01-01 16:57:52 49

原创 leetcode 108 将有序的数组转换成二叉搜索树

public TreeNode sortedArrayToBST(int[] nums) { if (Objects.isNull(nums) || nums.length ==0 )return null; int length = nums.length; TreeNode root = new TreeNode(nums[(length-1)/2]); System.out.println(root.val); roo.

2021-01-01 16:50:36 65

原创 leetcode 111 二叉树的最小深度

public int minDepth(TreeNode root) { return dfs(root); } int dfs(TreeNode root ){ if (root == null) return 0; if (root.left == null && root.right == null){ return 1; } if (root.left !=...

2021-01-01 15:52:49 60

原创 leetcode 112 路径总和

public boolean hasPathSum(TreeNode root, int sum) { if (root == null){ return false; } if (root.left == null && root.right == null){ return sum == root.val; } return hasPathSum(roo...

2021-01-01 15:26:04 51

原创 leetcode 226 翻转二叉树

public TreeNode invertTree(TreeNode root) { if (root == null) return null; LinkedList<TreeNode> queue = new LinkedList<>(); queue.addFirst(root); TreeNode t = null; while (!queue.isEmpty()){ T

2021-01-01 14:58:23 48

原创 leetcode 235二叉搜索树的最近公共祖先

TreeNode head ; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; if (q.val < root.val && p.val < root.val){ return lowestCommonAncestor(root.left,...

2020-12-31 16:32:59 50

原创 leetcode 404 左叶子之和

计算给定二叉树的所有左叶子之和。public int sumOfLeftLeaves(TreeNode root) { int num=0; LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()){ TreeNode node = queue.poll();

2020-12-30 16:45:49 49

原创 leetcode 501 二叉搜索树的众数

给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。 Map<Integer,Integer> map = new HashMap<>(); public int[] findMode(TreeNode root) { dfs(root); int num =0; List<Integer> res = new ArrayList<>(); f

2020-12-30 16:13:27 58

原创 leetcode 530 二叉搜索树的最小绝对差

给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。int res = Integer.MAX_VALUE; TreeNode pre; public int getMinimumDifference(TreeNode root) { dfs(root); return res; } void dfs(TreeNode root){ if (root == null) return;

2020-12-30 16:12:29 57

原创 leetcode 543 二叉树的直径

给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。int res=0; public int diameterOfBinaryTree(TreeNode root) { depth(root); return res; } int depth(TreeNode root){ if (root == null)return 0; int L

2020-12-30 13:23:22 57

原创 leetcode 563二叉树的坡度

给定一个二叉树,计算 整个树 的坡度 。一个树的 节点的坡度 定义即为,该节点左子树的节点之和和右子树节点之和的 差的绝对值 。如果没有左子树的话,左子树的节点之和为 0 ;没有右子树的话也是一样。空结点的坡度是 0 。整个树 的坡度就是其所有节点的坡度之和 public int findTilt(TreeNode root) { if (root == null) return 0; LinkedList<TreeNode> queue = new

2020-12-30 11:39:25 92

原创 leetcode 572 另一个树的子树

给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树 public boolean isSubtree(TreeNode s, TreeNode t) { LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(s); while (!queue.

2020-12-30 11:31:01 75

原创 leetcode 606 根据二叉树创建字符串

你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。空节点则用一对空括号 “()” 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/construct-string-from-binary-tree著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。public String tree2str(TreeNode t) {

2020-12-30 10:36:36 65

原创 leetcode 617 合并二叉树

给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点 public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if (t1 == null){ return t2; } if (t2 ==

2020-12-30 09:51:45 62

原创 leetcode 637 二叉树的层平均值

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。 public List<Double> averageOfLevels(TreeNode root) { List<TreeNode> queue = new LinkedList<>(); List<Double> res = new LinkedList<>(); queue.add(root); while (!

2020-12-30 09:39:21 55

原创 leetcode 653两数之和

public boolean findTarget(TreeNode root, int k) { List<Integer> arr = new ArrayList<>(); Map<Integer,Integer> map = new HashMap<>(); dfs(root,arr); int len = arr.size(); ...

2020-12-30 09:12:18 55

原创 leetcode 589 N叉树的前序遍历

public List<Integer> preorder(Node root) { LinkedList<Node> queue = new LinkedList<>(); LinkedList<Integer> res = new LinkedList<>(); if (root == null) return res; queue.add(root); w...

2020-12-28 12:51:37 46

原创 leetcode 590 N叉树的后序遍历

给定一个 N 叉树,返回其节点值的后序遍历List<Integer> res = new LinkedList<>(); public List<Integer> postorder(Node root) { dfs(root); return res; } void dfs(Node root){ if (root == null) return; for

2020-12-28 11:13:28 57

原创 leetcode 700 二叉搜索树中的搜索

给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。 public TreeNode searchBST(TreeNode root, int val) { if (root == null || root.val == val) return root; return val < root.val ? searchBST(root.left,val):searchBS

2020-12-23 17:29:34 63

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除