Write a function that takes a string as input and returns the string reversed.
Example:
Example:
Given s = "hello", return "olleh".
Answer:
public class Solution {
public String reverseString(String s) {
StringBuilder temp = new StringBuilder();
for(int i=0;i<s.length();i++){
char c =s.charAt(i);
temp.insert(0,c);
}
return temp.toString();
}
}
思路:翻转字符串。通过StringBuilder的insert方法,可以将每个字符重新插入进新的字符串中,返回即可。