-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseString.java
More file actions
53 lines (48 loc) · 1.43 KB
/
ReverseString.java
File metadata and controls
53 lines (48 loc) · 1.43 KB
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
package leetcode.array;
/**
* @ClassName ReverseString
* @Description 反转数组 https://leetcode-cn.com/problems/reverse-string/
* @Author changxuan
* @Date 2020/8/6 下午9:09
**/
public class ReverseString {
public static void main(String[] args) {
char[] s = {'h','e','l','l','o'};
//reverseString(s);
test(s);
}
public static void reverseString(char[] s) {
char temp;
int i = 0, j = s.length == 0 ? 0 : s.length -1;
while (i < j){
temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
// 不用输出
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (int l = 0; l < s.length; l++){
if (l == s.length-1)
stringBuilder.append("\""+s[l]+"\"]");
else
stringBuilder.append("\""+s[l]+"\",");
}
System.out.println(stringBuilder.toString());
}
public static void test(char[] s){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (int i = s.length-1; i >=0; i--){
if (i == 0)
stringBuilder.append("\""+s[i]+"\"]");
else
stringBuilder.append("\""+s[i]+"\",");
}
System.out.println(stringBuilder.toString());
}
public static void test1(char[] s){
}
}