Works in eclipse, but can't submit


  • 0
    S
    public class Solution {
        public void reverseWords(char[] s) {
            String str = new String(s);
            String[] strArr = str.split(" ");
            int i=0;
            int j=strArr.length-1;
            while(i<j){
                String temp = strArr[i];
                strArr[i]=strArr[j];
                strArr[j]=temp;
                i++;
                j--;
            }
            str = String.join(" ", strArr);
            s= str.toCharArray();
        }
    }
    

    OJ says code result is "a b" for "a b". When I give {'a', ' ', 'b'}, I get {'b', ' ', 'a'}


  • 1

    Obviously you can submit, otherwise the judge wouldn't tell you that result. And it doesn't work in eclipse. You're not changing the given array at all. That s= ... doesn't change it, it only makes your local variable point to a different array.


  • 0
    W

    Is there a way to make the local variable point the original char[] s?


  • 0
    W

    Can anyone modify the submitted code a little bit to make it work?


  • 0

    @wangbd
    Uh... yeah... by not making it point somewhere else.


  • 0
    S

    This worked for me

    public class Solution {
            int begin;
        public void reverseWords(char[] s) {
            reverse(s, 0, s.length-1);
            begin=0;
            for(int i=0;i<s.length;i++){
                if(s[i]==' '){
                    reverse(s, begin, i-1);
                }
                else if(i==s.length-1){
                    reverse(s, begin, i);
                }
            }
        }
        public void reverse(char[] s, int start, int end){
            begin=end+2;
            while(start<end){
                char temp=s[start];
                s[start]=s[end];
                s[end]=temp;
                start++;end--;
            }
            
        }
    }

  • 0
    W

    @StefanPochmann do you mean to just comment out of this line s= str.toCharArray();? But if you were to comment out this line, the original char array would not have been reversed. I'd really appreciate it if you could explain more. Thank you very much in advance.


  • 0

    Yes, that's what I mean, as s already points to the original array in the beginning.

    And yes, of course the reversed content still has to be written into it.


  • 0
    W

    @StefanPochmann I have the code below which works in eclipse with input "a b", output "b a", but when I post it in OJ I got the following error(My code isn't formatted correctly in this comment, the headers are over the grayed out code area)

    Input:
    "a b"
    Output:
    "b\x00a"
    Expected:
    "b a"

    public class Solution {

    public static void reverseWords(char[] s) {

            char[] result = new char[s.length];
            int walker = s.length ;
            int wordLength = 0;
            
            for(int i = 0; i < s.length; i++){
            	if(s[i] == ' ' ){
            		for(int a = wordLength; a > 0  ; a--){
            			result[walker-a] = s[i-a];
            		}
            		walker -= wordLength; 
            		//escap spaces 
            		walker--;
            		wordLength = 0;
            		 
            	}
            	else if(i == s.length -1){
            		wordLength++;
            		 
            		for(int a = wordLength; a > 0  ; a--){
            			result[walker-a] = s[i-a+1];
            		}
            	     
        		}
            	else{
            		wordLength++;
            	}
            }
               for(int i = 0; i < s.length; i++){
                         s[i] = result[i];
                 }
    
     }
    

    }


Log in to reply
 

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