344.反转字符串
541.反转字符串Ⅱ
剑指Offer 05.替换空格
151.反转字符串中的单词
剑指Offer58-Ⅱ.左旋转字符串
语言:Java
考点:字符串
链接:https://leetcode.cn/problems/reverse-string/
class Solution {
public void reverseString(char[] s) {
int left = 0;
int right = s.length - 1;
while(left < right){
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}
考点:字符串
链接:https://leetcode.cn/problems/reverse-string-ii/
注意事项:字符串常量要先转为字符数组再进行后续操作
class Solution {
public void myReverse(char[] cArray, int index1, int index2){
int left = index1;
int right = index2;
while(left < right){
char temp = cArray[left];
cArray[left] = cArray[right];
cArray[right] = temp;
left++;
right--;
}
}
public String reverseStr(String s, int k) {
char[] cArray = s.toCharArray();
for(int i = 0; i < s.length();){
int left = s.length() - i;
if(left < k){
myReverse(cArray, i, s.length() - 1);
i += left;
}
else if(left >= k && left < 2 * k){
myReverse(cArray, i, i + k - 1);
i += left;
}
else if(left >= 2 * k){
myReverse(cArray, i, i + k - 1);
i += 2 * k;
}
}
return new String(cArray);
}
}
考点:字符串
链接:https://leetcode.cn/problems/ti-huan-kong-ge-lcof/
改进:
① 直接用 StringBuffer / StringBuilder 遍历
② 双指针,先用 StringBuffer / StringBuilder 遍历出空格数量;然后left 指向原字符串尾,right 指向填充空格后的字符串尾;将 s 转为字符数组,left 和 right 同时向左移动
class Solution {
public String replaceSpace(String s) {
// 会产生过多冗余空间
char[] cArray = new char[s.length() * 3];
int j = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) != ' '){
cArray[j++] = s.charAt(i);
}
else if(s.charAt(i) == ' '){
cArray[j++] = '%';
cArray[j++] = '2';
cArray[j++] = '0';
}
}
return new String(cArray, 0, j);
}
}
考点:字符串
链接:https://leetcode.cn/problems/reverse-words-in-a-string/
改进:先去除多余空格,反转整个字符串,反转每个单词
class Solution {
public String reverseWords(String s) {
StringBuffer sb = new StringBuffer();
int left, right;
for(int i = s.length() - 1; i >= 0;){
while(i >= 0 && s.charAt(i) == ' '){
i--;
}
right = i;
while(i >= 0 && s.charAt(i) != ' '){
i--;
}
//这里的if判断不可以写成 if(i >= 0)
//否则对于以字母非空格开头的字符串会忽略掉起始单词
//if(i >= 0){
if(right >= 0){
left = i + 1;
sb.append(s.substring(left, right + 1));
sb.append(' ');
right = i;
}
}
sb.delete(sb.length() - 1, sb.length());
return sb.toString();
}
}
考点:字符串
链接:https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
直接使用自带substring函数是没有意义的
class Solution {
public String reverseLeftWords(String s, int n) {
if(n >= s.length()){
return s;
}
StringBuffer sb = new StringBuffer();
sb.append(s.substring(n, s.length()));
sb.append(s.substring(0, n));
return sb.toString();
}
}
改进:将前n个字符转移到字符串尾部就相当于在原字符串中先反转前n个元素,再反转其余元素,最后将整个字符串反转
class Solution {
public void reverse(char[] cArray, int index1, int index2){
int left = index1;
int right = index2;
while(left < right){
char temp = cArray[left];
cArray[left] = cArray[right];
cArray[right] = temp;
left++;
right--;
}
}
public String reverseLeftWords(String s, int n) {
char[] cArray = s.toCharArray();
reverse(cArray, 0, n - 1);
reverse(cArray, n, s.length() - 1);
reverse(cArray, 0, s.length() - 1);
return new String(cArray);
}
}