-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathReverseString344.java
50 lines (43 loc) · 1.33 KB
/
ReverseString344.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
/**
* Write a function that takes a string as input and returns the string reversed.
*
* Example:
* Given s = "hello", return "olleh".
*/
public class ReverseString344 {
public String reverseString(String s) {
if (s == null || s.length() <= 1) return s;
char[] chars = s.toCharArray();
int i = 0;
int j = s.length()-1;
while (i < j) swap(chars, i++, j--);
return String.valueOf(chars);
}
private void swap(char[] chars, int i, int j) {
char t = chars[i];
chars[i] = chars[j];
chars[j] = t;
}
/**
* https://leetcode.com/problems/reverse-string/discuss/80937/JAVA-Simple-and-Clean-with-Explanations-6-Solutions
*/
public String reverseString2(String s) {
char[] word = s.toCharArray();
int i = 0;
int j = s.length() - 1;
while (i < j) {
word[i] = (char) (word[i] ^ word[j]);
word[j] = (char) (word[i] ^ word[j]);
word[i] = (char) (word[i] ^ word[j]);
i++;
j--;
}
return new String(word);
}
/**
* https://leetcode.com/problems/reverse-string/discuss/80937/JAVA-Simple-and-Clean-with-Explanations-6-Solutions
*/
public String reverseString3(String s) {
return new StringBuilder(s).reverse().toString();
}
}