My 2ms java solution


  • 0
    public String reverseString(String s) {
            if (s == null) {
                return null;
            }
            char[] arr = s.toCharArray();
            int left=0, right=arr.length-1;
            while(left<right){
                char tmp=arr[left];
                arr[left] = arr[right];
                arr[right]=tmp;
                left++;
                right--;
            }
            
            return new String(arr);
        }

  • 0
    J

    //4ms
    public class Solution{

    static String reverseString(String str)
    {
    	int length = str.length();
    	char[] ch = new char[length];
    	for(int i = 0;i < length; i++)
    		ch[i] = str.charAt(length-i-1);	
    	return new String(ch);
        }
    

    }


Log in to reply
 

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