Java with 4/3 ms solution


  • 0
    Z

    Java using split way to do it in time range 4/3 ms

    public String reverseString(String s) {
        int l = 0;
        int r = s.length()-1;
        char[] charArr = new char[s.length()];
        
        if(s.length() == 0) return s;
        if(s.length() == 1) return s;
        if((s.length() % 2) != 0) 
            charArr[(s.length()/2)]= s.charAt(s.length()/2);
    
        while(l < r){
            charArr[r] = s.charAt(l);
            charArr[l] = s.charAt(r);
            l++;
            r--;
        }
        
        s = String.valueOf(charArr);
        
        return s;
    }

Log in to reply
 

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