当前位置:网站首页>Brush questions record----string
Brush questions record----string
2022-07-30 19:40:00 【HandsomeDog_L】
目录
next数组----Storage pattern string equals the longest suffix before
刷题记录----字符串
反转字符串
class Solution {
public void reverseString(char[] s) {
int l = 0;
int r = s.length - 1;
while (l < r) {
s[l] ^= s[r]; //构造 a ^ b 的结果,并放在 a 中
s[r] ^= s[l]; //将 a ^ b 这一结果再 ^ b ,存入b中,此时 b = a, a = a ^ b
s[l] ^= s[r]; //a ^ b 的结果再 ^ a ,存入 a 中,此时 b = a, a = b 完成交换
l++;
r--;
}
}
}
反转字符串||
给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转.
如果剩余字符少于 k 个,则将剩余字符全部反转.
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样.
class Solution {
public String reverseStr(String s, int k) {
char[] ch = s.toCharArray();
// 1. 每隔 2k 个字符的前 k 个字符进行反转
for (int i = 0; i< ch.length; i += 2 * k) {
// 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符
if (i + k <= ch.length) {
reverse(ch, i, i + k -1);
continue;
}
// 3. 剩余字符少于 k 个,则将剩余字符全部反转
reverse(ch, i, ch.length - 1);
}
return new String(ch);
}
// 定义翻转函数
public void reverse(char[] ch, int i, int j) {
for (; i < j; i++, j--) {
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
}
}
}替换空格
class Solution {
public:
string replaceSpace(string s) {
int count = 0; // 统计空格的个数
int sOldSize = s.size();
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
count++;
}
}
// 扩充字符串s的大小,也就是每个空格替换成"%20"之后的大小
s.resize(s.size() + count * 2);
int sNewSize = s.size();
// 从后先前将空格替换为"%20"
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;
}
};
翻转字符串里的单词
左旋字符串
把字符串前nTurn to the end
反转区间为前n的子串
反转区间为n到末尾的子串
反转整个字符串
class Solution {
public String reverseLeftWords(String s, int n) {
int len=s.length();
StringBuilder sb=new StringBuilder(s);
reverseString(sb,0,n-1);
reverseString(sb,n,len-1);
return sb.reverse().toString();
}
public void reverseString(StringBuilder sb, int start, int end) {
while (start < end) {
char temp = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, temp);
start++;
end--;
}
}
}
KMPAlgorithm classic topic
实现strStr()
文本串 aabaabaaf
模式串 aabaaf
前缀:String contains initials excluding last letter
后缀:......
next数组----Storage pattern string equals the longest suffix before
| a | 0 |
|---|---|
| aa | 1 |
| aab | 0 |
| aaba | 1 |
| aabaa | 2 |
| aabaaf | 0 |
Can be found that does not match the location of thef,那么就跳转到fBefore a subscript corresponds to the value of the place,也就是2We'll go on with the match
时间复杂度O(n+m)
构造next数组
初始化
定义两个指针i和j,j指向前缀末尾位置,i指向后缀末尾位置.
处理前后缀不相同的情况
j回退----j = next[j-1];
处理前后缀相同的情况
j++
class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) return 0;
int[] next = new int[needle.length()];
getNext(next, needle);
int j = 0;
for (int i = 0; i < haystack.length(); i++) {
while (j > 0 && needle.charAt(j) != haystack.charAt(i))
j = next[j - 1];
if (needle.charAt(j) == haystack.charAt(i))
j++;
if (j == needle.length())
return i - needle.length() + 1;
}
return -1;
}
private void getNext(int[] next, String s) {
int j = 0;
next[0] = 0;
for (int i = 1; i < s.length(); i++) {
while (j > 0 && s.charAt(j) != s.charAt(i))
j = next[j - 1];
if (s.charAt(j) == s.charAt(i))
j++;
next[i] = j;
}
}
}
重复的子字符串
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000.
如果len % (len - next[len - 1] ) == 0 ,则说明 (数组长度-最长相等前后缀的长度) 正好可以被 数组的长度整除,说明有该字符串有重复的子字符串.
数组长度减去最长相同前后缀的长度相当于是第一个周期的长度,也就是一个周期的长度,如果这个周期可以被整除,就说明整个数组就是这个周期的循环.
class Solution {
public:
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;
}
};
边栏推荐
- Vulkan开启特征(feature)的正确姿势
- The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
- Download Win11 how to change the default path?Download Win11 change the default path method
- MySQl数据库————DQL数据查询语言
- 又一家公司面试的内容
- Tensorflow2.0 confusion matrix does not match printing accuracy
- MongoDB打破了原则引入SQL?
- 监听开机广播
- MindSpore:【JupyterLab】查看数据时报错
- nlohmann json 使用指南【visual studio 2022】
猜你喜欢
随机推荐
部分分类网络性能对比
来了!东方甄选为龙江农产品直播带货
【Node实现数据加密】
M3SDA:用于多源域自适应的矩匹配
[hbuilder] cannot run some projects, open the terminal and cannot enter commands
阿里云武林头条活动分享
Range.CopyFromRecordset method (Excel)
【flink】报错整理 Could not instantiate the executor. Make sure a planner module is on the classpath
Tensorflow2.0 confusion matrix does not match printing accuracy
Google's AlphaFold claims to have predicted almost every protein structure on Earth
The technology is very powerful, do you still need to "manage up"?
MySQl数据库————DQL数据查询语言
【MindSpore1.2.0-rc1产品】num_workers问题
又一家公司面试的内容
golang日志库zerolog使用记录
刷题记录----字符串
MySQL数据库之JDBC编程
VBA batch import Excel data into Access database
Scala学习:breakable
DM8: Single database and single instance to build a local data guard service









