-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q1_字符串反转.java
65 lines (62 loc) · 1.67 KB
/
Q1_字符串反转.java
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
package com.algorithm.demo.string;
/**
* 1283. 翻转字符串
* 写一个方法,接受给定字符串作为输入,返回将这个字符串逐个字符翻转后的新字符串。
* <p>
* 样例
* 样例 1:
* <p>
* 输入:"hello"
* 输出:"olleh"
* 样例 2:
* <p>
* 输入:"hello world"
* 输出:"dlrow olleh"
* <p>
* string: Hello
* length: 5
* <p>
* 0 1 2 3 4
* before: H e l l o
* after: o l l e H
* <p>
* index sum
* 0: H--->o 0-->4 4
* 1: e--->l 1-->3 4
* 2: l--->l 2-->2 4
*/
public class Q1_字符串反转 {
public static void main(String[] args) {
String before = "hello world";
String after = reverseString2(before);
System.out.println(" " + after);
}
/**
* @param string: a string
* @return: return a string
*/
public static String reverseString(String string) {
if (string == null || string.length() == 0) return string;
int length = string.length();
char[] temp = string.toCharArray();
for (int i = 0; i < length; i++) {
temp[i] = string.charAt(length - i - 1);
}
return new String(temp);
}
/**
* @param string: a string
* @return: return a string
*/
public static String reverseString2(String string) {
// write your code here
if (string == null || string.length() == 0) return string;
int length = string.length();
char[] temp = string.toCharArray();
for (int i = 0; i < length / 2; i++) {
temp[i] = string.charAt(length - i - 1);
temp[length - i - 1] = string.charAt(i);
}
return new String(temp);
}
}