自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

寒泉

一只野生程序猿

  • 博客(1308)
  • 资源 (1)
  • 收藏
  • 关注

原创 leetcode 769. Max Chunks To Make Sorted | 769. 最多能完成排序的块(Java)

题目https://leetcode.com/problems/max-chunks-to-make-sorted/题解尽可能切成多份,使得上下的 set 包含相同的值。用 diff 记录当前所有不在正确位置的数字个数。然后类似水平触发,每次 diff=0 的时候,结果 +1.class Solution { public int maxChunksToSorted(int[] arr) { int[] count = new int[arr.length];

2021-12-03 08:45:09 38746

原创 leetcode 767. Reorganize String | 767. 重构字符串(贪心+分桶+26路归并)

题目https://leetcode.com/problems/reorganize-string/题解分桶策略,和两个月之前做的 621.Task Scheduler 类似。两个月之前看的答案还有印象,如果是三个月之前,就说不准了。。class Solution { public String reorganizeString(String s) { int L = s.length(); int[][] count = new int[26][2]

2021-12-03 00:11:26 39601

原创 PacificA: Replication in Log-Based Distributed Storage Systems 论文理解

PacificA: Replication in Log-Based Distributed Storage Systems 论文理解思考:论文有个结论说,相比 GFS 具有中心化的实体,PacificA 没有单点问题(指单点不可靠+单点有请求数量瓶颈)。它的配置中心不是单点的吗?回答:PacificA 只有在拓扑结构发生改变的时候,才会和配置中心进行交互,普通的请求是不会打到 configuration manager 的。思考:普通的请求为什么不会打到 configuration manager?

2021-12-01 13:25:57 36273 1

原创 leetcode 763. Partition Labels | 763. 划分字母区间(双指针)

题目https://leetcode.com/problems/partition-labels/题解将问题转化成线段分割问题:找到所有可以切的点,使得每一个线段都不会被切到class Solution { public List<Integer> partitionLabels(String s) { int[][] range = new int[26][2]; for (int i = 0; i < 26; i++) {

2021-12-01 09:32:05 43611

原创 leetcode 756. Pyramid Transition Matrix | 756. 金字塔转换矩阵(BFS)

题目https://leetcode.com/problems/pyramid-transition-matrix/题解BFS,把 pattern 用 map 存起来,然后 bfs 从下向上一层一层尝试每个 pattern 是否可行。注意状态的转移。class Solution { int N; Map<String, List<Character>> map; public boolean pyramidTransition(String bo

2021-11-30 11:47:52 43482

原创 leetcode 754. Reach a Number | 754. 到达终点数字(数学问题)

题目https://leetcode.com/problems/reach-a-number/题解一看数据规模,再看踩的数量,觉得这题不一般。。只能想出暴力解,不用试了,肯定超时。没有想到如何 dp(dp 即便可以,O(n) 应该也会超时)看了 答案,这是个数学问题。时隔两天,重新理了下思路,然后实现了一下:class Solution { public int reachNumber(int target) { int diff = -Math.abs(targ

2021-11-29 10:30:01 40564

原创 leetcode 797. All Paths From Source to Target | 797. 所有可能的路径(回溯法)

题目https://leetcode.com/problems/all-paths-from-source-to-target/题解回溯,中规中矩,直接上代码。class Solution { int N; public List<List<Integer>> allPathsSourceTarget(int[][] graph) { N = graph.length; boolean[][] g = new boolean

2021-11-28 10:55:00 39407

原创 leetcode 752. Open the Lock | 752. 打开转盘锁(BFS)

题目https://leetcode.com/problems/open-the-lock/题解先写了个 DFS,超时了。看了下 Related Topics,提示说应该是 BFS。这已经不是第一次遇到 BFS 可以、DFS 超时的题目了。总结了一下,对于这种可以用 DFS/BFS 求最短路径的题目,优先选择 BFS,因为你到达目标的可能性有超级多种,而大多数路线都走了很多弯路,去遍历它们都是无用功。用 BFS 的话,从 step=1 开始逐渐递增,在 step 受限的情况下,只要到达了目标,当

