是个人都会做。
我能想到三种方法:
// 1. two pointers, i < j
// 2. j >= 0 ; j --
// 3. Stack
Here I implement the 3rd one:
public String reverse(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
stack.push(s.charAt(i));
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
有人实现了6种:
https://discuss.leetcode.com/topic/43296/java-simple-and-clean-with-explanations-6-solutions