leetcode [#344] | GCidea's blog
目录

题目
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
解决方案
1 | public class Solution { |
注意事项
以下这种写法可以得到正确答案,但是会超时,无法通过。原因是String一旦创建就不可修改,因此遍历中每次为result衔接新增的部分都会创建新对象并将引用指向result,增大了开销。
1 | public class Solution { |