LeetCode百题【最长连续序列】
题目描述给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
示例 1:
123输入:nums = [100,4,200,1,3,2]输出:4解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
示例 2:
12输入:nums = [0,3,7,2,5,8,4,6,0,1]输出:9
提示:
0 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
解法
哈希表
将数组中的每个数存进哈希表
取的时候检查是否含有下一个数
注意剪枝,如果含有num-1,说明当前的num不是开始的元素
12345678910111213141516171819202122232425262728class Solution { public int longestConsecutive(int[] nums) { if (nums.length==0) ...
SpringBoot学习
HelloSpirngBoot
导入依赖
123456789101112<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version></parent><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>
创建主程序
12345678910import org.springframework.boot.SpringApplication ...
LeetCode百题【从前序与中序遍历序列构造二叉树】
题目描述给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的 先序遍历 , inorder 是同一棵树的 中序遍历 ,请构造二叉树并返回其根节点。
示例 1:
12输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]输出: [3,9,20,null,null,15,7]
示例 2:
12输入: preorder = [-1], inorder = [-1]输出: [-1]
提示:
1 <= preorder.length <= 3000
inorder.length == preorder.length
-3000 <= preorder[i], inorder[i] <= 3000
preorder 和 inorder 均 无重复 元素
inorder 均出现在 preorder
preorder 保证 为二叉树的前序遍历序列
inorder 保证 为二叉树的中序遍历序列
解法
对于任意一颗树而言,前序遍历的形式总是
[ 根节点, ...
LeetCode百题【二叉树的层序遍历】
题目描述给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例 1:
12输入:root = [3,9,20,null,null,15,7]输出:[[3],[9,20],[15,7]]
示例 2:
12输入:root = [1]输出:[[1]]
示例 3:
12输入:root = []输出:[]
提示:
树中节点数目在范围 [0, 2000] 内
-1000 <= Node.val <= 1000
解法
dfs
通过标识节点层级,将同一层级的节点加入即可
12345678910111213141516171819202122232425262728class Solution { List<List<Integer>> res=new ArrayList<>(); public List<List<Integer>> levelOrder(TreeNode root) { if (r ...
LeetCode百题【验证二叉搜索树】
题目描述给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
示例 1:
12输入:root = [2,1,3]输出:true
示例 2:
123输入:root = [5,1,4,null,null,3,6]输出:false解释:根节点的值是 5 ,但是右子节点的值是 4 。
提示:
树中节点数目范围在[1, 10^4] 内
-2^31 <= Node.val <= 2^31 - 1
解法
递归
对于一个二叉树节点来说
它大于它的左子树的所有节点
它小于它右子树的所有节点
也就是说当前节点的值是左子树的一个上届
也就是说当前节点的值是右子树的一个下届
12345678910111213141516171819202122232425262728class Solution { public boolean isValidBST(Tre ...
LeetCode百题【单词搜索】
题目描述给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例 1:
12输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"输出:true
示例 2:
12输入:board = [["A","B","C","E"],["S","F",&quo ...
Redis6学习
NoSQL简介什么是NoSQL?
NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库。
NoSQL 不依赖业务逻辑方式存储,而以简单的key-value模式存储。因此大大的增加了数据库的扩展能力。
不遵循SQL标准。
不支持ACID。
远超于SQL的性能。
NoSQL适用场景
对数据高并发的读写
海量数据的读写
对数据高可扩展性的
NoSQL不适用场景
需要事务支持
基于sql的结构化查询存储,处理复杂的关系,需要即席查询。
常见的NoSQL
Memcache
很早出现的NoSql数据库
数据都在内存中,一般不持久化
支持简单的key-value模式,支持类型单一
一般是作为缓存数据库辅助持久化的数据库
Redis
几乎覆盖了Memcached的绝大部分功能
数据都在内存中,支持持久化,主要用作备份恢复
除了支持简单的key-value模式,还支持多种数据结构的存储,比如 list、set、hash、zset等。
一般是作为缓存数据库辅助持久化的数据库
MongoDB
高性能、开源 ...
LeetCode百题【子集】
题目描述给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
12输入:nums = [1,2,3]输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:
12输入:nums = [0]输出:[[],[0]]
提示:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums 中的所有元素 互不相同
解法
递归
包含当前数的子集组合=上一个数的获得的每一个子集组合+当前数
123456789101112131415161718class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result=new ArrayList ...
LeetCode百题【颜色分类】
题目描述给定一个包含红色、白色和蓝色、共 n 个元素的数组 nums , 原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
必须在不使用库的sort函数的情况下解决这个问题。
示例 1:
12输入:nums = [2,0,2,1,1,0]输出:[0,0,1,1,2,2]
示例 2:
12输入:nums = [2,0,1]输出:[0,1,2]
提示:
n == nums.length
1 <= n <= 300
nums[i] 为 0、1 或 2
进阶:
你可以不使用代码库中的排序函数来解决这道题吗?
你能想出一个仅使用常数空间的一趟扫描算法吗?
解法
快排中的partition
简单来讲就是将区间分为三个区间
all in [0,p0)==0
all in [p0,i)==1
all in (p2,n-1]==2
123456789101112131415161718192021222324 ...
LeetCode百题【合并区间】
题目描述以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。
示例 1:
123输入:intervals = [[1,3],[2,6],[8,10],[15,18]]输出:[[1,6],[8,10],[15,18]]解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
123输入:intervals = [[1,4],[4,5]]输出:[[1,5]]解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。
提示:
1 <= intervals.length <= 10^4
intervals[i].length == 2
0 <= starti <= endi <= 10^4
解法
排序
按照区间的左端点排序,那么在排完序的列表中,可以合并的区间一定是连续的。
如果当前区间的左端点在数组 merged 中最后一个区间的右端点之后,那么它们 ...