Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
题意:将一个String字符串逆转。
思路:直接遍历逆转输出。嗯,恭喜,
超时!!!
懵逼,看大神,使用内置函数
StringBuilder。
在此之前,我以为内置函数的优化。。。网上有博主说内置函数有很多优化很好
代码:
class Solution {
public String reverseString(String s) {
if(s.length()<2)
return s;
return new StringBuilder(s).reverse().toString();
}
}