2021-11-27 18:48:44 38140

原创 leetcode 743. Network Delay Time | 743. 网络延迟时间(邻接矩阵,Dijkstra 算法)

题目https://leetcode.com/problems/network-delay-time/题解有向图,求源点到所有顶点的最短距离,经典 Dijkstra 算法,只要知道思路就能实现,然而每次思路都要重新查一遍,过几个月又忘了。。Dijkstra 算法1)Dijkstra 算法必须指定一个源点2)生成一个源点到各个点的最小距离表,一开始只有一条记录,即原点到自己的最小距离为0,源点到其他所有点的最小距离都为正无穷大3)从距离表中拿出没拿过记录里的最小记录,通过这个点发出的边,更新源

2021-11-26 10:44:40 42623

原创 leetcode 738. Monotone Increasing Digits | 738. 单调递增的数字(Java)

题目https://leetcode.com/problems/monotone-increasing-digits/题解先把 num 的每一位转化为数组。从左往右找第一个 前>后 的位置,记为a[i]。指针从 i 位置开始,回退到 a[i-1]==a[i] 的第一个位置。新的位置仍记为 i。最后,将 a[i]–,i 以后的位置全置为 9,生成新的数组即为所求。class Solution { public int monotoneIncreasingDigits(int n

2021-11-25 10:37:50 38061

原创 leetcode 986. Interval List Intersections | 986. 区间列表的交集(双指针)

题目https://leetcode.com/problems/interval-list-intersections/题解本题用双指针。(想了下,也可以用线段树,和天际线那道题类似)class Solution { public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { int i = 0; int j = 0; List<int[]&g

2021-11-24 08:41:46 38544

原创 leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)

题目https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/题解待优化:可以用 map 存一下每一个前序遍历元素对应的下标,这样更快一些。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNod

2021-11-21 10:56:31 40983

原创 leetcode 218. The Skyline Problem | 218. 天际线问题(线段树)

题目https://leetcode-cn.com/problems/the-skyline-problem/题解线段树问题,根据左神的思路改编,外加我想到的压缩的 tricks(数字范围太大,实在想不出别的办法了,一开始是直接把过大的数除以某个 scale,但是这样是有损的,无法复原,于是想到了用 map 存压缩前后的映射关系。只要保持压缩前后的数字大小顺序不变,这个压缩就是无损的。)class Solution { public static final int N = 1 <

2021-11-21 01:10:42 38492

原创 leetcode 729, 731, 732. My Calendar I, II, III | 729. 我的日程安排表 I, II, III(线段树)

题目https://leetcode.com/problems/my-calendar-i/题解看了左神课之后,自己实现了下改造后的线段树(非常不优雅),因为数组放不下,所以自建 Node 结构。class Node { int l; int r; boolean used; // 当前范围已全覆盖 Node left; Node right; public Node(int l, int r) { this.l = l;

2021-11-20 13:36:13 40072

原创 leetcode 722. Remove Comments | 722. 删除注释(Java)

