mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -143,6 +143,23 @@ for (int i = 0; i < a.size(); i++) {
|
|||||||
|
|
||||||
### Java:
|
### Java:
|
||||||
|
|
||||||
|
```java
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
String s = in.nextLine();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
if (Character.isDigit(s.charAt(i))) {
|
||||||
|
sb.append("number");
|
||||||
|
}else sb.append(s.charAt(i));
|
||||||
|
}
|
||||||
|
System.out.println(sb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
|
||||||
|
@ -145,8 +145,70 @@ int main() {
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
### Java:
|
### Java:
|
||||||
|
```Java
|
||||||
|
// 版本一
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
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--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 版本二
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
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 - n - 1); //反转前一段字符串,此时的字符串首尾是0,len - n - 1
|
||||||
|
reverseString(chars, len - n, len - 1); //反转后一段字符串,此时的字符串首尾是len - n,len - 1
|
||||||
|
reverseString(chars, 0, 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--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Python:
|
### Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user