-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode344.java
More file actions
39 lines (35 loc) · 1.08 KB
/
LeetCode344.java
File metadata and controls
39 lines (35 loc) · 1.08 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
import java.util.Arrays;
import util.PrintUtil;
public class LeetCode344 {
public static void main(String[] args) {
char[] s;
// 输入:s = ["h","e","l","l","o"]
// 输出:["o","l","l","e","h"]
s = new char[] { 'h', 'e', 'l', 'l', 'o' };
System.out.println(Arrays.toString(s));
new Solution344().reverseString(s);
System.out.println(Arrays.toString(s));
PrintUtil.printDivider();
// 输入:s = ["H","a","n","n","a","h"]
// 输出:["h","a","n","n","a","H"]
s = new char[] { 'H', 'a', 'n', 'n', 'a', 'h' };
System.out.println(Arrays.toString(s));
new Solution344().reverseString(s);
System.out.println(Arrays.toString(s));
PrintUtil.printDivider();
}
}
class Solution344 {
public void reverseString(char[] s) {
int left = 0;
int right = s.length - 1;
char temp;
while (left < right) {
temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}