题目题解测试用例太恶心了,受不鸟啊class Solution { public static final char NO_NEW_LINE = '\''; public List<String> removeComments(String[] source) { boolean pending = false; List<String> result = new ArrayList<>(); fo

2021-11-19 11:03:48 41431

原创 leetcode 721. Accounts Merge | 721. 账户合并(HashMap版并查集)

题目https://leetcode.com/problems/accounts-merge/题解HashMap 版的并查集。参考了:leetcode 684. Redundant Connection | 684. 冗余连接(并查集)class Solution { public List<List<String>> accountsMerge(List<List<String>> accounts) { Map<St

2021-11-18 10:58:35 40146

原创 leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)

题目https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/题解遍历数组,将数组中每个数字 n 作为下标,将数组中对应位置的符号位置 1。最后遍历修改后的数组,如果符号位是 0 的话,说明该下标没有出现过。class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { List<Inte

2021-11-18 08:50:52 37584

原创 leetcode 720. Longest Word in Dictionary | 720. 词典中最长的单词(Trie前缀树)

题目https://leetcode.com/problems/longest-word-in-dictionary/题解建立一个 Trie,在 insert 的过程中,除最后一个节点外,如果一路上每一个节点都 isEnd,则将其返回值 diff 设置为 false,表明它的差异可以被容忍,可参与最后的长度比较。class Node { Node[] map; boolean isEnd; public Node() { this.map = new N

2021-11-17 16:22:30 40431

原创 leetcode 740. Delete and Earn | 740. 删除并获得点数(暴力递归->傻缓存->DP)

题目https://leetcode.com/problems/delete-and-earn/题解建立 help 数组,相当于一个(正向)索引表。先排序,因为删除的顺序不影响最优结果(实际上是影响结果的,只不过最优解一定是从小到大删除的,因为如果先删除中间的元素的话,它的两边可能被误伤,而如果从边缘开始删除的话,只能误伤到一侧。)class Solution { public int deleteAndEarn(int[] nums) { Map<Integer,

2021-11-16 22:01:11 41135

原创 leetcode 668. Kth Smallest Number in Multiplication Table | 668. 乘法表中第k小的数(二分查找)

题目https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/题解看了答案Approach #3: Binary Search [Accepted]class Solution { public int findKthNumber(int m, int n, int k) { int L = 1; int R = m * n; // 二分找不小于目标

2021-11-16 09:48:44 38684

原创 leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

题目https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/题解经典的 暴力递归 -> 傻缓存 -> DP,重要思路来源于之前做过的 leetcode 309. Best Time to Buy and Sell Stock with Cooldown | 309. 最佳买卖股票时机含冷冻期(动态规划)当年看答案也摸不清门路的 dp,现在应不是玄学了,哈哈。将「买入」和「卖出

2021-11-15 11:17:43 39234

原创 leetcode 712. Minimum ASCII Delete Sum for Two Strings | 712. 两个字符串的最小ASCII删除和(暴力递归->傻缓存->DP)

题目https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/题解经典的 暴力递归 -> 傻缓存 -> DP实际上是找两个字符串的 最大ASCII码和 的公共子序列,然后用总的 ASCII 之和减一下就行了。可以参考两个字符串的最长公共子序列,只不过取 max 的依据不同,一个是 ASCII 码相加,一个是取长度。class Solution { public int minimumDelet

2021-11-12 11:05:31 40842

原创 leetcode 698. Partition to K Equal Sum Subsets | 698. 划分为k个相等的子集(回溯法)

题目https://leetcode.com/problems/partition-to-k-equal-sum-subsets/题解一上来以为是 dp(想到了左神讲的,将一个数组分成两个尽可能相等的部分那道题),再一想,原来是 backtracing。class Solution { public boolean canPartitionKSubsets(int[] nums, int k) { int sum = 0; for (int n : nums

2021-11-11 10:27:08 40914

原创 leetcode 695. Max Area of Island | 695. 岛屿的最大面积(DFS)

题目https://leetcode.com/problems/max-area-of-island/题解class Solution { int M, N; public int maxAreaOfIsland(int[][] grid) { M = grid.length; N = grid[0].length; boolean[][] visited = new boolean[M][N]; int result

2021-11-10 16:22:56 41788

原创 leetcode 1178. Number of Valid Words for Each Puzzle | 1178. 猜字谜(bitmask位运算)

题目https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/题解看了答案,堪称力扣最详细的答案,从时间复杂度的角度分析本题解法:https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/solution/其中,有一个重要的 trick(不是很懂,但很实用)Here we find another challenge: How to ite

2021-11-09 21:14:47 35875

原创 leetcode 43. Multiply Strings | 43. 字符串相乘(Java)

题目https://leetcode.com/problems/multiply-strings/题解模拟手动乘法,列竖式class Solution { public String multiply(String num1, String num2) { // guarantee len(num1) > len(num2) if (num1.length() < num2.length()) { String t = n

2021-11-08 10:39:24 39631

原创 leetcode 688. Knight Probability in Chessboard | 688. “马”在棋盘上的概率(dp,记忆化搜索)

题目https://leetcode.com/problems/knight-probability-in-chessboard/题解左神讲过类似问题:给定5个参数,N,M,row,col,k表示在NM的区域上,醉汉Bob初始在(row,col)位置Bob一共要迈出k步,且每步都会等概率向上下左右四个方向走一个单位任何时候Bob只要离开NM的区域,就直接死亡返回k步之后,Bob还在N*M的区域的概率对于本题来说,k步之后留在棋盘上的概率 = 留在棋盘上的方法数/总方法数class

2021-11-08 10:34:35 40387

原创 leetcode 687. Longest Univalue Path | 687. 最长同值路径(树形dp)

题目https://leetcode.com/problems/longest-univalue-path/题解:树形 dp 套路实现1:带有 Info 类的树形 dp/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(

2021-11-06 13:48:11 36665

原创 leetcode 239. Sliding Window Maximum | 239. 滑动窗口最大值(单调栈,窗口内最大最小值更新结构)

题目https://leetcode.com/problems/sliding-window-maximum/题解窗口内最大最小值更新结构,单调栈问题,左神视频讲过,《程序员算法面试指南》也有此题目。本题的关键在于利用双端队列来实现窗口最大值的更新。class Solution { public int[] maxSlidingWindow(int[] nums, int k) { Deque<Integer> valueQueue = new Linked

2021-11-04 14:36:47 36007

原创 APT: Package ‘vnc4server‘ has no installation candidate 排查过程及解决方法

找不到 vnc4server想安装 vnc4server,于是 sudo apt-get insall vnc4server,然而找不到包,说 Package ‘vnc4server‘ has no installation candidate,apt-cache search vnc4server 也搜不到这个包。解决方法参考:Package 'vncviewer ’ has no installation candidateAll this means is that there is no

2021-11-04 09:43:46 47053

原创 leetcode 686. Repeated String Match | 686. 重复叠加字符串匹配(KMP)

题目https://leetcode.com/problems/repeated-string-match/题解套了 KMP 模板,O(n) 复杂度。分析如下。class Solution { public int repeatedStringMatch(String a, String b) { int repeatedTimes = Math.max((int) Math.ceil((float) 2 * b.length() / a.length()), 2);

2021-11-03 09:11:10 44247

原创 Gitee ssh 公钥配置好后,仍然 permission denied 的排查过程及解决方法

突如其来今天 git pull 一个老项目,之前一直提交的好好的,这次突然报错 git@gitee.com: Permission denied (publickey).,明明是我自己的 repo,居然告诉我没有权限??无脑尝试一开始以为是本地 id_rsa.pub 变更导致 gitee 上原有的记录失效,于是 ssh-keygen 命令重新生成了下,贴到 gitee 上,还是老样子。然后我怀疑我在折腾的时候改乱了 git 的配置,又重新 pacman -S git 强制 reinstall 了下

2021-11-02 21:27:45 50378 13

原创 leetcode 62, 63, 980. Unique Paths I, II, III | 62, 63, 980. 不同路径 I, II, III(暴力递归->傻缓存->动态规划)

62. Unique Pathshttps://leetcode.com/problems/unique-paths/注意本题只能向右 / 向上走。DP 问题,经典又熟悉。暴力递归->傻缓存->动态规划。class Solution { int M, N; public int uniquePaths(int m, int n) { M = m; N = n; // Approach 1: Recursive, Bru

2021-11-02 10:11:40 42096

原创 leetcode 130. Surrounded Regions | 130. 被围绕的区域(DFS递归“感染“思路)

题目https://leetcode.com/problems/surrounded-regions/题解Related Topics 说是并查集问题,然而我并没有用到。带有 visited 数组的 DFS 时间复杂度应该是 O(M*N),因为每个坐标只被 visit 过 2 遍。有点像围棋,只要我是活的,那么我能伸展到的位置就都是活的。最初始的时候,有哪些是活的呢?那就是所有边沿上的 O。让它们去 DFS 做伸展,所以这是一个感染的过程。或者可以这样想,只有能和上下左右边缘 接壤 的 ‘O

2021-11-01 12:04:20 42504

原创 leetcode 684. Redundant Connection | 684. 冗余连接(并查集)

题目https://leetcode.com/problems/redundant-connection/题解并查集问题1)有若干个样本a、b、c、d…类型假设是V2)在并查集中一开始认为每个样本都在单独的集合里3)用户可以在任何时候调用如下两个方法:​ boolean isSameSet(V x, V y) : 查询样本x和样本y是否属于一个集合​ void union(V x, V y) : 把x和y各自所在集合的所有样本合并成一个集合4)isSameSet和union方

2021-11-01 10:18:21 42868

原创 leetcode 678. Valid Parenthesis String | 678. 有效的括号字符串(带缓存的暴力递归)

题目https://leetcode.com/problems/valid-parenthesis-string/题解带缓存的暴力递归,非常挫。用一个 string 模拟 stack,方便缓存记录状态。class Solution { public boolean checkValidString(String s) { StringBuilder stack = new StringBuilder(); Map<String, Boolean>

2021-10-31 23:40:46 42389

原创 leetcode 1044. Longest Duplicate Substring | 1044. 最长重复子串(Rabin Karp算法)

题目https://leetcode.com/problems/longest-duplicate-substring/题解这题暴力超时,看了 Related Topics,以及 Hint,主要用到 Rolling Hash,算法叫Rabin Karp,实现上参考:https://leetcode.com/problems/longest-duplicate-substring/discuss/1132925/JAVA-Solutionclass Solution { public sta

2021-10-30 23:59:20 41115

原创 leetcode 994. Rotting Oranges | 994. 腐烂的橘子(BFS)

题目https://leetcode.com/problems/rotting-oranges/题解和 leetcode 542. 01 Matrix | 542. 01 矩阵(图解,广度优先搜索) 这道题几乎没有区别,直接用 542 的图,来讲一下“感染” 的过程,实际上就是个 BFS只不过本题的 0,1,2 都被占用了,所以我们用 term=3 开始,标记感染轮数。感染过程中,每一轮 term+1,并且记录每一轮感染的数量 incr。如果某一轮出现 incr=0,即没有任何 orange 被

2021-10-29 12:48:03 43047

原创 哈夫曼编解码(C语言)

哈夫曼编解码(C语言)示例输入:helllllllo生成码表:a:00010010b:00010011c:00010100d:00010101e:001f:00010110g:00010111h:010i:00011000j:00011001k:00011010l:1m:00011011n:00011100o:011p:00011101q:00011110r:00011111s:0000000t:0000001u:0000010v:0000011w:00

2021-10-28 18:34:39 42601

转载 糟糕程序员的20个坏习惯

糟糕程序员的20个坏习惯转自:https://mp.weixin.qq.com/s/6hUU6SZsxGPWAIIByq93Rw阅读本文大约需要 5 分钟。你好,我是 Kaito。今天我想和你聊一聊优秀程序员的基本素养。我想你肯定遇到过这样一类程序员:他们无论是写代码,还是写文档,又或是和别****人沟通,都显得特别专业。每次遇到这类人,我都在想,他们到底是怎么做到的?随着工作时间的增长,渐渐地我也总结出一些经验,他们身上都保持着一些看似很微小的优秀习惯,但正是因为这些习惯,体现出了一个优秀

2021-10-28 16:04:43 43598

Java小球躲避小游戏

Java开发的小球躲避小游戏。通过上下左右按键控制小球移动,小球移动的方向包括:横向、纵向、斜向,小球具有初始生命值。小球通过移动来躲避炮弹,当小球碰到炮弹时,产生爆炸的动画效果。注释很全面,速度、初始子弹数量都可以通过全局变量修改。适合入门学习使用。

2019-06-24

空空如也

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

TA关注的人

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