Saturday, October 21, 2017

344. Reverse String

Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".

Solution:
There are so many solutions:
Stringbuilder, charArray ....
 public String reverseString(String s) {  
     return new StringBuilder(s).reverse().toString();  
   }  
  public String reverseString(String s) {  
     StringBuilder sb=new StringBuilder();  
     for(int i=s.length()-1;i>=0;i--) sb.append(s.charAt(i));  
     return sb.toString();  
   }  

No comments:

Post a Comment