当前位置:网站首页>leetcode--字符串
leetcode--字符串
2022-06-24 08:03:00 【雨幕丶】
KMP算法:解决字符串匹配问题
前缀:包含首字母不包含尾字母的子串
文本串:aabaabaaf
模式串:aabaaf
求最长相等前后缀:
a 0 aa 1 aab 0 aaba 1 aabaa 2 aabaaf 0
前缀表是 010120
next数组 (遇到冲突以后要回退):-1 0 -1 0 1 -1
next[0] = 0 表示当只有一个字符的字符串,相同前后缀的最大长度是0
344 反转字符串
void reverseString(vector<char>& s) {
for(int i = 0, j = s.size()-1; i < s.size()/2; i++,j--){
swap(s[i],s[j]);
}
}541 每隔2k个字符反转前k个字符
string reverseStr(string s, int k) {
for(int i = 0; i < s.size();i = i + (2 * k)){
//1.每隔2k个字符将前k个字符反转
//2.剩余字符大于k 小于2k 将前k个反转
if(i + k <= s.size()){
reverse(s.begin()+i,s.begin()+i+k);
continue;
}
//3.剩余字符少于k个,将剩余字符全部反转
reverse(s.begin()+i,s.end());
}
return s;
}58 左旋转字符串
string reverseLeftWords(string s, int n) {
/*
局部反转+整体反转 达到左旋转的目的
1.反转区间[0,n-1]
2.反转区间[n,end]
3.反转整个字符串
*/
reverse(s.begin(),s.begin() + n);
reverse(s.begin()+n,s.end());
reverse(s.begin(),s.end());
return s;
}5替换空格
string replaceSpace(string s){
//统计空格个数
int count = 0;
int sOldSize = s.size();
for(int i = 0; i < sOldSize;i++){
if(s[i] == ' ')
count++;
}
//扩充字符串s的大小
s.resize(sOldSize + count * 2);
int sNewSize = s.size();
//从后向前替换空格
for(int i = sNewSize - 1,j = sOldSize - 1 ; j < i ; i--,j--){
if(s[j] != ' '){
s[i] = s[j];
}else{
s[i] = '0';
s[i-1] = '2';
s[i-2] = '%';
i -= 2;
}
}
return s;
}151 反转字符串中的单词
原来: abc_def 反转后: def_abc
先反转每个单词 cba_fed ;在反转整个字符串 def_abc
28 实现strStr
kmp算法
void getNext(int* next,string& s){
int j = 0;//j 是前缀末尾
next[0] = 0;
for(int i = 1; i < s.size(); i++){ //i是后缀末尾
while(j > 0 && s[i] != s[j]){
j = next[j - 1];
}
if(s[i] == s[j]){
j++;
}
next[i] = j;
}
}
int strStr(string haystack, string needle) {
if(needle.size() == 0) return 0;
int next[needle.size()];
getNext(next,needle); //next数组中的值表示最长相等前后缀
int j = 0;
for(int i = 0; i < haystack.size(); i++){
while(j > 0 && haystack[i] != needle[j]){
j = next[j - 1];
}
if(haystack[i] == needle[j]){
j++;
}
if(j == needle.size()){
return (i - needle.size() + 1); // 3 - 2 + 1
}
}
return -1;
}459 重复的子字符串
void getNext (int* next, const string& s){
next[0] = 0;
int j = 0;
for(int i = 1;i < s.size(); i++){
while(j > 0 && s[i] != s[j]) {
j = next[j - 1];
}
if(s[i] == s[j]) {
j++;
}
next[i] = j;
}
}
bool repeatedSubstringPattern (string s) {
if (s.size() == 0) {
return false;
}
int next[s.size()];
getNext(next, s);
int len = s.size();
if (next[len - 1] != 0 && len % (len - (next[len - 1] )) == 0) {
return true;
}
return false;
}边栏推荐
- Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
- A tip to read on Medium for free
- 4275. Dijkstra sequence
- Data middle office: middle office practice and summary
- 1528. rearrange strings
- Dynamic saving and recovery of FPU context under risc-v architecture
- [quantitative investment] discrete Fourier transform to calculate array period
- Support vector machine (SVC, nusvc, linearsvc)
- When programmers are asked if they can repair computers... | daily anecdotes
- MySQL - SQL statement
猜你喜欢

陆奇:我现在最看好这四大技术趋势

当程序员被问会不会修电脑时… | 每日趣闻

Ebanb B1 Bracelet brush firmware abnormal interrupt handling

Yolox backbone -- implementation of cspparknet

深入了解 border
![[Niuke] length of the last word of HJ1 string](/img/8b/6ba6506415b8112aea957ac5647121.png)
[Niuke] length of the last word of HJ1 string
![[quantitative investment] discrete Fourier transform to calculate array period](/img/0d/aac02463ff403fb1ff871af5ff91fa.png)
[quantitative investment] discrete Fourier transform to calculate array period

leetcode——错误的集合

【ES6闯关】Promise堪比原生的自定义封装(万字)

The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
随机推荐
Chapter 7 operation bit and bit string (III)
Kaformer personal notes
MYCAT read / write separation and MySQL master-slave synchronization
[Niuke] convert string to integer
Squid proxy application
关于 GIN 的路由树
Numpy numpy中的np.c_和np.r_详解
“论解不了数独所以选择做个数独游戏这件事”
Go 语言项目开发实战目录
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
YOLOX backbone——CSPDarknet的实现
[redis implements seckill business ①] seckill process overview | basic business implementation
学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
软件系统依赖关系分析
How to import MDF and LDF files into MySQL workbench
Applet wx show
L01_一条SQL查询语句是如何执行的?
Ebanb B1 Bracelet brush firmware abnormal interrupt handling
Unable to change the virtual machine power status and report an error solution
可直接套用的Go编码规范