leetcode-344. Reverse String 字符串翻转,切片的用法

来源:互联网 发布:Ubuntu sambaclient 编辑:程序博客网 时间:2024/06/05 17:36

题目:

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

Example:
Given s = "hello", return "olleh".

题意:反转一个字符串,倒序输出


反转字符串,这个题python只用了一句话:

class Solution(object):

    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        

        return s[::-1]


笔记:

切片的概念:

python可对list、tuple、string的数据类型进行切片,即可获取list、tuple、string的部分元素,切片方法非常灵活。

以string类型为例:s=[abcdefg]

顺序截取:

起始位置:终止位置:每隔多少个获取一个字符      当某个位置省略时,用:号代替

例如:



逆序截取:

记住倒数第一个元素的索引-1:

终止位置:起始位置      当某个位置省略时,用:号代替




0 0