Output Limit Exceeded - how to improve code


  • 0
    L
    public class Solution {
        public String reverseString(String s) {
            String done="";
            int len = s.length();
            for (int i = 1; i <= len; i++){
                done += s.charAt(len-i);
            }
            return done;
        }
    }

  • 1
    E
    public String reverseString(String s) {
        StringBuilder str = new StringBuilder();
        for (int i=s.length()-1; i >= 0; i--) {
          str.append(s.charAt(i));
        }
        return str.toString();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.