字符串的处理
——代码训练营day08
目录
- 344.反转字符串
- 541 反转字符串2
- 卡码网:54.替换数字
- 151.翻转字符串里的单词
- 卡码网:55.右旋转字符串
1. 反转字符串
两种方式,重点掌握第二种方式
public class ReverseString {
public void reverseString(char[] s) {
int length = s.length;
int left = 0;
int right = length - 1;
while (left < right) {
char str = s[left];
s[left] = s[right];
s[right] = str;
left++;
right--;
}
}
public void reverseString(char[] chars, int left, int right) {
if (right >= chars.length) {
System.out.println("set a wrong right");
return;
}
/**
* 异或运算,可以解决这个问题
*/
while (left < right) {
chars[left] ^= chars[right];
chars[right] ^= chars[left];
chars[left] ^= chars[right];
left++;
right--;
}
}
2.反转字符串2
public class ReverseString2 {
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;
}
}
}
3.替换数字
public class ReplaceNumber {
public static String replaceDigitsWithNumber(String s) {
if (s == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
sb.append("number");
} else {
sb.append(c);
}
}
return sb.toString();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String output = replaceDigitsWithNumber(s);
System.out.println(output); // 输出:anumberbnumbercnumber
}
}
4.反转字符串中的单词
- 去除空格的算法和删除数组元素的有异曲同工之妙
- 也是先整体后局部
public class ReverseWords {
//用 char[] 来实现 String 的 removeExtraSpaces,reverse 操作
public static String reverseWords(String s) {
char[] chars = s.toCharArray();
//1.去除首尾以及中间多余空格
chars = removeExtraSpaces(chars);
//2.整个字符串反转
reverse(chars, 0, chars.length - 1);
//3.单词反转
reverseEachWord(chars);
return new String(chars);
}
//1.用 快慢指针 去除首尾以及中间多余空格,可参考数组元素移除的题解
public static char[] removeExtraSpaces(char[] chars) {
int slow = 0;
for (int fast = 0; fast < chars.length; fast++) {
//先用 fast 移除所有空格
if (chars[fast] != ' ') {
//在用 slow 加空格。 除第一个单词外,单词末尾要加空格
if (slow != 0)
chars[slow++] = ' ';
//fast 遇到空格或遍历到字符串末尾,就证明遍历完一个单词了
while (fast < chars.length && chars[fast] != ' ')
chars[slow++] = chars[fast++];
}
}
//相当于 c++ 里的 resize()
char[] newChars = new char[slow];
System.arraycopy(chars, 0, newChars, 0, slow);
return newChars;
}
//双指针实现指定范围内字符串反转,可参考字符串反转题解
public static void reverse(char[] chars, int left, int right) {
if (right >= chars.length) {
System.out.println("set a wrong right");
return;
}
/**
* 异或运算,可以解决这个问题
*/
while (left < right) {
chars[left] ^= chars[right];
chars[right] ^= chars[left];
chars[left] ^= chars[right];
left++;
right--;
}
}
//3.单词反转
public static void reverseEachWord(char[] chars) {
int start = 0;
//end <= s.length() 这里的 = ,是为了让 end 永远指向单词末尾后一个位置,这样 reverse 的实参更好设置
for (int end = 0; end <= chars.length; end++) {
// end 每次到单词末尾后的空格或串尾,开始反转单词
if (end == chars.length || chars[end] == ' ') {
reverse(chars, start, end - 1);
start = end + 1;
}
}
}
public static void main(String[] args) {
String str = "ni hao ya ni hao ya ";
String s = reverseWords(str);
System.out.printf("反转后的字符串:%s",s);
}
}
5. 右旋转字符串
- 先把整体反转
- 再进行局部反转
public class ReverseRightWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // 只需要创建一个即可
int n = Integer.parseInt(sc.nextLine());
String str = sc.nextLine();
int len = str.length(); //获取字符串长度
char[] chars = str.toCharArray();
reverseString(chars, 0, len - 1); //反转整个字符串
reverseString(chars, 0, n - 1); //反转前一段字符串,此时的字符串首尾尾是0,n - 1
reverseString(chars, n, len - 1); //反转后一段字符串,此时的字符串首尾尾是n,len - 1
System.out.println(chars);
}
/**
* 反转的算法
* @param chr
* @param left
* @param right
*/
public static void reverseString(char[] chr, int left, int right) {
if (chr.length <= right && left < 0) {
return;
}
while (left < right) {
chr[left] ^= chr[right];
chr[right] ^= chr[left];
chr[left] ^= chr[right];
left++;
right--;
}
}