Problem:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
public class Solution {
public String reverseString(String s) {
char[] cha = s.toCharArray();
int i = 0;
int j = cha.length-1;
for(; i < j; i++, j--){
char tmp;
tmp = cha[i];
cha[i] = cha[j];
cha[j] = tmp;
}
return new String(cha);
}
}
Code2:
public class Solution {
public String reverseString(String s) {
StringBuilder s5 = new StringBuilder(s);
return s5.reverse().toString();
}
}