当前位置:网站首页>402-字符串(题目:剑指Offer58-II.左旋转字符串、 28. 实现 strStr()、459.重复的子字符串)
402-字符串(题目:剑指Offer58-II.左旋转字符串、 28. 实现 strStr()、459.重复的子字符串)
2022-06-22 05:35:00 【liufeng2023】
题目:剑指Offer58-II.左旋转字符串

class Solution {
public:
string reverseLeftWords(string s, int n) {
string s1(s.begin(), s.begin()+n);
string s2(s.begin()+n, s.end());
return s2+s1;
}
};

28. 实现 strStr()

class Solution {
public:
void get_next(vector<int>& next, const string& str)
{
int j = 0;
int k = -1;
next[j] = k;
while (j < str.size() - 1)
{
if (-1 == k || str[j] == str[k])
{
j++;
k++;
next[j] = k;
}
else
{
k = next[k];
}
}
}
public:
int strStr(string haystack, string needle) {
vector<int> next(needle.size(), 0);
get_next(next, needle);
int i = 0;
int j = 0;
int size1 = haystack.size();
int size2 = needle.size();
while (i < size1 && j < size2)
{
if (-1 == j || haystack[i] == needle[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j == needle.size())
{
return i - j;
}
else
{
return -1;
}
}
};

459.重复的子字符串
自己加自己,掐头去尾找自己!

class Solution {
public:
bool repeatedSubstringPattern(string s) {
string s1(s+s);
string s2 = s1.substr(1, 2*s.size() - 2);
if(s2.find(s) == -1)
{
return false;
}
return true;
}
};

边栏推荐
- MinGW download and installation
- Use and decoupling of the mobile terminal's realm database, and use OC realm safely across threads
- Tensorflow 2.x(keras)源码详解之第十四章:keras中的回调及自定义回调
- Xshell download and installation (solve the problem of expired evaluation)
- Reptile initial and project
- Grabcut analysis
- 爬虫初始及项目
- innosetup判断程序已经运行方法
- C指针的理解
- Beifeng helps Nanchang Emergency Management Bureau to build a public-private integrated emergency communication guarantee network
猜你喜欢

Xshell download and installation (solve the problem of expired evaluation)

GrabCut分析

PID笔记

RGB及sRGB与XYZ坐标转换

AUTOSAR from getting started to mastering 100 lectures (150) -soa architecture and Application

Someone always asks me: how to play independent station? Three cases, you will understand after reading

为Cortex-M4编写优化的DSP代码

Use and decoupling of the mobile terminal's realm database, and use OC realm safely across threads

Hide symbol of dynamic library

C指针的理解
随机推荐
我不建议你工作太拼命
Unity app提高设备可用性
Market development trend forecast and investment risk outlook report of China's silicon carbide industry 2022-2027
Go language uses zap log Library
微信小程序开发 第一周:页面设置、页面跳转、数据绑定
JTAG interface
Using SystemVerilog to describe a state machine
Gerrit Code Review Setup
Why is "CPS alliance marketing" the most cost-effective promotion method?
Signal output library
错误:note: module requires Go 1.17
北峰助力南昌市应急管理局打造公专融合应急通信保障网
记录在处理SIF数据中,遇到的一些问题及解决过程
Reptile initial and project
Sourcetree reported an error SSH failure
Academic circle of cattle II (daily question 49 in spring)
Conversion between gray code and binary
数据的存储(进阶)
时序构成的测试平台
QEMU ARM interrupt system architecture