【算法题解答·四】字符串操作
接上文【算法方法总结·四】字符串操作的一些技巧和注意事项
滑动窗口相关题目如下:
151.反转字符串中的单词
(1)使用 API
\\s
:匹配任意空白字符;+
:匹配一个或多个前面的字符\\s+
:任意多个空白字符
class Solution {
public String reverseWords(String s) {
// 除去开头和末尾的空白字符
s = s.trim();
// 正则匹配连续的空白字符作为分隔符分割
List<String> wordList = Arrays.asList(s.split("\\s+"));
Collections.reverse(wordList);
return String.join(" ", wordList);
}
}
(2)自定义方法
class Solution {
public String reverseWords(String s) {
StringBuilder sb = reverseSpace(s);
reverseString(sb, 0, sb.length() - 1); // 1、移除多余空格
reverseEachWord(sb); // 2、将整个字符串反转
return sb.toString(); // 3、将每个单词反转
}
// 1、移除多余空格
private StringBuilder reverseSpace(String s) {
int start = 0;
int end = s.length() - 1;
while (s.charAt(start) == ' ') start++; // 移除串首空格
while (s.charAt(end) == ' ') end--; // 移除串尾空格
StringBuilder sb = new StringBuilder();
while (start <= end) {
char c = s.charAt(start);
// 当前字符c与sb串尾字符不同时为' '时,加入sb
if (c != ' ' || sb.charAt(sb.length() - 1) != ' ') {
sb.append(c);
}
start++;
}
return sb;
}
// 2、将整个字符串反转
public void reverseString(StringBuilder sb, int start, int end) {
while (start < end) {
char t = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, t);
start++;
end--;
}
}
// 3、将每个单词反转
private void reverseEachWord(StringBuilder sb) {
int start = 0;
int end = 1;
int n = sb.length();
while (start < n) {
// 找到单词末尾的空格的索引end
while (end < n && sb.charAt(end) != ' ') {
end++;
}
// end-1为单词末尾的索引
reverseString(sb, start, end - 1);
start = end + 1;
end = start + 1;
}
}
}
28.找出字符串中第一个匹配项的下标
class Solution {
// next就为前缀表
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;
}
// 得到next数组,其实就是PM数组
private void getNext(int[] next, String s) {
char[] ss = s.toCharArray();
int j = 0;
next[0] = 0;
for (int i = 1; i < s.length(); i++) {
while (j > 0 && ss[j] != ss[i]) j = next[j - 1];
if (ss[j] == ss[i]) j++;
next[i] = j;
}
}
}
459.重复的子字符串
方法一:技巧法
判断字符串
s
是否由重复子串组成,只要两个s
拼接在一起,里面还出现一个s
的话,就说明是由重复子串组成
方法二:KMP
-
一个字符串
s
是由重复子串组成,那么 最长相等前后缀不包含的子串一定是字符串s
的最小重复子串 -
next[s.length() - 1]
指的是字符串s
的最长相等前后缀长度 -
如果
len % (len - (next[len - 1])) == 0
,则说明数组的长度正好可以被最长相等前后缀不包含的子串的长度整除 ,说明该字符串有重复的子字符串
class Solution {
public boolean repeatedSubstringPattern(String s) {
if (s.length() == 0) return false;
int len = s.length();
int[] next = new int[len];
getNext(next, s);
// 最长相等前后缀长度 > 0,且有重复的子字符串
if (next[len- 1] > 0 && len % (len - next[len - 1]) == 0) {
return true;
}
return false;
}
// 得到next数组,其实就是PM数组
private void getNext(int[] next, String s) {
char[] ss = s.toCharArray();
int j = 0;
next[0] = 0;
for (int i = 1; i < s.length(); i++) {
while (j > 0 && ss[j] != ss[i]) j = next[j - 1];
if (ss[j] == ss[i]) j++;
next[i] = j;
}
}
}
算法题解答系列
【算法题解答·一】二分法
【算法题解答·二】双指针法
【算法题解答·三】滑动窗口
【算法题解答·四】字符串操作
【算法题解答·五】链表操作
【算法题解答·六】栈队列堆