是个人都会做。 我能想到三种方法: // 1. two pointers, i < j // 2. j >= 0 ; j -- // 3. Stack
Here I implement the 3rd one:
public String reverse(String s) { Stackstack = 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(); }复制代码