• LeetCode


    题目

    Write a function that reverses a string. The input string is given as an array of characters char[].

    Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

    You may assume all the characters consist of printable ascii characters.

    Example 1:

    Input: ["h","e","l","l","o"]
    Output: ["o","l","l","e","h"]
    

    Example 2:

    Input: ["H","a","n","n","a","h"]
    Output: ["h","a","n","n","a","H"]
    

    解题思路

    这是一道经典的反转字符串题目,难点在于要求不能申请额外的内存空间,并且要求时间复杂度为O(1)。第一次接触过这种类型的同学大概会懵逼,这道题其实可以转变成另一道题:

    如何在不占用任何额外空间的情况下交换x、y两个数的值?

    这里可以利用到异或运算来解决,异或有以下性质:

    1. 相同的两个数异或结果为0
    2. 任何数与0异或结果还是其自身
    3. 异或运算满足交换律和结合律

    于是就有了这样一种思路:将字符串的首尾字符通过异或运算来相互调换位置,比如第一个和最后一个字符互换位置,第二个和最后第二个互换位置,一共循环字符串长度/2的次数,这样就可以实现题目想要的效果。代码如下:

    class Solution {
        public void reverseString(char[] s) {
            int size = s.length;
            int a = size / 2;
            int last;
            for (int i = 0; i < a; i++) {
                last = size - 1 - i;
                s[i] ^= s[last];
                s[last] ^= s[i];
                s[i] ^= s[last];
            }
        }
    }
    

    下面是时间与内存的消耗:

    Runtime: 1 ms
    Memory Usage: 51.6 MB

    相关链接

  • 相关阅读:
    不使用C++ 11的整数转字符串
    1090 危险品装箱(25 分)
    C++中vector,set,map自定义排序
    D
    7-2 幼儿园数学题(29 分)
    李白打酒
    C++ string和int相互转换
    1049 数列的片段和(20)(20 分)
    11. 盛最多水的容器
    7. 整数反转
  • 原文地址:https://www.cnblogs.com/yulinlewis/p/10990738.html
Copyright © 2020-2023  润新知