Java using Xor


  • 3
    V
    public class Solution {
        public String reverseString(String s) {
            if(s == null) return "";
            
            char[] rev = s.toCharArray();
            int i = 0, j = s.length() - 1;
            while(i < j) {
                rev[i] ^= rev[j];
                rev[j] ^= rev[i];
                rev[i++] ^= rev[j--];
            }
            
            return String.valueOf(rev);
        }
    }
    

  • 0
    H

    Up for using XOR, I really didn't think about this.


Log in to reply
 

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