关于字符串算法总结
字符,String 是由一系列字符组成的。字符的类型是char,可能有216 个值。
常用函数
-
charAt() - 索引 -
length() - 长度 -
substring() - 提取子串 -
StringBuilder - 连接 -
char[] - 字符串数组 转换方式 :s.toCharArray()
Leecode
344. 反转字符串 - 力扣(LeetCode) (leetcode-cn.com)
异或:^
首先 a = a, b = b
令a = a^b;
则
b = a^b = a^b^b = a
a = a^b = a^b^a = b
class Solution {
public void reverseString(char[] s) {
int start = 0;
int end = s.length-1;
while(start < end){
s[start] ^= s[end];
s[end] ^= s[start];
s[start] ^= s[end];
start ++;
end --;
}
}
}
541. 反转字符串 II - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public String reverseStr(String s, int k) {
char[] ch = s.toCharArray();
for(int i = 0; i< ch.length; i += 2*k){
int start = i;
int end = Math.min(start+k-1,s.length()-1);
while(start < end){
ch[start] ^= ch[end];
ch[end] ^= ch[start];
ch[start] ^= ch[end];
end --;
start ++;
}
}
return new String(ch);
}
}
剑指 Offer 05. 替换空格 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public String replaceSpace(String s) {
StringBuilder str = new StringBuilder();
for(int i =0; i< s.length();i++){
if(s.charAt(i) ==' ') str.append("%20");
else str.append(s.charAt(i));
}
return str.toString();
}
}
151. 翻转字符串里的单词 - 力扣(LeetCode) (leetcode-cn.com)
- 移除多余空格
- 将整个字符串反转
- 将每个单词反转
class Solution {
public String reverseWords(String s) {
char[] str = s.toCharArray();
char[] newstr = new char[str.length+1];
int index = 0;
int i = str.length-1;
for(;i>=0;i--){
while(i>=0 && str[i] == ' ') i--;
int right = i;
while(i>=0 &&str[i] != ' ') i--;
for(int j = i+1;j<=right;j++){
newstr[index++] = str[j];
if(j == right) newstr[index++] = ' ';
}
}
if(index == 0) return "";
else return new String(newstr,0,index-1);
}
}
剑指 Offer 58 - II. 左旋转字符串 - 力扣(LeetCode) (leetcode-cn.com)
- 反转区间为前n的子串
- 反转区间为n到末尾的子串
- 反转整个字符串
class Solution {
public String reverseLeftWords(String s, int n) {
StringBuilder sb=new StringBuilder(s);
reverseString(0,n-1,sb);
reverseString(n,s.length()-1,sb);
return sb.reverse().toString();
}
public void reverseString(int start,int end,StringBuilder sb){
while(start < end){
char temp = sb.charAt(start);
sb.setCharAt(start,sb.charAt(end));
sb.setCharAt(end,temp);
start ++;
end --;
}
}
}
朴素解法
28. 实现 strStr() - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public int strStr(String haystack, String needle) {
int n = haystack.length(), m = needle.length();
char[] s = haystack.toCharArray(), p = needle.toCharArray();
for (int i = 0; i <= n - m; i++) {
int a = i, b = 0;
while (b < m && s[a] == p[b]) {
a++;
b++;
}
if (b == m) return i;
}
return -1;
}
}
KMP算法
代码随想录讲解的KMP算法B站视频:帮你把KMP算法学个通透!(理论篇)_哔哩哔哩_bilibili
主要理解:
- 前缀后缀
- next数组的构造方法
- 前缀表和next数组之间的关系
- 使用next数组来匹配
28. 实现 strStr() - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public int strStr(String haystack, String needle) {
if(needle.length() == 0) return 0;
int n = haystack.length();
int m = needle.length();
String ss = " " + haystack;
String pp = " " + needle;
char[] s= ss.toCharArray();
char[] p = pp.toCharArray();
int[] next = new int[m+1];
for(int i=2,j=0;i<=m;i++){
while(j>0 && p[i] != p[j+1]) j = next[j];
if(p[i] == p[j+1]) j++;
next[i] = j;
}
for(int i =1,j=0;i<=n;i++){
while(j>0&& s[i] != p[j+1]) j = next[j];
if(s[i] == p[j+1]) j++;
if(j == m) return i-m;
}
return -1;
}
}
459. 重复的子字符串 - 力扣(LeetCode) (leetcode-cn.com)
指路:leetcode-master/0459.重复的子字符串.md at master · youngyangyang04/leetcode-master (github.com)
class Solution {
public boolean repeatedSubstringPattern(String s) {
if(s.isEmpty()) return false;
int n = s.length();
String ss = " " + s;
char[] chars = ss.toCharArray();
int[] next = new int[n+1];
for(int i =2,j=0;i<=n;i++){
while(j>0 && chars[i] != chars[j+1]) j = next[j];
if(chars[i] == chars[j+1]) j++;
next[i] = j;
}
if(next[n] >0 && n%(n-next[n]) == 0) return true;
return false;
}
}
|