美文网首页
344. Reverse String

344. Reverse String

作者: jluemmmm | 来源:发表于2021-10-02 12:57 被阅读0次

编写一个函数,将输入的字符串反转过来。输入字符串以字符串数组 s 的形式给出。

不要给另外的数组分配额外的空间,必须原地修改输入数组,使用O(1)的额外空间解决这一问题。

  • 时间复杂度O(n),空间复杂度O(1)
  • Runtime: 166 ms, faster than 17.30%
  • Memory Usage: 46.2 MB, less than 48.03%
/**
 * @param {character[]} s
 * @return {void} Do not return anything, modify s in-place instead.
 */
var reverseString = function(s) {
  let left = 0;
  let right = s.length - 1;
  while (left < right) {
    let tmp = s[left];
    s[left++] = s[right];
    s[right--] = tmp;
  }
};

相关文章

  • 2019-04-08

    LeetCode 344. Reverse String Description Write a function...

  • 344. Reverse String

    344. Reverse String Easy Write a function that reverses a...

  • 344. 反转字符串

    344. 反转字符串[https://leetcode.cn/problems/reverse-string/] ...

  • 344. Reverse String

    344. Reverse String Python: 最Pythonic的解法咯 Discuss有人问如下解法为...

  • String

    唐博主刷了String,我那周太忙了,需要慢慢自己补啦。344. Reverse String : Easy3....

  • 344. Reverse String

    https://leetcode.com/problems/reverse-string/description/...

  • 344. Reverse String

    Leetcode Day 1 344 Reverse String 题目:Write a function tha...

  • 344. Reverse String

    Problem Write a function that takes a string as input and...

  • 344. Reverse String

    Write a function that takes a string as input and returns...

  • 344. Reverse String

    1.描述 Write a function that takes a string as input and re...

网友评论

      本文标题:344. Reverse String

      本文链接:https://www.haomeiwen.com/subject/fvsunltx.html