Java Solutions (using StringBuilder and Char array)


  • 0
    Z

    '''
    package Q344.Reverse_String;

    public class ReverseStr {

    public static void main(String [] str) {

    ReverseStr r = new ReverseStr();
    System.out.println(r.reverseString("hello"));
    System.out.println(r.reverseString1("hello"));

    }

    public String reverseString(String s) {

    StringBuilder sb = new StringBuilder(s).reverse();
     
    return sb.toString();
    	
    
    }	
    

    public String reverseString1(String s) {

    char [] charStr = s.toCharArray();
    int beginIndex = 0;
    int endIndex = charStr.length-1;
    char temp;
    while (beginIndex<endIndex) {
    	temp = charStr[beginIndex];
    	charStr[beginIndex]=charStr[endIndex];
    	charStr[endIndex] = temp;
    	endIndex--;
    	beginIndex++;
    		
    }
    return new String(charStr);
    	
    	
    }
    

    }

    '''


Log in to reply
 

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