明白了!这里帮你整理一份【C++ 笔试刷题 Day 29】的示范内容,包含几个典型刷题方向与代码示例,助你高效备考。
【C++】笔试刷题 Day 29
一、题目方向及重点
- 字符串处理
- 字符串反转
- 判断回文串
- 字符串匹配
- 链表操作
- 单链表反转
- 环检测
- 合并两个有序链表
- 数组相关
- 子数组和
- 排序算法实现
- 滑动窗口技巧
- 递归与DFS
- 组合问题
- 子集生成
- 矩阵路径搜索
二、经典题目示例
1. 判断字符串是否回文
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(const string& s) {
int left = 0, right = (int)s.size() - 1;
while (left < right) {
if (s[left++] != s[right--])
return false;
}
return true;
}
int main() {
string s = "level";
cout << (isPalindrome(s) ? "Yes" : "No") << endl;
return 0;
}
2. 反转单链表
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
while (head) {
ListNode* next = head->next;
head->next = prev;
prev = head;
head = next;
}
return prev;
}
3. 合并两个有序数组
#include <vector>
using namespace std;
vector<int> mergeSortedArrays(const vector<int>& a, const vector<int>& b) {
vector<int> res;
int i = 0, j = 0;
while (i < (int)a.size() && j < (int)b.size()) {
if (a[i] < b[j]) res.push_back(a[i++]);
else res.push_back(b[j++]);
}
while (i < (int)a.size()) res.push_back(a[i++]);
while (j < (int)b.size()) res.push_back(b[j++]);
return res;
}
4. DFS 生成子集
#include <vector>
using namespace std;
void dfs(vector<int>& nums, int idx, vector<int>& path, vector<vector<int>>& res) {
res.push_back(path);
for (int i = idx; i < (int)nums.size(); i++) {
path.push_back(nums[i]);
dfs(nums, i + 1, path, res);
path.pop_back();
}
}
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
vector<int> path;
dfs(nums, 0, path, res);
return res;
}
三、刷题建议
- 重点掌握指针操作、边界条件。
- 多练习 STL 容器与算法的结合。
- 注重代码的时间复杂度与空间复杂度分析。
如果你想要,我还可以帮你:
- 定制专属刷题计划(比如链表、字符串、动态规划专项)
- 提供笔试高频题目汇总与解析
- 生成详细代码注释和讲解视频脚本
需要哪种帮助,告诉我吧!
发表回复