博客园 - ACBingouuid:24b78886-0ed1-41c2-8670-e3f31dcf42c4;id=560712021-07-05T14:35:55ZACBingohttps://www.cnblogs.com/acbingo/feed.cnblogs.comhttps://www.cnblogs.com/acbingo/p/14974729.html[LeetCode] 726. 原子的数量 - ACBingo大模拟。。。烦这种题 遇到括号匹配的题目,用栈就对了。此题需要栈+Map 没啥难度,写了半天,Debug几个用例后,1A过了 class Solution { class Atom { String s; int cnt = 1; Atom(String s, int cnt) { this.s =2021-07-05T14:36:00Z2021-07-05T14:36:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】大模拟。。。烦这种题 遇到括号匹配的题目,用栈就对了。此题需要栈+Map 没啥难度,写了半天,Debug几个用例后,1A过了 class Solution { class Atom { String s; int cnt = 1; Atom(String s, int cnt) { this.s = <a href="https://www.cnblogs.com/acbingo/p/14974729.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14952512.html[LeetCode] 773. 滑动谜题 - ACBingo朴素的BFS,注意要考虑输入就是答案的情况 class Solution { class State { int[][] board; int p; int q; int cnt; State(int[][] b, int i, int j) { board = b; p = i; q = j; c2021-06-29T15:29:00Z2021-06-29T15:29:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】朴素的BFS,注意要考虑输入就是答案的情况 class Solution { class State { int[][] board; int p; int q; int cnt; State(int[][] b, int i, int j) { board = b; p = i; q = j; c <a href="https://www.cnblogs.com/acbingo/p/14952512.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14948009.html[LeetCode] 815. 公交路线 - ACBingo朴素BFS,擦边过。。不难,但是绕,需要想清楚。 大致描述一下做法吧,首先枚举能经过起点站的公交车,将公交车入队列。 然后对当前公交车能到达的每一站,看能否换乘其它公交车,将其它公交车入队。 注意,已经“坐过”的公交车就没有意义再坐一次了,当然,已经去过的站也没有必要再去一次了。所以有两个visit2021-06-28T15:47:00Z2021-06-28T15:47:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】朴素BFS,擦边过。。不难,但是绕,需要想清楚。 大致描述一下做法吧,首先枚举能经过起点站的公交车,将公交车入队列。 然后对当前公交车能到达的每一站,看能否换乘其它公交车,将其它公交车入队。 注意,已经“坐过”的公交车就没有意义再坐一次了,当然,已经去过的站也没有必要再去一次了。所以有两个visit <a href="https://www.cnblogs.com/acbingo/p/14948009.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14924751.html[LeetCode] 剑指 Offer 38. 字符串的排列 - ACBingo回溯DFS 但是该递归函数并没有满足「全排列不重复」的要求,在重复的字符较多的情况下,该递归函数会生成大量重复的排列。对于任意一个空位,如果存在重复的字符,该递归函数会将它们重复填上去并继续尝试导致最后答案的重复。 解决该问题的一种较为直观的思路是,我们首先生成所有的排列,然后进行去重。而另一种思路2021-06-23T14:36:00Z2021-06-23T14:36:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】回溯DFS 但是该递归函数并没有满足「全排列不重复」的要求,在重复的字符较多的情况下,该递归函数会生成大量重复的排列。对于任意一个空位,如果存在重复的字符,该递归函数会将它们重复填上去并继续尝试导致最后答案的重复。 解决该问题的一种较为直观的思路是,我们首先生成所有的排列,然后进行去重。而另一种思路 <a href="https://www.cnblogs.com/acbingo/p/14924751.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14885022.html[LeetCode] 237. 删除链表中的节点 - ACBingo下一个值覆盖当前值即可。。 class Solution { public void deleteNode(ListNode node) { while (node.next.next != null) { node.val = node.next.val; node = node.next; } 2021-06-15T04:37:00Z2021-06-15T04:37:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】下一个值覆盖当前值即可。。 class Solution { public void deleteNode(ListNode node) { while (node.next.next != null) { node.val = node.next.val; node = node.next; } <a href="https://www.cnblogs.com/acbingo/p/14885022.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14885009.html[LeetCode] 445. 两数相加 II - ACBingo链表形式的大数相加,没什么难的 题目要求不能修改原链表。那就把两个链表遍历一遍,把数取出来放到stack里,再进行相加 或者利用递归?(不过也是一样的,自己构建的栈只是换成了递归栈) class Solution { public ListNode addTwoNumbers(ListNode l12021-06-15T04:29:00Z2021-06-15T04:29:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】链表形式的大数相加,没什么难的 题目要求不能修改原链表。那就把两个链表遍历一遍,把数取出来放到stack里,再进行相加 或者利用递归?(不过也是一样的,自己构建的栈只是换成了递归栈) class Solution { public ListNode addTwoNumbers(ListNode l1 <a href="https://www.cnblogs.com/acbingo/p/14885009.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14883881.html[LeetCode] 852. 山脉数组的峰顶索引 - ACBingo二分的一种变种,这次是比较条件改变了,相邻的两位来比较,进而判断左右区间的选择 class Solution { public int peakIndexInMountainArray(int[] arr) { int n = arr.length; int left = 1, right = n 2021-06-14T16:26:00Z2021-06-14T16:26:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】二分的一种变种,这次是比较条件改变了,相邻的两位来比较,进而判断左右区间的选择 class Solution { public int peakIndexInMountainArray(int[] arr) { int n = arr.length; int left = 1, right = n <a href="https://www.cnblogs.com/acbingo/p/14883881.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14883857.html[LeetCode] 278. 第一个错误的版本 - ACBingo二分寻找边界 public class Solution extends VersionControl { public int firstBadVersion(int n) { int i = 1; int j = n; while (i<=j) { int mid = i + ((j-i)>>12021-06-14T16:05:00Z2021-06-14T16:05:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】二分寻找边界 public class Solution extends VersionControl { public int firstBadVersion(int n) { int i = 1; int j = n; while (i<=j) { int mid = i + ((j-i)>>1 <a href="https://www.cnblogs.com/acbingo/p/14883857.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14883838.html[LeetCode] 374. 猜数字大小 - ACBingo考察二分的一种写法,注意数据的溢出 public class Solution extends GuessGame { public int guessNumber(int n) { int left = 1; int right = n; while (left <= right) { int m2021-06-14T15:55:00Z2021-06-14T15:55:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】考察二分的一种写法,注意数据的溢出 public class Solution extends GuessGame { public int guessNumber(int n) { int left = 1; int right = n; while (left <= right) { int m <a href="https://www.cnblogs.com/acbingo/p/14883838.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14865107.html[LeetCode] 1049. 最后一块石头的重量 II - ACBingo看来最近力扣的每日一题换成了背包啊。 这个问题并不是一个直接的背包问题,需要做一下问题转化的。 把石头尽可能的平均分成两堆,即两堆石头的总重量尽可能的相等。如果所有石头总重量和为sum的话,那么分成的两堆石头,每堆石头的重量尽可能的接近sum/2。 让这两堆石头相撞,最后剩下的最小石头,就是两堆石头2021-06-08T16:04:00Z2021-06-08T16:04:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】看来最近力扣的每日一题换成了背包啊。 这个问题并不是一个直接的背包问题,需要做一下问题转化的。 把石头尽可能的平均分成两堆,即两堆石头的总重量尽可能的相等。如果所有石头总重量和为sum的话,那么分成的两堆石头,每堆石头的重量尽可能的接近sum/2。 让这两堆石头相撞,最后剩下的最小石头,就是两堆石头 <a href="https://www.cnblogs.com/acbingo/p/14865107.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856980.html494. 目标和 - ACBingo494. 目标和 一看数据最多才20个,直接暴力DFS感觉能过,没想到真过了o(╯□╰)o class Solution { int ans = 0; public int findTargetSumWays(int[] nums, int target) { int n = nums.length2021-06-06T16:14:00Z2021-06-06T16:14:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】494. 目标和 一看数据最多才20个,直接暴力DFS感觉能过,没想到真过了o(╯□╰)o class Solution { int ans = 0; public int findTargetSumWays(int[] nums, int target) { int n = nums.length <a href="https://www.cnblogs.com/acbingo/p/14856980.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856963.html[LeetCode] 132. 分割回文串 II - ACBingo上一个题的变种,改了一下,果然超时,然后在dfs的时候加一些剪枝,仍然超时。。。 因为太晚了,实在不想自己想了,所以看了下题解,果然是要用动态规划来做。 大致思路是k[0][j]表示,当前从0节点出发,到j节点,最少的分隔数。那么得到k[0][j]的递归公式就是 : Min(k[0][t])+1,t2021-06-06T16:01:00Z2021-06-06T16:01:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】上一个题的变种,改了一下,果然超时,然后在dfs的时候加一些剪枝,仍然超时。。。 因为太晚了,实在不想自己想了,所以看了下题解,果然是要用动态规划来做。 大致思路是k[0][j]表示,当前从0节点出发,到j节点,最少的分隔数。那么得到k[0][j]的递归公式就是 : Min(k[0][t])+1,t <a href="https://www.cnblogs.com/acbingo/p/14856963.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856961.html[LeetCode] 147. 对链表进行插入排序 - ACBingo中等题,别看挺简单,能很快写出来代码蛮难的,后面可以用作面试考察题 大致思路遍历链表,对于每一次遍历i,将当前节点从链表上摘除,然后从链表开头到i节点:[head,i),寻找可以插入的位置。注意要处理边界情况:1. 可能插在了当前head节点前面 2. 当i节点的val比[head,i)中所有节点都2021-06-06T16:00:00Z2021-06-06T16:00:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】中等题,别看挺简单,能很快写出来代码蛮难的,后面可以用作面试考察题 大致思路遍历链表,对于每一次遍历i,将当前节点从链表上摘除,然后从链表开头到i节点:[head,i),寻找可以插入的位置。注意要处理边界情况:1. 可能插在了当前head节点前面 2. 当i节点的val比[head,i)中所有节点都 <a href="https://www.cnblogs.com/acbingo/p/14856961.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856953.html[LeetCode] 130. 被围绕的区域 - ACBingo题目不难理解,可以将问题转化成判断O构成的连通区域,是否有位于边界的块。 直接用朴素的BFS解决问题。BFS时要向上下左右四个方向都走,走过的块记录的list中,走的同时判断如果有位于边界的块,flag记为true 最后BFS完成后,如果是 false,那么就可以把list中记录的块置为X了;如果f2021-06-06T15:58:00Z2021-06-06T15:58:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】题目不难理解,可以将问题转化成判断O构成的连通区域,是否有位于边界的块。 直接用朴素的BFS解决问题。BFS时要向上下左右四个方向都走,走过的块记录的list中,走的同时判断如果有位于边界的块,flag记为true 最后BFS完成后,如果是 false,那么就可以把list中记录的块置为X了;如果f <a href="https://www.cnblogs.com/acbingo/p/14856953.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856946.html[LeetCode] 1190. 反转每对括号间的子串 - ACBingo考察栈的使用,需要注意边界情况。不难,想全TestCase很容易1A通过 class Solution { public String reverseParentheses(String s) { Stack<Character> stack = new Stack<>(); if (s.lengt2021-06-06T15:56:00Z2021-06-06T15:56:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】考察栈的使用,需要注意边界情况。不难,想全TestCase很容易1A通过 class Solution { public String reverseParentheses(String s) { Stack<Character> stack = new Stack<>(); if (s.lengt <a href="https://www.cnblogs.com/acbingo/p/14856946.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856944.html[LeetCode] 131. 分割回文串 - ACBingo首先标记出所有的回文子串。boolean f[i][j]表示下标[i,j]构成的子串是回文串。 使用中心扩展法来算出整个f数组:枚举每一个下标为回文串的中心,然后逐渐向两边尽可能的扩展。最终O(n^2)跑完f数组。 然后使用dfs枚举出所有可能性 粗心了一下,可惜没有1A class Solutio2021-06-06T15:55:00Z2021-06-06T15:55:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】首先标记出所有的回文子串。boolean f[i][j]表示下标[i,j]构成的子串是回文串。 使用中心扩展法来算出整个f数组:枚举每一个下标为回文串的中心,然后逐渐向两边尽可能的扩展。最终O(n^2)跑完f数组。 然后使用dfs枚举出所有可能性 粗心了一下,可惜没有1A class Solutio <a href="https://www.cnblogs.com/acbingo/p/14856944.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856938.html[LeetCode] 461. 汉明距离 - ACBingo蛮简单的一个题,位运算 public int hammingDistance(int x, int y) { int cnt = 0; while (x>0&&y>0) { int a = x & 1; int b = y & 1; if (a!=b) cnt++; x>>=1; y>>=1; } 2021-06-06T15:53:00Z2021-06-06T15:53:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】蛮简单的一个题,位运算 public int hammingDistance(int x, int y) { int cnt = 0; while (x>0&&y>0) { int a = x & 1; int b = y & 1; if (a!=b) cnt++; x>>=1; y>>=1; } <a href="https://www.cnblogs.com/acbingo/p/14856938.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856934.html深入理解二分的两种实现 - ACBingo在日常面试中,经常遇到有的候选人甚至连二分都写不对。你可能会说,不就是个二分吗,有啥难的?其实,不要小看二分哦,能完全写对的真的不多。 首先我们先看最朴素的二分写法: public int binarySearch(int[] a, int target) { int left = 0; int r2021-06-06T15:52:00Z2021-06-06T15:52:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】在日常面试中,经常遇到有的候选人甚至连二分都写不对。你可能会说,不就是个二分吗,有啥难的?其实,不要小看二分哦,能完全写对的真的不多。 首先我们先看最朴素的二分写法: public int binarySearch(int[] a, int target) { int left = 0; int r <a href="https://www.cnblogs.com/acbingo/p/14856934.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856931.html[LeetCode] 1074. 元素和为目标值的子矩阵数量 - ACBingo矩阵前缀和。因为矩阵中可能包含负值,所以这题肯定不会存在什么剪枝,动态规划的可能性。所以这个题也就没什么弯弯绕绕。个人感觉算不上个Hard题目。 最直观的思路就是枚举子矩阵,既枚举矩阵的左上角节点和右下角节点所构成的子矩阵。枚举是4层循环。 然后矩阵和的计算是两层循环,肯定不能套在枚举子矩阵的循环里2021-06-06T15:50:00Z2021-06-06T15:50:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】矩阵前缀和。因为矩阵中可能包含负值,所以这题肯定不会存在什么剪枝,动态规划的可能性。所以这个题也就没什么弯弯绕绕。个人感觉算不上个Hard题目。 最直观的思路就是枚举子矩阵,既枚举矩阵的左上角节点和右下角节点所构成的子矩阵。枚举是4层循环。 然后矩阵和的计算是两层循环,肯定不能套在枚举子矩阵的循环里 <a href="https://www.cnblogs.com/acbingo/p/14856931.html" target="_blank">阅读全文</a>https://www.cnblogs.com/acbingo/p/14856927.html[LeetCode] 560. 和为K的子数组 - ACBingo前缀和问题。题目很好理解,读完题我们可以很迅速的想到利用前缀和实现O(n^2)的算法 其实,题目要求只统计满足k的子序列个数。所以当我们遍历到i时,prefix[i] + x = k,所以我们只需要知道x这个值出现了几次,就能统计出i节点所能构成的答案。借用Map结构就能很轻易得到。 最终一次遍历就2021-06-06T15:49:00Z2021-06-06T15:49:00ZACBingohttps://www.cnblogs.com/acbingo/【摘要】前缀和问题。题目很好理解,读完题我们可以很迅速的想到利用前缀和实现O(n^2)的算法 其实,题目要求只统计满足k的子序列个数。所以当我们遍历到i时,prefix[i] + x = k,所以我们只需要知道x这个值出现了几次,就能统计出i节点所能构成的答案。借用Map结构就能很轻易得到。 最终一次遍历就 <a href="https://www.cnblogs.com/acbingo/p/14856927.html" target="_blank">阅读全文</a>