首页 > 代码库 > leetcode344 Reverse String Java

leetcode344 Reverse String Java

自己写法,不过不被leetcode接受,提示Time Limit Exceeded~~~

public String reverseString(String s) {
        String res = "";
        Stack<Character> stack = new Stack<>();
        char[] ch = s.toCharArray();
        int count = 0;
        for(char c : ch) {
            count++;
            stack.push(c);
        }
        for(int i=0; i<count;i++) {
            res += stack.pop();
        }
        return res;
    }

 

leetcode344 Reverse String Java