随笔分类 -  基础算法

二分,前缀和,差分,排序,位运算,RMQ,高精度,双指针,离散化
摘要:十进制数m有01234567共8位 取123位置的二进制数方法 (m >> 4) & 0x07 取4567位置的二进制数方法 m & 0x0f 例题 响应报文时间 样例1 输入 3 0 20 1 10 8 20 输出 11 样例2 输入 2 0 255 200 60 输出 260 样例3 输入 1 阅读全文
posted @ 2025-02-14 23:35 Tshaxz 阅读(7) 评论(0) 推荐(0) 编辑
摘要:矩阵为nm型,n为行,m为列,n+m1为对角线数量 //撇对角线 for (int i = 0; i < n + m - 1; i++) { for (int j = max(0, i - m + 1); j <= min(i, n - 1); j++) { co 阅读全文
posted @ 2025-02-14 03:02 Tshaxz 阅读(2) 评论(0) 推荐(0) 编辑
摘要:sort降序排序 写法一 int q[N]; sort(q, q + n, greater<int>()); //q为数组名,n为数组长度 vector<int> q; sort(q.begin(), q.end(), greater<int>()); 写法二 int q[N]; sort(q, q 阅读全文
posted @ 2025-02-13 18:01 Tshaxz 阅读(23) 评论(0) 推荐(0) 编辑
摘要:头文件: #include <cstring> #include <sstream> 题目未给定数据个数,一般多在模拟题用到 string str; getchar(); //如果getline()之前已经读入过其他数据,需要用getchar()读掉前面剩下的回车 getline(cin, str) 阅读全文
posted @ 2025-02-07 17:28 Tshaxz 阅读(14) 评论(0) 推荐(0) 编辑
摘要:读取时间 HH:MM:SS:NN型 函数 //转为毫秒表示,方便比大小 int get(string time) { int h, m, s, ms; //从字符串内读数据 sscanf(time.c_str(), "%d:%d:%d.%d", &h, &m, &s, &ms); return h 阅读全文
posted @ 2025-02-07 17:24 Tshaxz 阅读(3) 评论(0) 推荐(0) 编辑
摘要:示例:给出一行用逗号隔开的数字,要求把这串数组存到数组a中 21,30,62,5,31 写法一:双指针写法 int n = 0; int a[N]; void getnum(string &s) { for (int i = 0; i < s.size(); i++) { int j = i, nu 阅读全文
posted @ 2025-01-31 15:39 Tshaxz 阅读(4) 评论(0) 推荐(0) 编辑
摘要:摩尔投票法 学习资料: 1.算法讲解116【扩展】摩尔投票大加强,线段树里捉海王 练习题 169.多数元素 写法一 class Solution { public: //摩尔投票法 https://www.bilibili.com/video/BV1Br421G7hB/ int majorityEl 阅读全文
posted @ 2025-01-29 15:55 Tshaxz 阅读(3) 评论(0) 推荐(0) 编辑
摘要:题目 样例 输入 1 20 输出 3 4 5 5 12 13 8 15 17 解释,1-20内有多组勾股数,但满足两两互质的只有上述三组。下图是1-20内的全部勾股数组 思路: n的范围在1e4,三重for循环会超时,所以可以枚举a,b,用ab计算c,看c是否满足条件,可以做到O(n2)阅读全文
posted @ 2025-01-26 23:35 Tshaxz 阅读(26) 评论(0) 推荐(0) 编辑
摘要:1.数据量较小时,可以用队列模拟 2.数据量较大时,需要使用递推公式 公式推导举例 找规律,举个例子 n = 5, m = 4, n下标从0开始,m下标从1开始 f[1] = 0; f[2] = (f[1] + a[4]) % i; f[3] = (f[2] + a[3]) % i; f[4] = 阅读全文
posted @ 2025-01-25 23:50 Tshaxz 阅读(7) 评论(0) 推荐(0) 编辑
摘要:01背包 恰好等于条件下求最小物品数 MELON的难题 每个物品(石头)的价值w[i]就是其自己的个数,为1 体积题目已给出。 状态定义:f[i][j]表示在前i个物品中选,且体积总和恰好等于j需要的物品个数的最小值 初始化: f[i][0] = 0 , 1 <= i <= n f[0][j] = 阅读全文
posted @ 2025-01-23 12:26 Tshaxz 阅读(2) 评论(0) 推荐(0) 编辑
摘要:77. 翻转单词顺序 讲解视频:https://www.acwing.com/video/2728/ 单词反转函数(字符串只能包含空格和字母) class Solution { public: string reverseWords(string s) { reverse(s.begin(), s. 阅读全文
posted @ 2025-01-20 00:59 Tshaxz 阅读(1) 评论(0) 推荐(0) 编辑
摘要:快速排序 912. 排序数组 class Solution { public: void quick_sort(vector<int>& q, int l, int r) { if(l >= r) return; int i = l - 1, j = r + 1, x = q[l + r >> 1] 阅读全文
posted @ 2022-09-06 18:48 Tshaxz 阅读(17) 评论(0) 推荐(0) 编辑
摘要:高精度模板 加法 vector<int> add(vector<int>& A, vector<int>& B) // 高精度加法 { vector<int> C; int t = 0; for (int i = 0; i < A.size() || i < B.size(); i ++ ) { i 阅读全文
posted @ 2022-09-01 21:00 Tshaxz 阅读(26) 评论(0) 推荐(0) 编辑
摘要:M进制转N进制 3374. 进制转换2 #include <iostream> #include <algorithm> #include <cstring> #include <vector> using namespace std; int main() { int a, b; string s 阅读全文
posted @ 2022-08-07 21:01 Tshaxz 阅读(55) 评论(0) 推荐(0) 编辑
摘要:记录LeetCode 热题 HOT 100 代码 1. 两数之和 class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> hash; for(int i 阅读全文
posted @ 2022-07-18 19:58 Tshaxz 阅读(274) 评论(0) 推荐(0) 编辑
摘要:62. 丑数 class Solution { public: int getUglyNumber(int n) { vector<int> q(1, 1); int i = 0, j = 0, k = 0; while( -- n) // 循环 n - 1 次 { int t = min(q[i] 阅读全文
posted @ 2022-05-17 17:43 Tshaxz 阅读(23) 评论(0) 推荐(0) 编辑
摘要:求下一个序列的考虑方法: 尽量保证地位不变,变高位。所以可以从后往前去考虑 视频讲解:LeetCode 31. 下一个排列 模板题: 31. 下一个排列 class Solution { public: void nextPermutation(vector<int>& nums) { int k 阅读全文
posted @ 2022-05-05 21:50 Tshaxz 阅读(57) 评论(0) 推荐(0) 编辑
摘要:map按value排序/自定义排序规则 sort不能直接排map,需要把map放入到vector中再对vector排序 方法如下 using PSI = pair<string, int>; vector<PSI> arr; unordered_map<string, int> mp 阅读全文
posted @ 2022-05-04 18:18 Tshaxz 阅读(42) 评论(0) 推荐(0) 编辑
摘要:1346. 回文平方 #include <iostream> #include <algorithm> #include <cstring> using namespace std; char get(int x) // 将x转化为b进制 { if(x <= 9) return x + '0'; r 阅读全文
posted @ 2022-04-18 22:11 Tshaxz 阅读(26) 评论(0) 推荐(0) 编辑
摘要:13. 找出数组中重复的数字 class Solution { public: int duplicateInArray(vector<int>& nums) { if(nums.empty()) return -1; unordered_map<int, int> hash; int n = nu 阅读全文
posted @ 2022-04-16 22:08 Tshaxz 阅读(16) 评论(0) 推荐(0) 编辑

Language: HTML
点击右上角即可分享
微信分享提示