![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法笔记
CS彩笔
这个作者很懒,什么都没留下…
展开
-
【Leetcode笔记】走迷宫(BFS, Dijkstra)
BFS, Dijkstra模板原创 2022-06-18 06:48:31 · 382 阅读 · 1 评论 -
【刷题笔记】双指针
双指针算法,刷题总结。原创 2022-06-08 12:57:03 · 122 阅读 · 0 评论 -
【刷题笔记】二分查找
二分模板与例题原创 2022-06-08 11:08:01 · 132 阅读 · 0 评论 -
Leetcode 第227 场周赛总结
题目链接class Solution { public boolean check(int[] nums) { // 判断是否存在两个非递减数列,且后一个的最大值 <= 前一个的最小值 int count = 0; // 逆序对个数 for (int i = 1; i < nums.length; ++i) { if (nums[i] < nums[i - 1]) count++; }原创 2021-02-07 12:36:30 · 225 阅读 · 0 评论 -
Leetcode 665. Non-decreasing Array
题目详情找规律class Solution { public boolean checkPossibility(int[] nums) { int count = 0; // 分情况讨论 for (int i = 1; i < nums.length; ++i) { if (nums[i] < nums[i - 1]) { //1. 当出现类似1536,修改nums[i-1] =原创 2021-02-07 10:00:55 · 94 阅读 · 0 评论 -
剑指 Offer 45. 把数组排成最小的数
题目链接排序法思路很巧妙,具体看注释,证明过程参考:K神题解class Solution { public String minNumber(int[] nums) { // 转化为排序问题,A拼接B > B拼接A,则B > A String[] strings = new String[nums.length]; for (int i = 0; i < nums.length; ++i) strings[i] = String原创 2021-02-06 21:04:20 · 89 阅读 · 0 评论 -
Leetcode1423. 可获得的最大点数
题目详情滑动窗口class Solution { public int maxScore(int[] cardPoints, int k) { // 使用滑动窗口,寻找连续的,长度为cardPoints.length-k的,且和最小的子数组 // 剩下的元素的和即为题目所求的maximum score int total = 0; for (int value: cardPoints) { total +=原创 2021-02-06 15:54:44 · 65 阅读 · 0 评论 -
Leetcode 1208. Get Equal Substrings Within Budget
题目链接参考题解:负雪明烛题解滑动窗口套滑动窗口模板,总体思路是右指针向前探索,不满足要求的时候左指针前进,直到符合要求,重复上述过程,在这个过程中一直更新满足要求的最大值。class Solution { public int equalSubstring(String s, String t, int maxCost) { int left = 0, right = 0; int N = s.length(); int[] diff = n原创 2021-02-05 11:04:11 · 95 阅读 · 0 评论 -
剑指 Offer 41. 数据流中的中位数
题目链接维护两个优先队列class MedianFinder { Queue<Integer> A, B; /** initialize your data structure here. */ public MedianFinder() { // A保存较大的一半,小顶堆 A = new PriorityQueue<>(); // B保存较小的一半,大顶堆 B = new Priority原创 2021-02-04 20:26:18 · 46 阅读 · 0 评论 -
剑指offer 复杂链表的复制
牛客链接普通链表的深拷贝只需要通过遍历原链表一次就能实现,但是题目给出的复杂链表多了random指针,会跳跃性的指向任意节点,这让我们无法通过一次遍历解决这个问题。利用HashMap建立映射提前存储新节点对应哪个原节点,这样就能够找到新节点的random指针应该指向哪(指向原节点的random指针指向的节点的对应新节点)。/*public class RandomListNode { int label; RandomListNode next = null; Random原创 2021-02-02 22:37:52 · 50 阅读 · 0 评论 -
Leetcode 191. Number of 1 Bits
题目链接本题可以利用32位int,写循环右移32次来做,但是我觉得更自然的方法是以下两种。1. 利用无符号右移Java没有无符号整型,但是利用>>>可以实现无符号右移,即不论符号位是1还是0,统一在左侧补0。public class Solution { public int hammingWeight(int n) { int count = 0; while (n != 0) { count += (n &原创 2021-02-02 00:27:35 · 82 阅读 · 0 评论 -
Leetcode 215. Kth Largest Element in an Array 优先队列&快速选择
题目链接快速选择使用快速排序的partition方法,结合二分查找的思想。class Solution { public int findKthLargest(int[] nums, int k) { if (nums == null || nums.length == 0 || k > nums.length) return 0; if (nums.length == 1) return 1; int lo = 0; i原创 2021-01-31 17:15:36 · 93 阅读 · 0 评论 -
Leetcode 17. Letter Combinations of a Phone Number
题目链接DFSclass Solution { public List<String> letterCombinations(String digits) { List<String> list = new ArrayList<String>(); if(digits.length() == 0) return list; Map<Character, String> map = new HashMa原创 2021-01-31 12:44:46 · 72 阅读 · 0 评论 -
Leetcode 1675. Minimize Deviation in Array 两种做法(带注释)
题目链接1. 使用TreeSetclass Solution { public int minimumDeviation(int[] nums) { if (nums == null || nums.length == 0) return 0; TreeSet<Integer> set = new TreeSet<>(); // 1. Pre-double all the odd numbers, and numbers o原创 2021-01-31 11:55:44 · 216 阅读 · 0 评论 -
Leetcode 1143. 最长公共子序列
class Solution {public: int longestCommonSubsequence(string text1, string text2) { // 动态规划 递推式 // dp[i][j] = dp[i-1][j-1] + 1 if text1[i] = text2[i] // dp[i][j] = max(dp[i][j-1], dp[i-1][j]) else int len1 = text1.size原创 2020-06-03 20:42:41 · 140 阅读 · 0 评论 -
Leetcode 面试题64. 求1+2+…+n
class Solution {public: int sumNums(int n) { !n || (n += sumNums(n-1)); return n; } int sumNums1(int n){ n && (n += sumNums1(n-1)); return n; }};利用与或来进行条件判断。或的特性:当第一项为1,则直接返回1而不执行第二项。与的特性:当第一项为0原创 2020-06-02 20:07:18 · 77 阅读 · 0 评论 -
《剑指offer》16. 数值的整数次方 python
快速幂class Solution: def myPow(self, x: float, n: int) -> float: if x == 0: return 0 # 幂数为负数,取倒数 if n < 0: x, n = 1/x, -n result = 1 # 二分法求快速幂 while n: # 当幂数为奇数的时候原创 2020-05-26 15:06:29 · 129 阅读 · 0 评论 -
《剑指offer》15. 二进制中1的个数 python
位运算def hammingWeight(self, n: int) -> int: # python 的int是无限大的,所以不能以unsigned flag溢出来判断 flag = 1 count = 0 for i in range(32): if n & flag != 0: count += 1 flag = flag << 1 return count最好不要采用右移n,和1与的方式,若n < 0原创 2020-05-25 16:56:44 · 164 阅读 · 0 评论 -
《剑指offer》14. 剪绳子 python
贪心class Solution: def cuttingRope(self, n: int) -> int: if n < 2: return 0 if n == 2: return 1 if n == 3: return 2 # 尽量剪3出来 timeOf3 = int(n / 3) if n - timeOf3*3原创 2020-05-25 15:55:27 · 68 阅读 · 0 评论 -
最大长方体问题
此题思路:1,先编写一维的“最大字段和”的解法。2,基于“最大字段和”,编写二维的“最大子矩阵和”的解法。3,基于“最大子矩阵和”,编写三维的“最大子长方体和”的解法。一维最大子段和采用动态规划,高维的则是遍历多个相邻行(最高维)的情况,将这些行压缩叠加为低一维的数据b,再用低一维的最大子段和算法算出最大子段和,这个结果就是高一维的最大子段和。代码:#include&l...原创 2019-11-29 22:13:34 · 551 阅读 · 0 评论 -
工作分配问题(回溯法)
Description有n件工作分配给n个人,将工作i分配给第j个人需要支付劳务费用Cij。请为每人分配一个工作,并使得总劳务费用达到最小。输入格式第一行一个正整数n(1<=n<=11),表示n个工作数,接下来n行,每行代表第i个工作支付给n个不同的人的劳务费用。输出格式两行。第一行为最小的总劳务费用。第二行有n个数,表示工作分配方案。...原创 2019-11-22 00:15:01 · 1852 阅读 · 0 评论 -
有重复元素的全排列
使用递归的方法解决全排列问题全排列的本质:对于一个字符串,递归的令每一个元素与其后面的每一个元素交换。或者可以理解为例如字符串abc:先观察第一个元素a,将a分别与b,c交换 得到bac cba 再对于abc, bac, cba 观察第二个元素,与第三个元素交换得 acb, bca, cab ...原创 2019-10-09 13:22:26 · 2322 阅读 · 0 评论 -
分治策略求最大子数组
def maxCrossSum(nums, left, right): mid = int((left+right) / 2) # 初始化为无穷小 left_max = float('-inf') # 倒序遍历左序列,得到从中见到左边延伸的最大值 left_sum = 0 for i in range(mid, left-1, -1): ...原创 2019-09-10 15:19:17 · 122 阅读 · 0 评论