matteo's coding field.

matteo's coding field.

better late than never

LeetCode.134 Gas Station
https://leetcode.com/problems/gas-station/submissions/ 思路1  算出每个station出发之后剩余的gas,保存在数组中 从数组中找出剩余gas不小于0的位置,然后从该位置开始查询,走回原点或者gas小于0退出循环 这个解法能AC,但是非常慢,继续想 AC解class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int gasLeft[] = new int[gas.length]; for (int i = 0...
LeetCode.133 Clone Graph
https://leetcode.com/problems/clone-graph/ 思路 头昏眼花想不出任何方法,看了别人思路发现好简单 1、广度优先遍历(BFS)2、深度优先遍历(DFS)2.1、递归2.2、非递归https://www.cnblogs.com/ganganloveu/p/4119462.html 解法class Solution { HashMap<Node, Node> map = new HashMap<>(); public Node cloneGraph(Node node) { if (node == null)...
LeetCode.132 Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/ 题意 找出将字符串分割成回文子串的最少分割数思路 开始想着用131的dfs,因为131没有TLE,提交之后果然还是TLE了,131因为要输出所有的分割,所以没有类似”ababababababababababababcbabababababababababababa”这样的测试用例。 后来想用动态规划做,但是没有想出来怎么做,后来参考的别人的思路。但是第二次的DP是自己想出来的,第一次的解法很精妙,没想出来~ (动态规划) O(n2)O(n2)一共进行两次动态规划。第一次动规...
LeetCode.131 Palindrome Partitioning
https://leetcode.com/problems/palindrome-partitioning/ 题意 取出所有的回文子串,想到的就是dfs,开始想着可能会超时,后来写出来了发现并没有 解法class Solution { Stack<String> stack = new Stack<>(); List<List<String>> results = new ArrayList<>(); public List<List<String>> partition(String s) ...
LeetCode.130 Surrounded Regions
https://leetcode.com/problems/surrounded-regions/ 思路 一开始想的是顺序找到第一个位置然后找出路,这样的话太复杂,需要记下路径判断该路径能否突围 后面反着想就是从四个边开始找注定能突围的位置,然后向四个位置查找,只要是连通的肯定能突围,后续计算量会小很多 写完AC后发现时间差很多,去查看了100%的答案,发现思路完成一样,于是一句句改提交为什么会慢这么多,最后发现是因为我用System.out.println()打印日志导致的😂 解法class Solution { public void solve(char[][] boa...
LeetCode.126 Word Ladder II
https://leetcode.com/problems/word-ladder-ii/ 思路 这题可能用126的解法,区别在于这题需要记下所有的最短解,作下面几点修改 queue中需要记下完整路径,每一层往queue中增加最短解的时候需要在新增list里增加当前word 每一层中前面使用的word也能用于后面的情况,但是后面层不会出现该层及之前使用的word,因为广度优先搜索,每个word第一次出现的肯定是最短路 当某一层中找到解之后,该层遍历结束后即结束寻找 虽然很慢,但还是AC了,后续再看找更优解法 解法class Solution { public List<...
LeetCode.127 Word Ladder
https://leetcode.com/problems/word-ladder/ 思路 开始用dfs递归,不出所望超时了,后来想着用动态规划,做了一上午没有一点头绪。算法实在太难了,真心不适合我😭 因为要求最短路径,如果我们用深度优先搜索的话必须遍历所有的路径才能确定哪个是最短的,而用广度优先搜索的话,一旦搜到目标就可以提前终止了,而且根据广度优先的性质,我们肯定是先通过较短的路径搜到目标。另外,为了避免产生环路和重复计算,我们找到一个存在于字典的新的词时,就要把它从字典中移去。这么做是因为根据广度优先,我们第一次发现词A的路径一定是从初始词到词A最短的路径,对于其他可能再经过词A...
LeetCode.123 Best Time to Buy and Sell Stock III
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 思路 暴力解法就是在121的基础上加个二分计算,能AC但是需要447ms,时间复杂度O(2$n^2$) class Solution { public int maxProfit(int[] prices, int start, int end) { int min = Integer.MAX_VALUE, max = 0; for (int i = start; i < end; i++) { //记录到截...
LeetCode.120 Triangle
https://leetcode.com/problems/triangle/ 思路 一开始想到用递归,写的真是优美简洁,然并卵~~一个TLE糊脸 class Solution { int min = Integer.MAX_VALUE; public int minimumTotal(List<List<Integer>> triangle) { search(triangle, 0, 0, 0); return min; } void search(List<List<Integer>> triangl...
LeetCode.118 Pascal's Triangle
https://leetcode.com/problems/pascals-triangle/ 思路 开始想的是直接递归解决,提交后发现TLE,于是采用HashMap记录中间状态,能够AC class Solution { HashMap<Integer, HashMap<Integer, Integer>> map = new HashMap<>(); List<List<Integer>> results = new ArrayList<>(); public List<List<Intege...
avatar
matteo