zoukankan      html  css  js  c++  java
  • 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

    相关链接

  • 相关阅读:
    js弹出框、对话框、提示框、弹窗总结
    GIT本地免密配置
    SpringMVC 框架的搭建及基本功能的实现
    win10调用局域网内xp系统上的打印机
    JSP页面之间互相传值
    Spring MVC 框架的架包分析,功能作用,优点
    hibernate_Restrictions用法
    服务器中配置多个Tomcat及内存溢出配置
    破解navicat
    如何重装mysql8及初次修改密码
  • 原文地址:https://www.cnblogs.com/yulinlewis/p/10990738.html
Copyright © 2011-2022 走看看