## 字符串

常见解决方法

- **逆转**
- **双指针**
  - 首尾双指针:删除空格,要的才给StringBuilder
  - 双头双指针:移动元素
-

| 类型                 | 题目                                                         | 解决                           | 做题时间       |
| -------------------- | ------------------------------------------------------------ | ------------------------------ | -------------- |
| 多次逆转             | [右旋字符串](https://www.programmercarl.com/kama55.右旋字符串.html#思路) | 三次逆转                       | 2024-1-5 x     |
| 删除空格、逆转字符串 | [151. 反转字符串中的单词 - 力扣(LeetCode)](https://leetcode.cn/problems/reverse-words-in-a-string/) | 双指针删除空格,<br />字符逆战 | **2024-1-5 x** |
|                      |                                                              |                                |                |
This commit is contained in:
whai 2024-01-05 14:26:02 +08:00
parent 92d8e06065
commit 418bbabea8
3 changed files with 117 additions and 24 deletions

View File

@ -0,0 +1,57 @@
package cn.whaifree.leetCode.String;
import org.junit.Test;
/**
* 字符串的右旋转操作是把字符串尾部的若干个字符转移到字符串的前面给定一个字符串 s 和一个正整数 k请编写一个函数将字符串中的后面 k 个字符移到字符串的前面实现字符串的右旋转操作
*
* 例如对于输入字符串 "abcdefg" 和整数 2函数应该将其转换为 "fgabcde"
*
* 输入输入共包含两行第一行为一个正整数 k代表右旋转的位数第二行为字符串 s代表需要旋转的字符串
*
* 输出输出共一行为进行了右旋转操作后的字符串
*/
public class Kama55 {
@Test
public void test() {
String s = "abcdefg";
int k = 2;
String result = new Solution().reverse(s, k);
System.out.println(result);
}
public static void main(String[] args) {
}
class Solution{
/**
* 逆转
* abcde 2
* cbaed 两个区间分别逆转
* deabc 整体逆转
* @param s
* @param target
* @return
*/
public String reverse(String s, int target) {
StringBuilder stringBuilder = new StringBuilder(s);
reverseString(stringBuilder, 0, target - 1);
reverseString(stringBuilder, target, stringBuilder.length() - 1);
reverseString(stringBuilder, 0, stringBuilder.length() - 1);
return stringBuilder.toString();
}
public void reverseString(StringBuilder stringBuilder,int start, int end) {
while (start < end) {
char tmp = stringBuilder.charAt(start);
stringBuilder.setCharAt(start, stringBuilder.charAt(end));
stringBuilder.setCharAt(end, tmp);
start++;
end--;
}
}
}
}

View File

@ -10,15 +10,14 @@ import java.util.List;
public class LeetCode151 { public class LeetCode151 {
// todo
@Test @Test
public void test() { public void test() {
String s = " the sky is blue"; String s = " a b cd ef g ";
System.out.println(new Solution1().reverseWords(s));
char[] chars = new Solution1().deleteSpace(s.toCharArray());
for (char aChar : chars) {
System.out.println("=" + aChar + "=");
}
} }
class Solution { class Solution {
@ -44,36 +43,68 @@ public class LeetCode151 {
// 不要使用辅助空间空间复杂度要求为O(1) // 不要使用辅助空间空间复杂度要求为O(1)
class Solution1 { class Solution1 {
public String reverseWords(String s) { public String reverseWords(String s) {
char[] chars = s.toCharArray(); StringBuilder stringBuilder = deleteSpace(s, ' ');
reverse(stringBuilder, 0, stringBuilder.length() - 1);
reverseWord(stringBuilder);
// 移除多余空格 // 移除多余空格
// 反转char // 反转char
// 反转每个字符 // 反转每个字符
return null; return stringBuilder.toString();
} }
public char[] deleteSpace(char[] chars) { public void reverseWord(StringBuilder stringBuilder) {
int start = 0;
int superFlowSpace = 0;
int index = 0; int index = 0;
System.out.println(chars[index] == ' '); while (index < stringBuilder.length() + 1) {
while (chars[index] == ' ') { if (index == stringBuilder.length() || stringBuilder.charAt(index) == ' ') {
superFlowSpace++; reverse(stringBuilder, start, index - 1);
start = index + 1;
}
index++; index++;
} }
index = 0;
while (index < chars.length - 1 - superFlowSpace) {
chars[index] = chars[index + superFlowSpace];
if (' ' == chars[index + superFlowSpace] && ' ' == chars[index + superFlowSpace + 1]) {
superFlowSpace++;
} else {
index++;
} }
public void reverse(StringBuilder str, int start, int end) {
while (start < end) {
char tmp = str.charAt(start);
str.setCharAt(start, str.charAt(end));
str.setCharAt(end, tmp);
end--;
start++;
}
} }
return Arrays.copyOfRange(chars, 0, chars.length - superFlowSpace); /**
* 双指针移动元素移除target
* - 参考LeetCode 27
* @param target
* @return
*/
public StringBuilder deleteSpace(String s,char target) {
// 前后指针
int start = 0;
int end = s.length() - 1;
while (s.charAt(start) == target) start++;
while (s.charAt(end) == target) end--;
// 1. 删除首尾空格
StringBuilder stringBuilder = new StringBuilder();
while (start <= end) {
if (s.charAt(start) !=target){
stringBuilder.append(s.charAt(start) );
}else if (s.charAt(start) == target && stringBuilder.charAt(stringBuilder.length() - 1) != target) {
stringBuilder.append(s.charAt(start) );
}
start++;
}
// 2. 如果遇到空格判断上一个是不是空格
// - 不是入String
// - 跳过
return stringBuilder;
} }
} }

View File

@ -2,6 +2,11 @@ package cn.whaifree.leetCode.String;
import org.junit.Test; import org.junit.Test;
/**
* 反转字符串 II
* 给定一个字符串 s 和一个整数 k从字符串开头算起每计数至 2k 个字符就反转这 2k 字符中的前 k 个字符
* 如果剩余字符少于 k 则将剩余字符全部反转
*/
public class LeetCode541 { public class LeetCode541 {
@Test @Test