forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_344.java
31 lines (28 loc) · 773 Bytes
/
_344.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
package com.fishercoder.solutions;
/**
* 344. Reverse String
*
* Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".*/
public class _344 {
public String reverseString_cheating(String s) {
return new StringBuilder(s).reverse().toString();
}
public String reverseString(String s) {
int i = 0, j = s.length()-1;
char[] chars = s.toCharArray();
while(i < j){
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
}
StringBuilder sb = new StringBuilder();
for(char c : chars){
sb.append(c);
}
return sb.toString();
}
}