344.reverse string 解答

Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.

解答

1
2
3
4
5
6
7
8
9
10
public class Solution {
public String reverseString(String s) {
StringBuilder sbs = new StringBuilder();
int l = s.length();
for (int i = 0; i < l; i++) {// 由此看出l=5,并且string字符串计数也是从0开始,但为什么replace计数反而不正常?解答:因为java中范围边界取值实际为下标左边的值
sbs.insert(0, s.charAt(i));
}
return sbs.toString();
}
}