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

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

在这里介绍python中字符串翻转的几种方法:

1.步长为-1的切片操作。

 class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]

2.交换前后字母位置。

 class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
t = list(s)
l = len(t)
for i,j in zip(range(l-1, 0, -1), range(l//2)):
t[i], t[j] = t[j], t[i]
return "".join(t)
zip函数可参见文档:http://python.usyiyi.cn/translate/python_278/index.html
zip([iterable, ...])

该函数返回一个以元组为元素的列表,其中第 i 个元组包含每个参数序列的第 i 个元素。返回的列表长度被截断为最短的参数序列的长度。当多个参数都具有相同的长度时,zip()类似于带有一个初始参数为Nonemap()。只有一个序列参数时,它返回一个1元组的列表。没有参数时,它返回一个空的列表。

3. 递归的方式, 每次输出一个字符。
 class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
if len(s) <= 1:
return s
return reverseString(s[1:]) + s[0]

4. 双端队列, 使用extendleft()函数。

 from collections import deque
class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
d = deque()
d.extendleft(s)
return ''.join(d)

5.使用for循环, 从左至右输出。

 class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return ''.join(s[i] for i in range(len(s)-1, -1, -1))

以上内容参见博客:http://blog.csdn.net/caroline_wendy/article/details/23438739

我在LeetCode上提交的是:

 class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
l=len(s)
if l>0:
str=s[l-1]
while l!=1:
l-=1
str=str+s[l-1]
else:
return s
return str

LeetCode344:Reverse String@Python的更多相关文章

  1. 每天一道LeetCode--344. Reverse String

    Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...

  2. leetcode344——Reverse String(C++)

    Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...

  3. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  4. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  5. LeetCode Reverse String

    原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...

  6. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

  7. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  8. 344. Reverse String(C++)

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  9. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

随机推荐

  1. Android Preference

    http://blog.csdn.net/liuhe688/article/details/6448423 这个被google废弃了,替换方案是?

  2. iOS AudioQueue机制的延迟问题探究

    关键字:VOIP,AudioUnit,AudioQueue,RemoteIO问题描述VOIP通话,iOS底层音频方式采用AudioUnit机制,本来也挺好,但是会有遇到CS域来电时RemoteIO挂死 ...

  3. Android 开发错误信息001

    Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessExceptio ...

  4. 怎么实时查看mysql当前连接数

    如何实时查看mysql当前连接数? 如何实时查看mysql当前连接数? .查看当前所有连接的详细资料: ./mysqladmin -uadmin -p -h10. processlist .只查看当前 ...

  5. jQuery radio change事件 checkbox选中事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. javaScript DOM JQuery AJAX

    http://www.cnblogs.com/wupeiqi/articles/5369773.html 一 JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript ...

  7. Appium使用PageFactory初始化对象时报空指针错误

    自己的测试框架里面,每个app页面都要初始化appium field,所以想到使用一个静态的变量,后来初始化一个页面对象时总是报空指针. 在网上找了好多材料,看着没有什么区别.后来在github上面看 ...

  8. 为什么引用不了App_Code里的类(报“未能找到类型或命名空间名称”错误)

    在Web应用程序中不能通过右键项目-〉”添加“-〉”添加ASP.NET文件夹“方式添加 .因为Web应用程序中App_Code就不存在 . 不过可以通过手动的方式创建,添加一个文件夹命名为App_Co ...

  9. 基于MATLAB的GUI(Graphical User Interface)音频实时显示设计

    摘要:本文章的设计主要讲基于matlab的gui音频实时显示设计,此次设计的gui相当于一个简洁的音乐播放器,界面只有”录音“和”播放“两个控件,哈哈,够简洁吧.通过”录音“按钮可以实现声音从电脑的声 ...

  10. python---Memcached

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...