java 2ms solution


  • 0
    A
    public class Solution {
               public String reverseString(String s) {
                   char[] str = s.toCharArray();
                   int begin = 0;
                   int end = s.length() - 1;
                   while (begin < end) {
                       str[begin] = (char) (str[begin] ^ str[end]);
                       str[end] = (char) (str[begin] ^ str[end]);
                       str[begin] = (char) (str[end] ^ str[begin]);
                       begin++;
                       end--;
                   }
                   return new String(str);
               }
           }
       
    

  • 0
    H

    Is it necessary to use the bit manipulation operators?

    Does it provide better optimization than directly swapping the characters

    char c = cs[begin];
    cs[begin] = cs[end];
    cs[end] = c;
    
    

Log in to reply
 

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