Reverse String


  • 0
    J
        public String reverseString(String s) {
            char[] arr = s.toCharArray();
            char[] arr2 = new char[arr.length];
            for(int i =arr.length-1,j=0;i>=0;i--,j++){
                arr2[j]=arr[i];              
            }
            //System.out.println(arr2);
            
            return  arr2.toString();
        }
    }
    
    What is wrong with the code? It prints the address and not the value.

  • 0
    M

    your return statement should be

    Arrays.toString(arr2);


  • 0
    J

    @mattraporte I did a "new String(arr2);" and that did work. Would you know the reason it did not work in the first place?


Log in to reply
 

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