simple java AC solution with O(n)


  • 0
    G
    class Solution {
        public String reverseString(String s) {
            if(s.length() == 0 || s == null)
                return "";
            
            int i = 0, j = s.length() - 1;
            char[] sArr = s.toCharArray();
            
            while(i < j)
            {
                swap(sArr, i, j);
                i++;
                j--;
            }
            
            return new String(sArr);
        }
        
        private void swap(char[] arr, int i, int j)
        {
            char temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    

Log in to reply
 

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