Leetcode 344. Reverse String

Problem

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

You must do this by modifying the input array in-place with O(1) extra memory.

Algorithm

Just exchange the i th item with the len(s)-1-i th item.

Code

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        slen = len(s)
        for i in range(slen // 2):
            temp = s[i]
            s[i] = s[slen-1-i]
            s[slen-1-i] = temp

相关推荐

  1. Leetcode 344. Reverse String

    2023-12-08 19:58:03       39 阅读
  2. leetcode344. 反转字符串

    2023-12-08 19:58:03       42 阅读
  3. LeetCode344 -反转字符串

    2023-12-08 19:58:03       18 阅读
  4. LeetCode 344.反转字符串

    2023-12-08 19:58:03       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 19:58:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 19:58:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 19:58:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 19:58:03       20 阅读

热门阅读

  1. rabbitmq的路由策略

    2023-12-08 19:58:03       34 阅读
  2. tomcat 如何优化?

    2023-12-08 19:58:03       34 阅读
  3. 数据库函数大全(更新中)

    2023-12-08 19:58:03       31 阅读
  4. 解决Qt发送信号指定重载

    2023-12-08 19:58:03       35 阅读
  5. Glide系列-生命周期的监听

    2023-12-08 19:58:03       33 阅读
  6. macOS sandbox 获取用户路径文件夹

    2023-12-08 19:58:03       34 阅读
  7. final, finally, finalize的区别

    2023-12-08 19:58:03       29 阅读
  8. 算法训练营Day9(字符串,以后补KMP)

    2023-12-08 19:58:03       41 阅读
  9. 数据宝库:深入探讨数据隐私与安全的要义

    2023-12-08 19:58:03       28 阅读