
leetcode
=w=
luckycoding
luckycoding只会失败!绝对不允许放弃!
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
剑指 Offer II 005. 单词长度的最大乘积
位运算记录字母是否出现过,然后枚举值class Solution {public: int maxProduct(vector<string>& words) { int visit[1005] = {0}; for (int i = 0; i < words.size(); i++) { for (string::iterator it = words[i].begin(); it != words[i].e原创 2022-02-18 00:04:31 · 393 阅读 · 0 评论 -
1330. 翻转子数组得到最大的数组值
贪心法:1. 翻转中间,则使用(最小区间最大值-最大区间最小值)*2 + 基准为数组值,这里不一定为正收益2.翻转头或者尾,则位基准重新计算翻转点基准=sum(abs(num[i] - num[i - 1])) (1<=i<nums.size())class Solution {public: int maxValueAfterReverse(vector<int>& nums) { if (nums.size() <= 1.原创 2022-02-17 23:52:02 · 339 阅读 · 0 评论 -
583. 两个字符串的删除操作
问题等于len(s1)+len(s2) - len(最长子串)*2直接求最长公共子序列class Solution {public: int minDistance(string word1, string word2) { const size_t n = word1.size(); const size_t m = word2.size(); int visit[505][5005] = {0}; for (int i原创 2022-02-15 23:29:13 · 217 阅读 · 0 评论 -
1371. 每个元音包含偶数次的最长子字符串
暴力,预处理记录每个位置元音出现了多少次,然后穷举n(从size到1)正确解法,记录元音是否出现偶次,可用状态压缩到位,然后出现了相同的状态是即满足要求,记录状态第一次出现的位置。class Solution {public: char index[256]; int visit[500005][5]; void init_index() { memset(index, -1, sizeof(char) * 256); index['a']原创 2022-02-14 23:09:11 · 346 阅读 · 0 评论 -
386. 字典序排数
字典序特点:先比对首位,在比对第二位,依次类推。所以第一位先填1,在判断下一位是否大于n,如果小于就递归。class Solution {public: void solve(vector<int> &ans, const int n, int now) { if (now <= n) ans.push_back(now); for (int i = 0; i < 10; ++i) {原创 2022-02-14 22:00:56 · 228 阅读 · 0 评论 -
1038. 把二叉搜索树转换为累加树
当前节点下1.如果有右子树,则先递归右子树。得出右子树累加和。当前节点为和加最大值。2.如果无右子树,则当前值为原树值+MAX(左子树和,父节点值)。3.如果左子树,则递归左子树。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), lef原创 2021-07-14 00:02:25 · 82 阅读 · 0 评论 -
剑指 Offer 13. 机器人的运动范围
bfsclass Solution {public: bool judge(int x, int y, int k) { int tmp = 0; while (x > 0) { tmp += x % 10; x /= 10; } while (y > 0) { tmp += y % 10; y /= 10;原创 2021-06-02 21:18:06 · 98 阅读 · 0 评论 -
1382. 将二叉搜索树变平衡
通过先序遍历得到有序数组,然后重建。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: void tree2原创 2021-05-17 22:51:52 · 93 阅读 · 0 评论 -
745. 前缀和后缀搜索
预处理所有的情况,然后查找class WordFilter {public: unordered_map<string, unordered_map<string, int>> visit; WordFilter(vector<string>& words) { for (int n = 0; n < words.size(); ++n) { const auto &index = wo原创 2021-04-28 23:11:25 · 124 阅读 · 0 评论 -
477. 汉明距离总和
二进制每一位分开计算,当前位有n个1,则有size-n个0,则不一样个数为n*(size-n)。class Solution {public: int totalHammingDistance(vector<int>& nums) { int visit[32] = {0}; for (auto &index:nums) { for (int i = 0; i < 32; i++)原创 2021-04-28 22:05:12 · 82 阅读 · 0 评论 -
494. 目标和
初始化 dp[0][x] = dp[0][x] = 1 (x为num[0])转移方程为dp[n][m] = dp[n - 1][m + num[n - 1]] + dp[n - 1][m - num[n - 1]] (n为1~nums.size() - 1, m为-1000~1000)可以使用滚动数组优化空间。class Solution {public: void exchage(int num, int count, int &a, int &b) {原创 2021-04-27 23:51:52 · 68 阅读 · 0 评论 -
831. 隐藏个人信息
题目链接:https://leetcode-cn.com/problems/masking-personal-information/解题思路:硬处理class Solution {public: string maskPII(string S) { auto index = S.find("@"); std::string ans; if (index != std::string::npos) { ans =原创 2021-04-23 00:11:46 · 138 阅读 · 0 评论 -
面试题 17.19. 消失的两个数字
用原数组的负数表示已经出现过class Solution {public: vector<int> missingTwo(vector<int>& nums) { vector<int> ans; int end[2]; for (int i = 0; i < nums.size(); i++) { int tmp = abs(nums[i]) - 1;原创 2021-04-21 23:20:15 · 88 阅读 · 0 评论 -
744. 寻找比目标字母大的最小字母
二分查找,没有找到元素则循环,循环一定是第一个最小,故用它初始化。class Solution {public: char nextGreatestLetter(vector<char>& letters, char target) { target++; int l = 0, r = letters.size() - 1; char ans = letters[0]; while (l <= r) {原创 2021-04-12 21:55:22 · 69 阅读 · 0 评论 -
1591. 奇怪的打印机 II
贪心贪出一片天。先判断图上的完整的长方形,然后设置成0,0认为是任意颜色,然后继续找长方形,直到找不到为止,如果矩形全部为0则表示可达。class Solution {public: bool solve(vector<vector<int>>&mp, int color, int x, int y, int x_max, int y_max) { // cout << x << " " << y <&l原创 2021-04-07 23:14:53 · 129 阅读 · 0 评论 -
1672. 最富有客户的资产总量
直接枚举。class Solution {public: int maximumWealth(vector<vector<int>>& accounts) { int ans = INT_MIN; for (auto &tmp:accounts) { int num = 0; for (auto &index:tmp) num +=原创 2021-04-07 22:13:28 · 72 阅读 · 0 评论 -
230. 二叉搜索树中第K小的元素
树形便利。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr原创 2021-04-07 00:30:15 · 79 阅读 · 0 评论 -
237. 删除链表中的节点
难点题意,要删除得node就是传入得node。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: void deleteNode(ListNode* node) {原创 2021-04-06 23:39:07 · 67 阅读 · 0 评论 -
617. 合并二叉树
递归合并/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr)原创 2021-04-06 23:28:02 · 89 阅读 · 0 评论 -
1288. 删除被覆盖区间
begin从小到大,end从大到小。然后枚举。class Solution {public: bool judge(vector<int> &a, vector<int> &b) { return a[0] <= b[0] && a[1] >= b[1]; } int removeCoveredIntervals(vector<vector<int>>&原创 2021-03-31 22:59:38 · 127 阅读 · 0 评论 -
891. 子序列宽度之和
长度为n的数组,应该有1<<n个子序列。排序之后不会改变子序列个数。对于一个数,左边的数组构成了让他变成最大数的子序列,那么它为最大数的个数为1<<left个数。同理右边个数为它为最少数个数。对于A[i] * ((1<<left) - (1<<right)) 求和class Solution {public: int sumSubseqWidths(vector<int>& A) { long ans原创 2021-03-31 22:31:52 · 128 阅读 · 0 评论