Why it is error?


  • 0
    T
    char[] c = s.toCharArray();
    	String str = "";
    	for(int i = c.length-1; i >= 0; i --){
    		str += c[i];
    	}
    	return str;

  • 0
    T

    I think it's right....Maybe I'm too weak. Waiting another answers.


  • 0
    C

    I got the same answer as yours. The error said that it exceeded the time limitation, which indicated that this methods was too slow.


  • 0
    T

    yes, it is too slow. thanks
    My solution is change String to StringBulider


  • 0
    T

    it is right, but too slow


  • 0
    S

    String is immutable. u already initialized str to "" empty string,
    you cant append it with c[i] as you intended to create a new string and re-assign it to str refernece

    to create a new string you could either use StringBuffer or StringBuilder to build it. or using new String(char[]) to build a new string

    Hope this help


Log in to reply
 

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