![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++
前行_的路上
这个作者很懒,什么都没留下…
展开
-
记一次踩坑:循环里有递归的时候不要轻易用临时变量存放数组元素或字符串元素
记一次踩坑:循环里有递归的时候不要轻易用临时变量存放数组元素或字符串元素原创 2022-08-21 13:12:24 · 228 阅读 · 0 评论 -
关于c++的memset函数的踩坑避雷
memset函数的避雷原创 2022-06-15 20:25:32 · 613 阅读 · 0 评论 -
双调查找:数组先递增后递减
以极值点为分界线分为三段:mid在极值点,mid在极值点左侧,mid在极值点右侧,分段后二分查找原创 2022-06-15 12:43:11 · 575 阅读 · 0 评论 -
c++用归并排序计算右侧小于当前元素个数
c++用归并排序计算右侧小于当前元素个数原创 2022-06-05 20:53:02 · 132 阅读 · 0 评论 -
用递归和循环两种方法解决华为4月20日机试第一题(100分)
华为2022年4月20日机试第一题原创 2022-06-05 17:27:08 · 148 阅读 · 0 评论 -
判断一棵树是否为完全二叉树
问题:有一棵二叉树,给定它的根节点 root,请设计一个算法判断它是否是完全二叉树分析:因为要判断一棵树是否为完全二叉树,考虑用BFS算法,一层一层的去判断:1、如果一个节点有右子树但没有左子树,就返回false2、如果一个节点没有子树,而下一个节点又并非叶子节点,返回false代码:bool check(TreeNode* root){ bool leaf = false; queue<TreeNode*> q; q.push(root);原创 2022-05-27 16:36:37 · 102 阅读 · 0 评论 -
二分思想递归解决寻找两个正序数组的中位数
两个数组一个长度为m,一个长度为n,总长度total=m+n,k=(total/2)。由于两个数组是有序的,所以我们只需要找到两个数组中的第k+1个元素(总长度为偶数的时候,还要找到第k个元素)。class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int total = nums1.size() +原创 2022-05-19 16:43:22 · 135 阅读 · 0 评论 -
用快速排序的思想解决 数组中第K个最大元素
用快速排序的思想解决 数组中第K个最大元素思路:题目限定使用快排思想,就直接舍弃用最小堆这种办法。第一种方法:直接进行一遍快排,然后取倒数第K个元素即可。第二种方法:是优化后的快排,因为我们只需要找到倒数第K个元素即可,因此没必要把全部元素都排一遍,所以在排序的时候如果发现当前快速排序所排序元素的下标等于n-K就终止排序即可!class Solution {public: int k; int findKthLargest(vector<int>& .原创 2022-05-19 09:21:08 · 389 阅读 · 0 评论 -
原地哈希这一大类问题的通解
这类问题在力扣上对应的题目为:41、缺失的第一个正数442、数组中重复的数据448、找到所有数组中消失的数字首先,这类题目在题干中就会有所提示:如442题中的“其中 nums 的所有整数都在范围 [1, n]”。448题中的“其中 nums[i] 在区间 [1, n] 内”。41题描述的比较隐晦,“请你找出其中没有出现的最小的正整数”。例题讲解,以442题为例讲解一下这类问题的原理:看完题目之后,首先会想到使用额外空间构建一个哈希表,在遍历数组时一旦发现该元素已经在哈希表中出原创 2022-05-14 22:11:20 · 177 阅读 · 0 评论 -
力扣208题:实现字典树Trie
用树枝表示字符,用节点记录截止符(判断一个单词是否结尾)class Trie { bool isEnd; Trie* next[26];public: Trie() { this->isEnd = false; memset(next, 0, sizeof(next)); } void insert(string word) { Trie* node = this; for(cha原创 2022-04-29 20:23:40 · 335 阅读 · 0 评论 -
力扣113.路经总和2深度优先算法回溯
class Solution {public: int sum=0; vector<vector<int>> ans; vector<int> track; void backtrack(TreeNode* root, int targetSum){ if(root==nullptr) return; sum+=root->val; /*不要剪枝,因为节点有负数 if.原创 2022-04-13 09:59:06 · 649 阅读 · 0 评论 -
C++手撸哈希双链表:LRU缓存
struct Node { int key; int val; Node* prev; Node* next; Node(int key, int val) { this->key = key; this->val = val; }};//以Node为节点构造双链表struct DoubleList { Node *head, *tail; int size; DoubleList() { head = new Node(0, 0); tail = n.原创 2022-04-07 12:59:52 · 1134 阅读 · 0 评论 -
题目:公司食堂 方法:优先队列
#include<iostream>#include<queue>#include<vector>using namespace std;vector<int> assignment(int T, int N, int M, string eatingPerson, string lineingPerson){ vector<int> ans(M); priority_queue<int, vector<i.原创 2022-03-28 20:18:21 · 422 阅读 · 0 评论 -
199.二叉树的右视图(递归万岁)
使用递归解决这道题目。需要注意的点:1、要先递归右子树,再递归左子树。2、要考虑二叉树的深度,防止左子树深度大于右子树深度这种情况class Solution {public: vector<int> ans; void dfs(TreeNode* root, int depth){ if(!root) return; if(depth == ans.size()){ ...原创 2022-03-11 10:09:16 · 1390 阅读 · 0 评论 -
力扣:蜡烛之间的盘子
使用了前缀和的知识,但除了前缀和之外,我觉得还有一处地方非常巧妙:用left数组和right数组存放蜡烛的位置,使得查询蜡烛位置非常的方便class Solution {public: vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries) { int n = s.size(); vector<int> preSum原创 2022-03-08 09:41:29 · 127 阅读 · 0 评论 -
搜索二维矩阵,使用了两次二分查找
代码中使用了c++11中匿名函数的新特性,和auto关键字,大大减少了代码量:class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { auto row = upper_bound(matrix.begin(), matrix.end(), target, [](int x, vector<int> &y){原创 2022-03-03 10:58:35 · 418 阅读 · 0 评论 -
将二叉搜索树变平衡
class Solution {public: //对二叉树进行递归 void helper(TreeNode* root, vector<int>& nums){ if(root==nullptr) return; helper(root->left, nums); nums.push_back(root->val); helper(root->right, nums); }.原创 2022-03-01 17:36:58 · 189 阅读 · 0 评论 -
建立一棵二叉树并对其进行中序遍历
#include <iostream>using namespace std;#include <vector>struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr.原创 2022-03-01 08:58:16 · 815 阅读 · 0 评论 -
Floyd判圈算法--判断链表中有没有圈
class Solution {public: bool hasCycle(ListNode *head) { if(head==NULL||head->next==NULL){ return false; } ListNode* slowNode = head; ListNode* fastNode = head->next; while(fastNode!=slowNode){.原创 2022-02-27 11:25:54 · 220 阅读 · 0 评论 -
仅仅反转字母(双指针)
class Solution {public: string reverseOnlyLetters(string s) { int n = s.size(); int left = 0; int right = n-1; while(left<right){ if(isalpha(s[left])&&isalpha(s[right])){ swap(s[l.原创 2022-02-23 08:23:36 · 371 阅读 · 0 评论 -
回文链表(用快慢指针)
class Solution {public: bool isPalindrome(ListNode* head) { if(head==nullptr){ return true; } ListNode* midNode = halfList(head); ListNode* endNote = reverseList(midNode); while(head!=nullptr){ .原创 2022-02-19 21:37:12 · 492 阅读 · 0 评论 -
骑士在棋盘上的概率(递归)
这道题使用递归思想虽然代码较简洁,但会出现大量的重复计算,但还是想把这种思路展示给大家:class Solution {public: //vector<int> dirs = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; double knightProbability(int n, int k, int row, int column) { return (double)proce原创 2022-02-17 14:25:23 · 7683 阅读 · 0 评论 -
(使用栈)力扣:反转链表
class Solution {public: ListNode* reverseList(ListNode* head) { stack<ListNode*> s; while (head != nullptr){ s.push(head); head = head->next; } if (s.empty()){ return nullpt.原创 2022-02-13 15:47:34 · 239 阅读 · 0 评论