数据结构与算法
南不喃
当笔记本用
展开
-
左值(Lvalues)与右值(Rvalues)
左值(Lvalues)与右值(Rvalues)定义Every C++ expression has a type, and belongs to a value category.左值右值都是表达式属性,该属性称为value category.在现代C++中,对于左值表达式,通过具体名字和引用来指定一个对象,非左值就是右值。左值表示程序中必须有一个特定的名字引用到这个值。An lvalue has an address that your program can access. Exam原创 2022-01-24 19:14:12 · 850 阅读 · 0 评论 -
二叉树本地调试范例
struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x): val(x), left(NULL), right(NULL) {}};class Solution {public: int count = 0; int pathSum(TreeNode* root, int sum) { if (root == NULL) return count; dfs(root, sum); p.原创 2022-01-24 18:48:34 · 323 阅读 · 1 评论 -
剑指Offer 19.正则表达式匹配
剑指Offer 19.正则表达式匹配示例 1:输入:s = “aa”p = “a”输出: false解释: “a” 无法匹配 “aa” 整个字符串。示例 2:输入:s = “aa”p = “a*”输出: true解释: 因为 ‘*’ 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 ‘a’。因此,字符串 “aa” 可被视为 ‘a’ 重复了一次。示例 3:输入:s = “ab”p = “.*”输出: true解释: “.*” 表示可匹配零个或多个(原创 2022-01-19 16:26:02 · 368 阅读 · 1 评论 -
力扣刷题之排序
力扣刷题之排序 147.对链表进行插入排序148.排序链表147.对链表进行插入排序class Solution {public: ListNode* insertionSortList(ListNode* head) { if (head == nullptr) { return head; } ListNode* dummyHead = new ListNode(0); dummyHead->原创 2021-11-17 20:30:14 · 676 阅读 · 1 评论 -
力扣刷题之深度优先搜索
力扣刷题之深度优先搜索98.验证二叉搜索树99.恢复二叉搜索树113.路径总和II114.二叉树展开为链表116.填充每个节点的下一个右侧节点指针117.填充每个节点的下一个右侧节点指针II129.求根节点到叶节点数字之和199.二叉树的右视图98.验证二叉搜索树法一:递归class Solution {public: bool helper(TreeNode* root, long long lower, long long upper) { if (root == n原创 2021-11-10 17:15:22 · 888 阅读 · 0 评论 -
力扣刷题之哈希表
力扣刷题之哈希表133.克隆图138.复制带随机指针的链表146.LRU缓存机制(手写双向链表)202.快乐数133.克隆图法一:深度搜索class Solution {public: unordered_map<Node*, Node*> visited; Node* cloneGraph(Node* node) { if (node == nullptr) { return node; }原创 2021-10-27 09:09:13 · 208 阅读 · 0 评论 -
力扣刷题之动态规划
力扣刷题之动态规划241.为运算表达式设计优先级263.丑数264.丑数II313.超级丑数338.比特位计数241.为运算表达式设计优先级分治+递归class Solution {public: vector<int> diffWaysToCompute(string expression) { // 存储中间值 vector<int> count; for(int i = 0; i < expression原创 2021-10-25 17:13:49 · 134 阅读 · 0 评论 -
力扣刷题之字符串
力扣刷题之字符串6.Z字形变换8.字符串转换整数(atoi)12.整数转罗马数字6.Z字形变换class Solution {public: string convert(string s, int numRows) { if (numRows == 1) return s; vector<string> rows(min(numRows, int(s.size()))); // 防止s的长度小于行数 int curRow = 0; bool goingDown =原创 2021-10-21 09:38:18 · 396 阅读 · 0 评论 -
力扣刷题之数组(二)
力扣刷题之数组(二)200.岛屿数量204.计数质数209.长度最小的子数组215.数组中的第K个最大元素(经典!)220.存在重复元素III(经典)221.最大正方形229.求众数II238.除自身以外数组的乘积240.搜索二维矩阵II260.只出现一次的数字III剑指Offer II 080.含有k个元素的组合剑指Offer II 079.所有子集200.岛屿数量class Solution {private: void dfs(vector<vector<char>&原创 2021-10-17 09:45:22 · 148 阅读 · 0 评论 -
力扣刷题之数组
力扣刷题之数组41.缺失的第一个正数42.接雨水54.螺旋矩阵56.合并区间57.插入区间59.螺旋矩阵II64.最小路径和73.矩阵置零74.搜索二维矩阵75.颜色分类79.单词搜索26.删除有序数组中的重复项80.删除有序数组中的重复项II41.缺失的第一个正数https://leetcode-cn.com/problems/first-missing-positive/solution/que-shi-de-di-yi-ge-zheng-shu-by-leetcode-solution/原创 2021-09-28 11:45:03 · 365 阅读 · 0 评论 -
剑指Offer 68-II.二叉树的最近公共祖先
class Solution {public: unordered_map<int, TreeNode*> fa; unordered_map<int, bool> vis; void dfs(TreeNode* root){ if (root->left != nullptr) { fa[root->left->val] = root; dfs(root->left..原创 2021-09-24 09:25:28 · 59 阅读 · 0 评论 -
剑指Offer 68-I.二叉搜索树的最近公共祖先
https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/solution/er-cha-sou-suo-shu-de-zui-jin-gong-gong-0wpw1/class Solution {public: vector<TreeNode*> getPath(TreeNode* root, TreeNode* target) { vector..原创 2021-09-23 11:54:01 · 71 阅读 · 0 评论 -
剑指Offer 64.求1+2+...+n
https://leetcode-cn.com/problems/qiu-12n-lcof/solution/qiu-12n-by-leetcode-solution/class Solution {public: int sumNums(int n) { n && (n += sumNums(n-1)); return n; }};原创 2021-09-23 11:28:51 · 62 阅读 · 0 评论 -
剑指Offer 63.股票的最大利润
class Solution {public: int maxProfit(vector<int>& prices) { int inf = 1e9; int minprice = inf, maxprofit = 0; for (int price: prices) { maxprofit = max(maxprofit, price - minprice); minprice .原创 2021-09-23 11:00:40 · 57 阅读 · 0 评论 -
剑指Offer 62.圆圈中最后剩下的数字
https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/solution/jian-zhi-offer-62-yuan-quan-zhong-zui-ho-dcow/本题是著名的 “约瑟夫环” 问题,可使用 动态规划 解决。class Solution {public: int lastRemaining(int n, int m) { int x = 0;..原创 2021-09-23 10:42:04 · 56 阅读 · 0 评论 -
剑指Offer 55-II.平衡二叉树
自顶向下的递归类似于二叉树前序遍历class Solution {public: int height(TreeNode* root) { if (root == NULL) { return 0; } else { return max(height(root->left), height(root->right)) + 1; } } bool isBalanced.原创 2021-09-23 10:03:53 · 50 阅读 · 0 评论 -
剑指Offer 61.扑克牌中的顺子
class Solution {public: bool isStraight(vector<int>& nums) { vector<int> map(14); //构建哈希集合 int minValue = INT_MAX, maxValue = INT_MIN; for(int n : nums) { if(map[n] >= 1) return ..原创 2021-09-23 09:25:20 · 52 阅读 · 0 评论 -
剑指Offer 67.把字符串转换成整数
class Solution {public: int strToInt(string str) { int ret = 0; // 转换后的数字 int flag = 1; // 标记正负号 int len = str.size(); if (len <= 0) return ret; // 临界条件 int index = 0; // 遍历字符串,寻找第一个不是空格的字符 w.原创 2021-09-23 08:44:22 · 59 阅读 · 0 评论 -
剑指Offer 60.n个骰子的点数
https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof/solution/jian-zhi-offer-60-n-ge-tou-zi-de-dian-sh-z36d/class Solution {public: vector<double> twoSum(int n) { int dp[15][70]; memset(dp, 0, sizeof(dp));//初始化dp数组 ..原创 2021-09-22 20:53:37 · 52 阅读 · 0 评论 -
剑指Offer 66.构建乘积数组
整体思路:结果集中任何一个元素 = 其左边所有元素的乘积 * 其右边所有元素的乘积。一轮循环构建左边的乘积并保存在结果集中,二轮循环 构建右边乘积的过程,乘以左边的乘积,并将最终结果保存class Solution {public: vector<int> constructArr(vector<int>& a) { int len = a.size(); if (len == 0) return {}; // 判空 ..原创 2021-09-22 18:04:27 · 47 阅读 · 0 评论 -
剑指Offer 59-II.队列的最大值
class MaxQueue { int q[20000]; int begin = 0, end = 0;public: MaxQueue() { } int max_value() { int ans = -1; for (int i = begin; i != end; ++i) ans = max(ans, q[i]); return ans; } .原创 2021-09-22 17:25:56 · 47 阅读 · 0 评论 -
剑指Offer 65.不用加减乘除做加法
class Solution {public: int add(int a, int b) { while(b!=0){ int c=(unsigned int)(a&b)<<1;// c++不支持负值左移,需要强制转换为无符号数 a^=b; b=c; } return a; }};...原创 2021-09-14 09:35:27 · 46 阅读 · 0 评论 -
剑指Offer 49.丑数
https://leetcode-cn.com/problems/chou-shu-lcof/solution/chou-shu-by-leetcode-solution-0e5i/小根堆,注意小根堆的定义方式class Solution {public: int nthUglyNumber(int n) { vector<int> factors = {2, 3, 5}; unordered_set<long> seen;..原创 2021-09-13 22:16:40 · 118 阅读 · 0 评论 -
剑指Offer 54.二叉搜索树的第k大节点
//自己写的class Solution {public: int kthLargest(TreeNode* root, int k) { vector<int>r=inorder(root); return r[k-1]; } vector<int>res; vector<int> inorder(TreeNode*root){ if(root==NULL)retur.原创 2021-09-13 21:38:30 · 51 阅读 · 0 评论 -
剑指Offer 48.最长不含重复字符的子字符串
滑动窗口class Solution {public: int lengthOfLongestSubstring(string s) { int maxsub = 0, left = 0, pos = 0; vector<bool> used(256, false); while(pos < s.size()){ while(used[s[pos]]) used[s[left++]] = false..原创 2021-09-13 21:19:07 · 44 阅读 · 0 评论 -
剑指Offer 58-II.左旋转字符串
class Solution {public: string reverseLeftWords(string s, int n) { string res; res.append(s.substr(n,s.size()-n)); res.append(s.substr(0,n)); return res; }};原创 2021-09-13 20:38:30 · 65 阅读 · 0 评论 -
剑指Offer 58-I.翻转单词顺序
https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/solution/mian-shi-ti-58-i-fan-zhuan-dan-ci-shun-xu-shuang-z/class Solution {public: string reverseWords(string s) { string res; int n = s.size(); if(n == 0) r..原创 2021-09-13 20:22:57 · 51 阅读 · 0 评论 -
剑指Offer47.礼物的最大价值
class Solution { public int maxValue(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(i == 0 &&..原创 2021-09-13 20:00:44 · 52 阅读 · 0 评论 -
剑指Offer-46.把数字翻译成字符串
https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof/solution/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-by-leetcode-sol/「滚动数组」优化动态规划的方法,这是一种非常常见的空间优化整型转换为字符串型class Solution {public: int translateNum(int num) { string src = t..原创 2021-09-13 19:42:33 · 49 阅读 · 0 评论 -
剑指Offer-57-II.和为s的连续正数序列
class Solution {public: vector<vector<int>> findContinuousSequence(int target) { vector<vector<int>> vec; vector<int> res; int sum = 0, limit = (target - 1) / 2; // (target - 1) / 2 等效于 target / ..原创 2021-09-10 22:01:16 · 53 阅读 · 0 评论 -
剑指Offer-45.把数组排成最小的数
https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/solution/mian-shi-ti-45-ba-shu-zu-pai-cheng-zui-xiao-de-s-4/class Solution {public: string minNumber(vector<int>& nums) { vector<string> strs; ...原创 2021-09-10 21:25:11 · 68 阅读 · 0 评论 -
剑指Offer-57.和为s的两个数
双指针class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { int i=0,j=nums.size()-1; while(i<j){ if(nums[i]+nums[j]>target){ j--; }else if(nums[i]+num..原创 2021-09-10 20:15:21 · 43 阅读 · 0 评论 -
剑指Offer-56-I、II.数组中数字出现的次数
https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/solution/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-by-leetcode/class Solution {public: vector<int> singleNumbers(vector<int>& nums) { int ret = 0;/...原创 2021-09-08 23:21:15 · 54 阅读 · 0 评论 -
剑指Offer 55-I.二叉树的深度
class Solution {public: int maxDepth(TreeNode* root) { if(!root){ return 0; } return max(maxDepth(root->left),maxDepth(root->right))+1; }};原创 2021-09-08 22:36:04 · 66 阅读 · 0 评论 -
剑指Offer-51.数组中的逆序对
考点:归并排序与逆序对息息相关考虑在归并排序的合并阶段统计「逆序对」数量,完成归并排序时,也随之完成所有逆序对的统计。https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/solution/jian-zhi-offer-51-shu-zu-zhong-de-ni-xu-pvn2h/class Solution {public: int mergeSort(vector<int>& num..原创 2021-09-08 22:16:58 · 58 阅读 · 0 评论 -
剑指Offer-34.二叉树中和为某一值的路径
考点:回溯class Solution {public: vector<vector<int>>ret; vector<int>path; void dfs(TreeNode* root,int target){ if(root==nullptr){ return; } path.emplace_back(root->val); target-=..原创 2021-09-08 20:42:46 · 57 阅读 · 0 评论 -
剑指Offer-30.第一个只出现一次的字符
法一:哈希表class Solution {public: char firstUniqChar(string s) { unordered_map<int,int>map;//注意map里面的类型 //if(s==" ")return ' '; for(int i=0;i<s.size();i++){ map[s[i]]++; } for(int i=0;i<s..原创 2021-09-07 10:31:54 · 178 阅读 · 0 评论 -
剑指Offer-33.二叉搜索树的后序遍历序列
https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/solution/mian-shi-ti-33-er-cha-sou-suo-shu-de-hou-xu-bian-6/法一:递归分治//递归class Solution {public: bool verifyPostorder(vector<int>& postorder) { r..原创 2021-09-07 09:58:51 · 54 阅读 · 0 评论 -
剑指Offer-32-I、II、III.从上到下打印二叉树
利用队列实现层序遍历class Solution{ public: vector<int>levelOrder(TreeNode* root){ vector<int>res; if(root==NULL)return res; queue<TreeNode*>q; q.emplace(root); while(!q.empty()){ TreeNo.原创 2021-09-06 22:07:01 · 56 阅读 · 0 评论 -
剑指Offer-39.数组中出现次数超过一半的数字
https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/solution/mian-shi-ti-39-shu-zu-zhong-chu-xian-ci-shu-chao-3/哈希表class Solution {public: int majorityElement(vector<int>& nums) { unorder..原创 2021-09-06 21:22:16 · 90 阅读 · 0 评论