leetcode-344 | 反转字符串


title: leetcode-344 | 反转字符串
date: 2019-4-24 14:40:40
comments: true
categories: “leetcode”
tags:
- leetcode 简单难度

344. 反转字符串( Reverse String)

编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。

示例1:
输入:[“h”,“e”,“l”,“l”,“o”]
输出:[“o”,“l”,“l”,“e”,“h”]
示例2:
输入:[“H”,“a”,“n”,“n”,“a”,“h”]
输出:[“h”,“a”,“n”,“n”,“a”,“H”]

思路

方法一: reverse函数

python中有reverse函数,用于列表的操作。这里我们也直接使用这种方式。

class Solution(object):
    def reverseString(self, s):
        """
        :type s: List[str]
        :rtype: None Do not return anything, modify s in-place instead.
        """
        s.reverse()

结果:
执行用时 : 232 ms, 在Reverse String的Python提交中击败了65.80% 的用户
内存消耗 : 18.6 MB, 在Reverse String的Python提交中击败了79.84% 的用户

提交时间 状态 执行用时 内存消耗 语言
几秒前 通过 232 ms 18.6MB python

方法二:我们自己定义指针操作

定义ij两个指针,分别指向首尾,在遍历的过程中,交换。

class Solution(object):
    def reverseString(self, s):
        """
        :type s: List[str]
        :rtype: None Do not return anything, modify s in-place instead.
        """
        i,j = 0, len(s)-1
        while i<=j:
            s[i],s[j] = s[j], s[i]
            i+=1
            j-=1
        

结果:
执行用时 : 376 ms, 在Reverse String的Python提交中击败了13.38% 的用户
内存消耗 : 18.7 MB, 在Reverse String的Python提交中击败了60.28% 的用户

提交时间 状态 执行用时 内存消耗 语言
几秒前 通过 376 ms 18.7MB python
发布了169 篇原创文章 · 获赞 139 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/102641730