Java two solutions. 2ms & 2 lines


  • 1
    W

    two simple solutions.

    public class Solution {
        // just 2 lines
        public static String reverseString(String s) {
            return new StringBuilder(s).reverse().toString();
        }
    
        // just take 2ms
        public String reverseString(String s) {
            char[] chars = s.toCharArray();
            int length = chars.length;
            int n = length - 1;
            for (int i = n >> 1; i >= 0; i--) {
                int k = n - i;
                char ci = chars[i];
                char ck = chars[k];
                chars[i] = ck;
                chars[k] = ci;
            }
            return new String(chars);
        }
    }
    

  • 0
    L
    This post is deleted!

Log in to reply
 

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