3.二叉树的右视图
123456789101112131415161718192021222324class Solution {public: vector<int> rightSideView(TreeNode* root) { vector<int>result; if(!root)return result; queue<TreeNode*>q; q.push(root); while(!q.empty()){ int size=q.size(); TreeNode*lastnode=nullptr; for(int i=0;i<size;++i){ TreeNode*node=q.front(); q.pop(); lastnode=node; if(node->...
2.无重复字符的最长子串
1234567891011121314151617181920212223242526272829303132class Solution {public: int lengthOfLongestSubstring(string s) { int cnt=s.size(),sum=0; map<char,int>mp; for(int i=0;i<cnt;i++){ if(!mp.count(s[i])){ sum++; mp[s[i]]=1; } } if(sum==1)return 1; if(sum==2)return 2; if(sum==cnt)return sum; for(int i=sum;i>=3;i--){ for(int j=0;j&...
1.手撕快排
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657class Solution { int partition(vector<int>& nums, int left, int right) { int i = left + rand() % (right - left + 1); int pivot = nums[i]; swap(nums[i], nums[left]); i = left + 1; int j = right; while (true) { while (i <= j && nums[i] < pivot) { i++; } ...
简易筛质数
一、int is(int n) 的作用是什么?123456int is(int n){ for(int i=2;i*i<=n;i++) if(n%i==0) return 0; return 1;} 👉 作用:判断一个整数 n 是否是质数(素数) 返回 1:n 是质数 返回 0:n 不是质数 所以它本质上是一个 质数判定函数。 二、为什么这样写能判断质数?1️⃣ 质数的定义 质数:只能被 1 和自身整除的正整数(n ≥ 2) 2️⃣ 为什么从 i = 2 开始? 1 肯定能整除任何数,没意义 所以从 最小可能的因子 2 开始试 3️⃣ 为什么只枚举到 i * i <= n?这是关键点 👇 数学原理:如果 n 是合数: 1n = a × b 那么: 至少有一个因子 ≤ √n 如果两个因子都 > √n,那么乘积会 > n(矛盾) ✅ 所以: 只要检查到 √n,就能确定 n 是否有因子 这也是为什么不用枚举到 n-1,可以大幅优化速度。 4️⃣ if(n % i == 0) retur...
如何启用butterfly主题的搜索功能
步骤 1:配置搜索功能从butterfly的_config.yml文件中到找search 在 search 设置中,选择使用本地搜索(local_search)或使用 Algolia 搜索(algolia_search)。如果你想使用本地搜索,可以按照以下步骤配置: 启用本地搜索 在 search 配置块下,设置 use: local_search,并确保 local_search 的配置项被正确设置。 12345678910search: use: local_search placeholder: "搜索文章..." # 你可以自定义搜索框的占位符 local_search: preload: true # 页面加载时预加载搜索数据 top_n_per_article: 1 # 每篇文章显示前 n 个搜索结果 unescape: false # 将 HTML 字符串转义为可读形式 pagination: enable: false # 不启用搜索结果分页 hitsPerPage: 8 # 每...
队列queue的用法
一、queue 是什么1234#include <queue>using namespace std;queue<int> q; 先进先出(FIFO) 数据结构 底层用 deque 实现(也可以用 list) 常用操作:入队、出队、访问队头/队尾 二、基本操作1️⃣ 入队12q.push(10); // 队尾插入 10q.push(20); 2️⃣ 出队1q.pop(); // 删除队头元素(不返回值) 3️⃣ 访问队头 / 队尾12cout << q.front(); // 队头元素cout << q.back(); // 队尾元素 4️⃣ 队列大小 / 是否为空12cout << q.size();if (q.empty()) cout << "queue is empty"; 三、遍历队列(注意!不能直接用 range-for)123456789queue<int> q;q.push(1);q.push(2);q.push...
map的用法
一、map 是什么12#include <map>map<Key, Value> mp; 键值对(key → value) key 自动有序(默认升序) 底层是红黑树 操作复杂度:O(log n) 二、基本用法1️⃣ 插入元素12345map<int, int> mp;mp[1] = 10;mp[3] = 30;mp[2] = 20; ⚠️ 用 [] 插入,如果 key 不存在,会自动创建并初始化 value 2️⃣ 访问元素1cout << mp[2]; // 20 ❗注意: 1cout << mp[100]; // 不存在,但会创建 key=100,value=0 3️⃣ 判断 key 是否存在(很重要)❌ 错误写法: 1if (mp[x]) // 不可靠 ✅ 正确写法: 1if (mp.count(x)) // 返回 0 或 1 或: 1if (mp.find(x) != mp.end()) 4️⃣ 删除元素12mp.erase(2); // 按 key 删除...
快速幂
12345678long long ksm(long long a, long long x) { long long res = 1; while(x) { if(x & 1) res = res * a % m; a = a * a % m, x >>= 1; } return res;} 这是一个**快速幂(Binary Exponentiation / 快速取模幂)**函数,用来计算: $$(a^x) \bmod m$$逐行直接讲代码逻辑 👇(按你偏好,给结论型解释) 函数整体作用1int ksm(int a, int x) 计算 a 的 x 次方,对 m 取模等价于:pow(a, x) % m,但更快、更安全 变量含义1int res = 1; res:保存最终结果 初始为 1,因为乘法单位元是 1 核心循环1while(x) {} 只要指数 x > 0 就继续 每一轮把 x 二进制右移一位 ...
栈stack的用法
stack 是什么?stack 是 栈,一种 后进先出(LIFO) 的数据结构。 类比:一摞盘子 只能从 顶部 放 只能从 顶部 取 需要的头文件1#include <stack> 通常还会配合: 12#include <iostream>using namespace std; 定义一个栈12stack<int> st; // 存 intstack<string> st2; // 存 string stack<T> 中的 T 可以是任意类型。 常用成员函数(核心) 函数 作用 st.push(x) 入栈 st.pop() 出栈(不返回值) st.top() 访问栈顶元素 st.empty() 是否为空 st.size() 元素个数 注意: pop() 不会返回元素 取元素用 top() 基本示例1234567891011121314151617181920#include <iostream>#include <stack>...
find()的用法
一、std::find(最常用)头文件 1#include <algorithm> 原型 12template<class InputIt, class T>InputIt find(InputIt first, InputIt last, const T& value); 作用 在区间 [first, last) 中查找 第一个等于 value 的元素 1️⃣ 在 vector 中查找123456789vector<int> a = {1, 3, 5, 7};auto it = find(a.begin(), a.end(), 5);if (it != a.end()) { cout << "找到了,下标 = " << it - a.begin() << endl;} else { cout << "没找到" << endl;} 📌 关键点 返回的是...





