Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathReverse String.java
More file actions
Latest commit
executable file
·35 lines (29 loc) · 851 Bytes
/
Copy pathReverse String.java
File metadata and controls
executable file
·35 lines (29 loc) · 851 Bytes
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
E
tags: TwoPointers, String
SimilartoReverseInteger.
可以用StringBuffer, 也可以twopointerreversehead/tail
```
/*
Write a function that reverses a string. The input string is given as an array of characters char[].
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
*/
/*
Thoughts:
Obvious: new StringBuilder().reverse().
Or, turn into charArray and reverse
*/
classSolution {
publicvoidreverseString(char[] s) {
if (s == null || s.length <= 1) {
return;
}
intn = s.length;
for (inti = 0; i < n / 2; i++) {
chartemp = s[i];
s[i] = s[n - i - 1];
s[n - i - 1] = temp;
}
}
}
```