Can execute in PC but RunTime Error in here!


  • 0
    S

    origin code

    public class Solution {
       public String reverseWords(String s) {
       String[] str = s.split(" ");
       int len = str.length;
       StringBuffer strBuffer = new StringBuffer(len);
       for (int i = len-1; i > 0; i--) {
           strBuffer.append(str[i]);
           strBuffer.append(" ");
       }
       strBuffer.append(str[0]);
       return strBuffer.toString();
    }
    

    Error type:

    Runtime Error Message: Line 11: java.lang.ArrayIndexOutOfBoundsException: 0
    Last executed input: " "


  • 0
    S

    what if s is an empty string? Will the length of str be 0 or 1?

    And also check the Questions in FAQ , it could help.


Log in to reply
 

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