一个本硕双非的小菜鸡,备战24年秋招,计划二刷完卡子哥的刷题计划,加油!
二刷决定精刷了,于是参加了卡子哥的刷题班,训练营为期60天,我一定能坚持下去,迎来两个月后的脱变的,加油!
推荐一手卡子哥的刷题网站,感谢卡子哥。代码随想录
在计算机编程中,字符串传统上是一个字符序列,可以是字面常量,也可以是某种变量。后者可能允许其元素发生变异并改变长度,或者它可能是固定的(在创建之后)。
字符串通常被认为是一种数据类型,通常被实现为字节(或单词)的数组数据结构,它使用某种字符编码存储一系列元素,通常是字符。
字符串也可以表示更通用的数组或其他序列(或列表)数据类型和结构。
28. 找出字符串中第一个匹配项的下标
Note:每次看到KMP的实现过程都有收获
class Solution {
public:
void getNext(int * next, const string& s) {
int j = -1;
next[0] = j;
for (int i = 1; i < s.size(); i++) {
while (j >= 0 && s[i] != s[j + 1]) {
j = next[j];
}
if (s[i] == s[j + 1]) {
j++;
}
next[i] = j;
}
}
int strStr(string haystack, string needle) {
if (needle.size() == 0) return 0;
int next[needle.size()];
getNext(next, needle);
int j = -1;
for (int i = 0; i < haystack.size(); i++) {
while (j >= 0 && haystack[i] != needle[j + 1])
j = next[j];
if (haystack[i] == needle[j + 1])
j++;
if (j == (needle.size() - 1))
return (i - needle.size() + 1);
}
return -1;
}
};
459. 重复的子字符串
Note:巧妙地解法
class Solution {
public:
bool repeatedSubstringPattern(string s) {
string t = s + s;
t.erase(t.begin());
t.erase(t.end() - 1);
if (t.find(s) == -1)
return false;
return true;
}
};
Note:KMP解法
class Solution {
public:
void getNext(int* next, const string& s) {
int j = -1;
next[0] = -1;
for (int i = 1; i < s.size(); i++) {
while (j >= 0 && s[i] != s[j + 1]) {
j = next[j];
}
if (s[i] == s[j + 1]) {
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] != -1 && len % (len - (next[len - 1] + 1)) == 0)
return true;
return false;
}
};
字符串或串(String)是由数字、字母、下划线组成的一串字符。它是编程语言中表示文本的数据类型。在程序设计中,字符串为符号或数值的一个连续序列。