代码随想录算法训练营第七天-344反转字符串、541反转字符串II、卡码网54替换数字、151翻转字符串里的单词、卡码网55右旋转字符串
Kiml Lv5
  • 前言
    状态:都是可以写出来的。但是有些与参考种给出的解析思路不同。

  • 更新

1
24.05.27 初始记录

初步题解

344 反转字符串

题目链接:(https://leetcode.cn/problems/reverse-string/description/)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class LE344 {  
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
reverseString(scanner.nextLine().toCharArray());
}

public static void reverseString(char[] s) {
for (int i = 0, j = s.length - 1; i <= j; i++, j--) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
System.out.println(Arrays.toString(s));
}
}

541 反转字符串 II

题目链接:(https://leetcode.cn/problems/reverse-string-ii/description/)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class LE541 {  
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(reverseStr(scanner.nextLine(), Integer.parseInt(scanner.nextLine())));
}

/**
* 画图翻译了一下
* 大概意思就是奇数段反转
*/
public static String reverseStr(String s, int k) {
// 判断能拆分成几段
int size = s.length() / k;
if (s.length() % k != 0) {
size = size + 1;
}

StringBuilder reverseStr = new StringBuilder();
for (int i = 1; i <= size; i++) {
String substring = s.substring((i - 1) * k, Math.min(i * k, s.length()));
if (i % 2 != 0) {
char[] chars = substring.toCharArray();
for (int n = 0, m = chars.length - 1; n <= m; n++, m--) {
char temp = chars[n];
chars[n] = chars[m];
chars[m] = temp;
}
reverseStr.append(chars);
} else {
reverseStr.append(substring);
}
}

return reverseStr.toString();
}
}

卡码网 54 替换数字

题目链接:(https://kamacoder.com/problempage.php?pid=1064)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class CM54 {  
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(replaceNumber(scanner.nextLine()));
}

public static String replaceNumber(String s) {
StringBuilder stringBuilder = new StringBuilder();
char[] chars = s.toCharArray();
for (char c : chars) {
if ('0' <= c && '9' >= c) {
stringBuilder.append("number");
} else {
stringBuilder.append(c);
}
}
return stringBuilder.toString();
}
}

151 翻转字符串里的单词

题目链接:(https://leetcode.cn/problems/reverse-words-in-a-string/description/)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {  
Scanner scanner = new Scanner(System.in);
System.out.println(reverseWords(scanner.nextLine()));
}

private static String reverseWords(String s) {
String[] split = s.trim().split(" ");
StringBuilder stringBuilder = new StringBuilder();
for (int i = split.length - 1; i >= 0; i--) {
String trim = split[i].trim();
if ("".equals(trim)) {
continue;
}
stringBuilder.append(trim).append(" ");
}

return stringBuilder.toString().trim();
}

卡码网 55 右旋转字符串

题目链接:(https://kamacoder.com/problempage.php?pid=1065)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class CM55 {  
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = Integer.parseInt(scanner.nextLine());
String s = scanner.nextLine();
System.out.println(rightString(k, s));
}

private static String rightString(int k, String s) {
// 过界判断
if (k >= s.length() || k <= 0) {
return s;
}

return s.substring(s.length() - k) + s.substring(0, s.length() - k);
}
}

看讲解

344 反转字符串

题目链接/文章讲解/视频讲解:(https://programmercarl.com/0344.反转字符串.html)

541 反转字符串 II

题目链接/文章讲解/视频讲解:(https://programmercarl.com/0541.反转字符串II.html)

卡码网 54 替换数字

题目链接/文章讲解/视频讲解:(https://programmercarl.com/kamacoder/0054.替换数字.html)

根据讲解,这题本意是先扩充再分配。要用到双指针的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Main {
public static String replaceNumber(String s) {
int count = 0; // 统计数字的个数
int sOldSize = s.length();
for (int i = 0; i < s.length(); i++) {
if(Character.isDigit(s.charAt(i))){
count++;
}
}
// 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小
char[] newS = new char[s.length() + count * 5];
int sNewSize = newS.length;
// 将旧字符串的内容填入新数组
System.arraycopy(s.toCharArray(), 0, newS, 0, sOldSize);
// 从后先前将空格替换为"number"
for (int i = sNewSize - 1, j = sOldSize - 1; j < i; j--, i--) {
if (!Character.isDigit(newS[j])) {
newS[i] = newS[j];
} else {
newS[i] = 'r';
newS[i - 1] = 'e';
newS[i - 2] = 'b';
newS[i - 3] = 'm';
newS[i - 4] = 'u';
newS[i - 5] = 'n';
i -= 5;
}
}
return new String(newS);
};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
System.out.println(replaceNumber(s));
scanner.close();
}
}

151 翻转字符串里的单词

题目链接/文章讲解/视频讲解:(https://programmercarl.com/0151.翻转字符串里的单词.html)

这题。。。Java 不好像没有办法空间复杂度为 O(n)。(面试的话,不太清楚会不会这么细,平时可能就直接 API 搞定了。。。一般机考也不会考这个吧😶‍🌫️)

只能说思路很重要:

  • 移除多余空格()

  • 将整个字符串反转

  • 将每个单词反转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class LE151 {  
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(reverseWords(scanner.nextLine()));
}

/**
* 本题考察点应该和上一题一样
* 步骤如下:
* 1.移除字符串中的空格
* 2.倒序
* 3.每个单词再反转
* 要求:不使用任何内置API
*/ public static String reverseWords(String s) {
String removeSpace = removeSpace(s);
System.out.println(removeSpace);
return reverseWord(removeSpace);
}

/**
* 单个单词倒置
*/
private static String reverseWord(String s) {
String[] list = s.split(" ");
if (list.length <= 0) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
for (String s1 : list) {
char[] chars = s1.toCharArray();
for (int i = 0, j = chars.length - 1; i <= j; i++, j--) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
stringBuilder.append(chars).append(" ");
}
return stringBuilder.substring(0, stringBuilder.length() - 1);
}

/**
* 去除字符串中的多余空格
* 并进行倒序
*/
private static String removeSpace(String s) {
char[] chars = s.toCharArray();
int start = 0;
int end = s.length() - 1;
while (s.charAt(start) == ' ') {
start++;
}
while (s.charAt(end) == ' ') {
end--;
}
if (start > end) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = end; i >= start; i--) {
if (chars[i] == ' ' && chars[i - 1] == ' ') {
continue;
}
stringBuilder.append(chars[i]);
}
return stringBuilder.toString();
}

}

卡码网 55 右旋转字符串

题目链接/文章讲解/视频讲解:(https://programmercarl.com/kama55.右旋字符串.html)

这题也是。。。反正直接用 API 了,看了说《剑指 Offer》有差不多的题目。

没有再写一遍,直接抄了解析中的写法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String s = in.nextLine();

int len = s.length(); //获取字符串长度
char[] chars = s.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);
}

public static void reverseString(char[] ch, int start, int end) {
//异或法反转字符串,参照题目 344.反转字符串的解释
while (start < end) {
ch[start] ^= ch[end];
ch[end] ^= ch[start];
ch[start] ^= ch[end];
start++;
end--;
}
}
}
 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
访客数 访问量