自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(47)
  • 资源 (1)
  • 收藏
  • 关注

原创 [leetCode]226周赛

题目一class Solution { public int countBalls(int lowLimit, int highLimit) { int[] counter = new int[50]; int max = 0; for (int i = lowLimit; i <= highLimit; i++) { int num = i; int sum = 0; w

2021-01-31 17:44:24 65

原创 [leetCode]212. 单词搜索 II

题目https://leetcode-cn.com/problems/word-search-ii/字典树思路:使用深度优先搜索,遍历以各个字符开头的字符串,如果字符串存在于words列表中则加入答案。可以发现如果字符串的前缀不存在于字符串列表中,那么就不用继续搜索了,因此可以使用字典树进行优化。class Solution { private List<String> ans = new ArrayList<>(); private int[][]

2021-01-31 14:09:20 77

原创 [leetCode]839. 相似字符串组

题目https://leetcode-cn.com/problems/similar-string-groups/并查集由题目可知,只要两个字符串相似则它们属于同一个连通分量,可以通过并查集维护连通分量的个数,最后返回。class Solution { public int numSimilarGroups(String[] strs) { int n = strs.length; UnionFind uf = new UnionFind(n);

2021-01-31 09:35:49 133

原创 [leetCode]211. 添加与搜索单词 - 数据结构设计

题目https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/前缀树在遇到 “.” 的时候,使用递归方法,将该结点的每一个分支都看过去,只要有一个分支返回 true 就可以了,全部分支都走过去,都没有返回 true 的才返回 false。class WordDictionary { private TrieNode root; class TrieNode { bo

2021-01-30 17:46:49 79

原创 [leetCode]778. 水位上升的泳池中游泳

题目https://leetcode-cn.com/problems/swim-in-rising-water/二分查找如果在某一个时间 t0,可以从左上角到达右下角那么,当 t > t0 时仍然可以从左上角到达右下角,因此可以使用二分查找。根据提示给的范围在[0, N*N - 1]区间内猜一个数,然后进行深度优先搜索,或者广度优先搜索,搜索完毕时根据是否能到达左下角来确定如何缩小区间范围。class Solution { private int[][] dirs = ne

2021-01-30 15:44:05 115

原创 [leetCode]单词替换

题目https://leetcode-cn.com/problems/replace-words/submissions/字典树将词根加入前缀树中, 然后遍历句子中的每一个单词,如果匹配到最短的前缀则替换该单词,否则不用替换。class Solution { public String replaceWords(List<String> dictionary, String sentence) { // 1. 把所有词根加入前缀树 TrieN

2021-01-29 16:40:38 155

原创 [leetCode]425. 单词方块

题目https://leetcode-cn.com/problems/word-squares/字典树 + 回溯class Solution { private List<List<String>> ans = new ArrayList<>(); private List<String> ansBuilder = new ArrayList<>(); public List<List<Strin

2021-01-29 15:26:43 198

原创 [leetCode]最小体力消耗路径

题目https://leetcode-cn.com/problems/path-with-minimum-effort/二分查找题目可以转化为:是否存在一条路径,该路径上的体力值不超过x,可以从左上角到达右下角假设x = x0时存在路径可以从左上角到达右下角,那么当x增大时原来的路径仍然可以使用。因此可以使用二分查找,每次估测一个x,然后进行广度或者深度优先搜索,最后根据能否到达右下角来缩小搜索范围。class Solution { private int[][] dirs =

2021-01-29 11:26:48 291

原创 [leetCode]12. 整数转罗马数字

题目https://leetcode-cn.com/problems/integer-to-roman/贪心算法罗马符号由7个字符构成,每个字符都有自己对应的数值,根据减法规则一共有13个独特的字符。为了表示一个整数尽可能选取大的罗马字符,然后不断减去这个字符值到当前数值小于当前罗马字符的值,不断重复这个过程直到数值为0。class Solution { private int[] values = new int[]{1000, 900, 500, 400, 100, 90, 50,

2021-01-28 13:56:49 105

原创 [leetCode]557. 反转字符串中的单词 III

题目https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/字符串操作 双指针class Solution { public String reverseWords(String s) { char[] s2arr = s.toCharArray(); int start = 0; for (int i = 0; i <= s2arr.length; i++) {

2021-01-28 12:44:46 71

原创 [leetCode]208. 实现 Trie (前缀树)

题目https://leetcode-cn.com/problems/implement-trie-prefix-tree/submissions/字典树class Trie { private TrieNode root; class TrieNode { char c; Map<Character, TrieNode> children = new HashMap<>(); boolean isWord

2021-01-28 10:44:46 84

原创 [leetCode]724. 寻找数组的中心索引

题目https://leetcode-cn.com/problems/find-pivot-index/前缀和使用前缀和来快速计算数组某个范围内的和class Solution { public int pivotIndex(int[] nums) { int n = nums.length; int[] sum = new int[n + 1]; for (int i = 1; i <= n; i++) {

2021-01-28 09:42:55 68

原创 [leetCode]1579. 保证图可完全遍历

题目https://leetcode-cn.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/并查集对于Alice来说当图中只有Alice的独占边和公共边时要求整个图是连通的,也就是说整个图的连通分量只有一个。同理,对于Bob来说当图中只有Bob的独占边和公共边时也要求整个图是连通的。题目要求删除最多的边,也就是要保留最少的边,可以通过给含有n个节点的无向图添加边来满足上面的要求。添加边的策略是优

2021-01-27 16:05:47 84

原创 [leetCode]1128. 等价多米诺骨牌对的数量

题目https://leetcode-cn.com/problems/number-of-equivalent-domino-pairs/计数统计等价的二元对有多少个,把二元对转化为相同的格式,即第一维不大于第二维,由于二元对的元素都不大于9所以,二元对可以转化为一维 即 10 * x + y,这样用一个长度为100的数组即可class Solution { public int numEquivDominoPairs(int[][] dominoes) { int[]

2021-01-26 17:10:51 107

原创 [leetCode]959. 由斜杠划分区域

题目https://leetcode-cn.com/problems/regions-cut-by-slashes/并查集这题的关键点就是如何将每个方格进行划分,以及如何计算每个小方格的坐标,然后根据每个方格的类型在方格内合并,然后再方格之间合并,最后返回连通分量的个数即可class Solution { public int regionsBySlashes(String[] grid) { int N = grid.length; int size

2021-01-25 17:01:20 122

原创 [leetCode]287. 寻找重复数

题目https://leetcode-cn.com/problems/find-the-duplicate-number/solution/二分查找二分要弄清楚找的是什么,然后再确定查找的范围,然后确定如和缩小区间。抽屉原理:桌上有十个苹果,要把这十个苹果放到九个抽屉里,无论怎样放,我们会发现至少会有一个抽屉里面放不少于两个苹果。这题我们要查找的是一个整数,改整数的范围在[1, n],由抽屉原理可知如果数组中的元素小于等于 mid 的个数 严格大于 mid 则重复元素一定在[1,mid]

2021-01-24 21:25:16 86

原创 [leetCode]350. 两个数组的交集 II

题目https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/哈希表使用一个哈希表对一个数组中的元素进行计数,为了降低时间复杂度选择较短的数组进行计数,计数完成后遍历另一个数组,如果哈希表中存在当前元素则加入答案,并使该元素的数量减1class Solution { public int[] intersect(int[] nums1, int[] nums2) { int len1 = nums1.l

2021-01-24 20:30:12 75

原创 [leetCode]154. 寻找旋转排序数组中的最小值 II

题目https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/二分查找解题关键:通过nums[mid] 与 nums[hi] 的大小关系来缩小搜索区间class Solution { public int findMin(int[] nums) { // 确定搜索范围[lo, hi] int lo = 0; int hi = nums.length -

2021-01-24 15:13:57 70

原创 [leetCode]674. 最长连续递增序列

题目https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/动态规化要注意题目中的连续二词,使用cur变量维护当前连续子序列的长度,maxLen维护最长连续子序列的长度,如果当前连续子序列是递增的则使cur++并更新maxLen,如果连续子序列递减了则重新将cur置为一。class Solution { public int findLengthOfLCIS(int[] nums) {

2021-01-24 09:42:53 93

原创 [leetCode]50. Pow(x, n)

题目https://leetcode-cn.com/problems/powx-n/快速幂 递归比如说210=25∗25,25=22∗22∗2,22=2∗22^{10} = 2^5 * 2^5,2^5=2^2*2^2*2,2^2=2*2210=25∗25,25=22∗22∗2,22=2∗2class Solution { public double myPow(double x, int n) { if (n == 0 || x == 1) re

2021-01-23 21:33:03 78

原创 [leetCode]702. 搜索长度未知的有序数组

题目https://leetcode-cn.com/problems/search-in-a-sorted-array-of-unknown-size/二分查找先确定右边界,为了保持对数时间复杂度需要成倍的增加right再使用二分查找查找目标值class Solution { public int search(ArrayReader reader, int target) { int left = 0, right = 1; while (rea

2021-01-23 19:30:38 193

原创 [leetCode]270. 最接近的二叉搜索树值

题目https://leetcode-cn.com/problems/closest-binary-search-tree-value/二分查找根据root值选选择在左子树中查找还是在右子树中查找class Solution { public int closestValue(TreeNode root, double target) { int closest = root.val; while (root != null) {

2021-01-23 19:15:49 213

原创 [leetCode]658. 找到 K 个最接近的元素

题目https://leetcode-cn.com/problems/find-k-closest-elements/双指针数组是排序的,使用双指针指向数组两端,如果一端离x更远就排除该指针指向的元素,如果一样远,根据题意排除右指针指向的元素class Solution { public List<Integer> findClosestElements(int[] arr, int k, int x) { int left = 0, right = arr

2021-01-23 16:08:02 97

原创 [leetCode]162. 寻找峰值

题目https://leetcode-cn.com/problems/find-peak-element/二分查找通过中间值nums[mid] 与nums[mid + 1]来缩小搜索区间class Solution { public int findPeakElement(int[] nums) { int lo = 0, hi = nums.length - 1; while (lo < hi) { int mid = l

2021-01-23 13:12:17 63

原创 [leetCode]1319. 连通网络的操作次数

题目https://leetcode-cn.com/problems/number-of-operations-to-make-network-connected/solution/lian-tong-wang-luo-de-cao-zuo-ci-shu-by-leetcode-s/解法连接n台电脑则至少需要n - 1条边,如果边数小于n - 1 则直接返回-1。如果m条边组成的图中恰好有一个连通分量说明n台电脑是连通的,如果有多个连通分量则说明边集中有些边是多余的,由于边集大于等于n-1,需要

2021-01-23 10:11:02 100

原创 [leetCode]989. 数组形式的整数加法

题目https://leetcode-cn.com/problems/add-to-array-form-of-integer/逐位相加取出K的最后一位与A的最后一位相加,如果发生进位则将进位加在K上class Solution { public List<Integer> addToArrayForm(int[] A, int K) { List<Integer> ans = new ArrayList<>(); i

2021-01-22 10:34:14 71

原创 [leetCode]1489. 找到最小生成树里的关键边和伪关键边

题目https://leetcode-cn.com/problems/find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/Kruscal算法先用Kruscal算法计算其中一棵最小生成树的权值v,使用并查集维护连通分量。然后遍历每一条边,去除该边重新生成最小生成树,如果最后连通分量不为1或者权值nv > v那么这条边就是关键边,然后continue。判断是否是伪关键边可以先将当前遍历的边使用并查集进行合并,然后

2021-01-21 10:39:56 296

原创 [leetCode]912.排序数组

题目https://leetcode-cn.com/problems/sort-an-array/快速排序class Solution { public int[] sortArray(int[] nums) { sort(nums, 0, nums.length - 1); return nums; } private void sort(int[] nums, int l, int r) { if (l >= r

2021-01-20 11:07:14 83

原创 [leetCode]33. 搜索旋转排序数组

题目https://leetcode-cn.com/problems/search-in-rotated-sorted-array/二分查找二分查找的最终目的是要减半搜索,这题通过判断左右端点的值可以判断哪一部分是有序的。如果target在有序部分则在有序部分查找,否则在无序部分查找。class Solution { public int search(int[] nums, int target) { return searchHelper(nums, target,

2021-01-20 09:05:49 70

原创 [leetCode]1584. 连接所有点的最小费用

题目https://leetcode-cn.com/problems/min-cost-to-connect-all-points/Kruscal算法最小生成树:图的生成树是他的一棵含有所有顶点的无环连通子图。一幅加权图的最小生成树(MST)是它的一棵权值最小的生成树。题目可以转换为求最小生成树的权值总和Kruscal算法流程:将图G={V,E}G = \{V, E\}G={V,E} 中所有的边按照从小到达排序,等长的边任意排序初始化图G′为{V,、∅}G'为 \{V, 、\v

2021-01-19 09:30:56 175

原创 时间复杂度与空间复杂度

思维导图

2021-01-18 12:50:47 72

原创 [leetCode]721. 账户合并

题目https://leetcode-cn.com/problems/accounts-merge/并查集账户之间只要有一个邮箱相同那么就是同一个账户,也就是同属一个连通分量因此可以使用并查集。由于并查集底层使用了数组,需要知道一共有多少个不同的邮箱,因此可以使用哈希表给每个邮箱编号,并使用哈希表记录每个邮箱对应的姓名。遍历所有账户,对每个账户的邮箱进行合并就能知道合并完成后一共有几个账户。查询每个邮箱对应哪一个账户(连通分量)将其添加到该账户的列表下,然后将每个列表排序后,使用某个邮箱查询对应

2021-01-18 09:57:06 145

原创 [leetCode]1232. 缀点成线

题目https://leetcode-cn.com/problems/check-if-it-is-a-straight-line/submissions/数学以第一个点P0为参照将所有的点平移(-P0x,-P0y),那么直线过原点,Ax + By = 0,A = P1y, B = -P1x, 遍历之后的所有点代入直线公式即可class Solution { public boolean checkStraightLine(int[][] coordinates) {

2021-01-17 09:30:51 135

原创 [leetCode]947. 移除最多的同行或同列石头

题目https://leetcode-cn.com/problems/most-stones-removed-with-same-row-or-column/并查集移除石头的过程其实就是有个逆向深度优先搜索或者广度优先搜索的过程。石子的横坐标相等或纵坐标相等相当于在石子之间形成了边。同一个连通分量的石子一定能移除到只剩下一块,所以题目可以转化为求连通分量的个数,而题目给的输入数组是石子的坐标,因此可以考虑使用并查集。并查集中「合并」的语义是:所有横坐标为 x 的石头和所有纵坐标为 y 的石头

2021-01-15 10:28:24 153

原创 [leetCode]96. 不同的二叉搜索树

题目https://leetcode-cn.com/problems/unique-binary-search-trees/动态规化令G(n)为n个节点能够组成二叉搜索树的数量,F(j,n):以j为根节点长度为n的二叉搜索树的数量。 G(n) = F(1, n) + ... + F(j, n) +...+ F(n, n ), F(j, n) = G(j - 1) * G(n - j) 所以G(n) = G(1 - 1) * G(n - 1) +...+G(j - 1) * G(n - j) +.

2021-01-14 10:48:10 64

原创 [leetCode]1018. 可被 5 整除的二进制前缀

题目https://leetcode-cn.com/problems/binary-prefix-divisible-by-5/模拟i > 1时N_i = N_i-1 * 2 + A[i],由于A很长所以需要注意溢出的情况,N_i = (N_i-1 * 2 + A[i])mod 5class Solution { public List<Boolean> prefixesDivBy5(int[] A) { int n = A.length;

2021-01-14 09:57:40 114

原创 [leetCode]1203. 项目管理

题目https://leetcode-cn.com/problems/sort-items-by-groups-respecting-dependencies/拓扑排序每个项目之间存在执行的先后顺序,因此可以对项目拓扑排序得到一个列表。 项目与组是一对多的关系也可以对组进行一个拓扑排序的到组的先后顺序。由项目的拓扑排序可以得到组与项目的一对多的关系,将组的拓扑排序替换为项目的拓扑排序即可得到结果。class Solution { public int[] sortItems(int n

2021-01-13 15:08:06 132

原创 [leetCode]684. 冗余连接

题目https://leetcode-cn.com/problems/redundant-connection/并查集在一棵树中,边的数量比树的结点少一个。这道题中的图在树的基础上多了一条附加的边,因此边的数量也是 N。使用并查集来查找附加的边。一开始所有结点都属于各自独立的一个连通分量,遍历每一条边,如果两个结点属于不同的连通分量则将两个结点合并,合并的两个结点已经属于同一个连通分量则说明这条边是附加的。class Solution { private int[] parent;

2021-01-13 12:15:35 118

原创 [leetCode]1202. 交换字符串中的元素

题目https://leetcode-cn.com/problems/smallest-string-with-swaps/并查集  分析示例可以发现索引交换具有传递性,如果两个索引对出现公共索引,那么索引对中的下标可以任意交换  将可任意交换次序的部分按照字典序升序排序,得到的字符串的字典序就是最小的;问题可以转化为下标连通分量的问题。  先根据索引对集合将索引合并,这样可任意交换的索引同处一个连通分量。由于字符串每个索引位置的字符都应取该索引对应连通分量中的字典序的最小值,因

2021-01-11 11:06:51 286

原创 [leetCode]228. 汇总区间

题目https://leetcode-cn.com/problems/summary-ranges/solution/hui-zong-qu-jian-by-leetcode-solution-6zrs/双指针需要注意溢出的情况,因此不能做减法判断相邻数的增量是否为1class Solution { public List<String> summaryRanges(int[] nums) { int n = nums.length; Lis

2021-01-10 09:41:53 76

ListViewTest2.zip

在自定义Adapter中 - 重写getItemViewType()方法用来判断当前Item对象的类别 - 重写getViewTypeCount()返回item一共有几个类别 - 在geView方法中通过判断类别来填充不同的item布局并给控件设置值

2019-08-22

空空如也

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

TA关注的人

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