随笔分类 - LeetCode
摘要:给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 ###1. 暴力法 实际上是求以第一个字符为开头的最长回文子串 class Solution { public: string shortestPalindrome(string s) {
阅读全文
posted @ 2023-05-05 01:37
失控D大白兔
摘要:给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度 ####1. 栈操作 对于括号,要知道括号的类型以及位置,类似单调栈的删除操作,找消除后最左边位置 这里直接用正负号整数同时表示类型和位置 class Solution { public: int longe
阅读全文
posted @ 2023-05-04 22:52
失控D大白兔
摘要:简化unix文件路径 ####1. 分割提取+栈 class Solution { public: string simplifyPath(string path) { vector<string> names = split(path, '/');//消除/并得到待处理的多段文件名 vector<
阅读全文
posted @ 2023-05-04 22:11
失控D大白兔
摘要:给你一个字符串 s,请你将s分割成一些子串,使每个子串都是回文串,返回 s 所有可能的分割方案 ###1. 回溯+动态规划 class Solution { public: vector<vector<int>> dp; vector<vector<string>> res; vector<stri
阅读全文
posted @ 2023-05-04 21:18
失控D大白兔
摘要:x坐标轴上分配了水果的位置(升序)和个数 给你一个开始位置和步数,可以选择往左或往右,返回可以摘到的最大水果数 ###1. 二分查找+前缀和+滑动窗口 枚举先左后右和先右后左两种情况下的左右边界,利用二分和前缀和快速求值 class Solution { public: int maxTotalFr
阅读全文
posted @ 2023-05-04 01:07
失控D大白兔
摘要:###1. 移动石子直到连续(三个石子) class Solution { public: vector<int> numMovesStones(int a, int b, int c) { int x = min({a, b, c}); int z = max({a, b, c}); int y
阅读全文
posted @ 2023-05-01 05:25
失控D大白兔
摘要:如果一个字符串满足以下条件,则称其为 美丽字符串 : * 它由英语小写字母表的前 k 个字母组成。 * 它不包含任何长度为 2 或更长的回文子字符串。 返回下一个美丽字符串 ###1. 贪心更改 从后往前试探增加,满足条件继续往后走,同时判断是否满足回文 ``` class Solution { p
阅读全文
posted @ 2023-05-01 04:22
失控D大白兔
摘要:给你初始位置和目标位置,以及一些位置之间的快速路径,普通点之间的代价为曼哈顿距离 求初始位置到目标位置最小代价 两个算法都直接使用了sp路径序列当做点,边(代价计算)定义为前一路径第二坐标到后一路径第一坐标 ###1. Floyd算法 class Solution { public: const i
阅读全文
posted @ 2023-05-01 04:02
失控D大白兔
摘要:给你一个包含若干互不相同整数的数组nums,你需要执行以下操作直到数组为空 : 如果数组中第一个元素是当前数组中的最小值则删除它 否则,将第一个元素移动到数组的末尾 请你返回需要多少个操作使nums为空 ###一. 数学思维减小问题规模 除去删除元素的操作外,考虑每个数需要移动的次数 假设数组按升序
阅读全文
posted @ 2023-04-30 14:33
失控D大白兔
摘要:给你一个整数数组 nums 以及两个整数 lower 和 upper 求数组中,值位于范围 [lower, upper] (包含 lower 和 upper)之内的 区间和的个数 ###一. 前缀和+双重循环(超时) class Solution { public: int countRangeSu
阅读全文
posted @ 2023-04-25 22:36
失控D大白兔
摘要:###一. 数组 添加线性,访问常数 class MedianFinder { public: MedianFinder() { n = 0; } void addNum(int num) { n++; nums.push_back(num); int index = n - 1; for(int
阅读全文
posted @ 2023-04-24 19:52
失控D大白兔
摘要:###一. 买卖一次(简单) dp[i]表示第i天卖出时的最大值,可以用滚动变量优化 ``` class Solution { public: int maxProfit(vector& prices) { int n = prices.size(); vector dp(n+1); int min
阅读全文
posted @ 2023-04-24 19:17
失控D大白兔
摘要:给你一个字符串 s ,找出它的所有子串并按字典序排列,返回排在最后的那个子串 ###1. 暴力截取比较(超时) 记录最大字符位置,暴力截取比较 class Solution { public: string lastSubstring(string s) { map<char, vector<int
阅读全文
posted @ 2023-04-24 01:44
失控D大白兔
摘要:给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数 ###1. 固定每一位找规律 class Solution { public: int countDigitOne(int n) { unsigned i = 1, ans = 0, befor = 0; //i表示记录到了第
阅读全文
posted @ 2023-04-24 00:10
失控D大白兔
摘要:给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。 你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 返回 滑动窗口中的最大值 。 ###1. 暴力遍历(超时) 暴力法 class Solution { public: vector<int
阅读全文
posted @ 2023-04-23 22:02
失控D大白兔
摘要:给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数 ###1. 暴力排序查找 排序+去重+二分查找 class Solution { public: int firstMissingPositive(vector<int>& nums) { sort(nums.begin(),
阅读全文
posted @ 2023-04-23 20:47
失控D大白兔
摘要:给你一个整数数组 nums,返回nums中最长等差子序列的长度 ###一. 动态规划 该题类似最长递增子序列 dp[i][j]定义为以i为结尾,公差为j的最长等差数列长度 class Solution { public: int longestArithSeqLength(vector<int>&
阅读全文
posted @ 2023-04-22 22:40
失控D大白兔
摘要:给你两个整数数组 arr1 和 arr2,返回使 arr1 严格递增所需要的最小「操作」数(可能为 0)。 每一步「操作」中,你可以分别从 arr1 和 arr2 中各选出一个索引, 分别为 i 和 j,0 <= i < arr1.length 和 0 <= j < arr2.length,然后进行
阅读全文
posted @ 2023-04-21 00:50
失控D大白兔

浙公网安备 33010602011771号