刷leetcode hot100返航版--字符串6/15
字符串基本操作
#include <string>
#include <cctype> // 字符处理函数// 包含toupper, tolower
#include <algorithm> // transformusing namespace std;
// 1. 创建与初始化
string s1 = "Hello";
string s2(5, 'a'); // "aaaaa"// 2. 基本操作
int len = s1.length(); // 或 s1.size()
char c = s1[0]; // 访问字符
s1 += " World"; // 拼接
s1.append("!"); // 追加// 3. 子串操作
string sub = s1.substr(6, 5); // "World"
int pos = s1.find("World"); // 查找位置// 4. 插入与删除
s1.insert(5, ","); // "Hello, World!"
s1.erase(5, 1); // 删除逗号// 5. 比较
if(s1 == "Hello World!") { ... }
int cmp = s1.compare("Apple"); // 字典序比较//6,判断是不是字母
if((c<='z' && c>='a') ||( c<='Z' && c>='A')
//7变化大小写
char a = toupper(c);
or tolower(c)
听说字符串很重要
KMP算法实现
回文串的动态规划处理
字符串解析(如计算器表达式)
字典树实现单词统计
双指针法解决子串问题
听deepseek怎么说
deepseek怎么说-CSDN博客
### 一、字符串匹配(KMP算法)
**考点**:实现高效的字符串匹配
- **LeetCode 28. 实现 strStr()**
- 实现子串查找(KMP经典应用)
- **牛客网 NC149 kmp算法**
- 实现KMP算法
- **LeetCode 214. 最短回文串**
- KMP的变种应用---
### 二、回文串处理
**考点**:动态规划/中心扩展法
- **LeetCode 5. 最长回文子串**
- 求最长回文子串(必考题)
- **LeetCode 647. 回文子串**
- 统计所有回文子串数量
- **LeetCode 131. 分割回文串**
- 回溯+动态规划综合应用---
### 三、子串问题
**考点**:滑动窗口/双指针
- **LeetCode 3. 无重复字符的最长子串**
- 滑动窗口经典题(高频)
- **牛客网 NC127 最长公共子串**
- 动态规划求公共子串
- **LeetCode 76. 最小覆盖子串**
- 滑动窗口进阶(困难)---
### 四、字符串解析
**考点**:状态机/栈应用
- **LeetCode 224. 基本计算器**
- 解析含括号的表达式(栈应用)
- **LeetCode 468. 验证IP地址**
- 字符串分割+规则验证
- **LeetCode 394. 字符串解码**
- 嵌套结构解析(栈应用)---
### 五、字符串操作
**考点**:原地修改/高效处理
- **LeetCode 151. 翻转字符串里的单词**
- 原地翻转(常见笔试)
- **LeetCode 557. 反转字符串中的单词 III**
- 基础翻转操作
- **牛客网 NC89 字符串变形**
- 大小写转换+单词翻转---
### 六、字典树应用
**考点**:前缀处理/词频统计
- **LeetCode 208. 实现 Trie (前缀树)**
- 字典树基础实现
- **LeetCode 692. 前K个高频单词**
- Trie+堆的综合应用
- **LeetCode 720. 词典中最长的单词**
- Trie树应用---
### 七、正则表达式
**考点**:模式匹配
- **LeetCode 10. 正则表达式匹配**
- 实现正则引擎(动态规划)---
### 八、综合难题
**考点**:多知识点结合
- **LeetCode 32. 最长有效括号**
- 字符串+栈+动态规划
- **LeetCode 227. 基本计算器 II**
- 解析含乘除的表达式
- **LeetCode 44. 通配符匹配**
- 带通配符的字符串匹配---
### 高频题目训练建议
1. **必刷基础题**:
- LeetCode 3(滑动窗口)
- LeetCode 5(回文串)
- LeetCode 28(KMP)
- LeetCode 151(字符串操作)2. **进阶训练**:
- LeetCode 76(最小覆盖子串)
- LeetCode 224(表达式解析)
- LeetCode 394(嵌套解码)3. **牛客网专项**:
- NC149(KMP实现)
- NC127(公共子串)
- NC89(字符串变形)---
### 机试技巧总结
1. **KMP模板**:必须掌握next数组的构建
```cpp
void getNext(string& p, vector<int>& next) {
next[0] = -1;
int i = 0, j = -1;
while (i < p.size()) {
if (j == -1 || p[i] == p[j]) {
next[++i] = ++j;
} else {
j = next[j];
}
}
}
```2. **滑动窗口框架**:
```cpp
int left = 0, right = 0;
while (right < s.size()) {
// 1. 扩大窗口
char c = s[right++];
// 2. 更新状态
// 3. 满足条件时收缩窗口
while (window needs shrink) {
char d = s[left++];
// 更新状态
}
}
```3. **回文串中心扩展模板**:
```cpp
for (int i = 0; i < n; i++) {
// 奇回文
expand(s, i, i);
// 偶回文
expand(s, i, i+1);
}
```建议针对每个考点精刷3-5题,重点掌握模板化代码,机试中字符串题目出现频率高达30%以上。
1.为视频标题生成标签【6/15】
力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
问题:错误用例:
原来:空格f....
正确输出:f...
错误输出:F..
应该再加一个判断是不是第一个字母的标记
class Solution {
public:string generateTag(string caption) {//驼峰命名字符串 ,并在前面加上 '#'//移除 所有不是英文字母的字符//截断 为最多 100 个字符string res;res+='#';//字符串添加int flag = 0;int first = 1;for(auto c:caption){if((c>='a' && c <='z') || (c>='A' && c <='Z')){char e;if(flag == 1){if(first == 1){e = tolower(c); //考虑第一个字字母}else{e = toupper(c);}}else{e = tolower(c);}res+=e;flag = 0;first = 0;}else if(c == ' '){// fflag = 1;}else{continue;}}//将结果 截断 为最多 100 个字符。???string ans;if(res.size()>100){ans = res.substr(0,100);}else{ans = res;} return ans;}
};