My simple Java solutions


  • 0
    N
    public class Solution {
        public String reverseString(String s) {
            char[] c = s.toCharArray();
            int i = 0, j = c.length - 1;
            while(i < j){
                char ch = c[i];
                c[i++] = c[j];
                c[j--] = ch;
            }
            return String.valueOf(c);
        }
    }
    
    
    public class Solution {
        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();
        }
    }
    

Log in to reply
 

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