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. PC缺少一个或多个网络协议 qq可登录(win10)

    打开适配器连接 1打开网络适配器   2卸载microsoft  3 网络客户端   4重启

  2. 详解Objective-C runtime

    感谢翻译小组成员wingpan热心翻译.本篇文章是我们每周推荐优秀国外的技术类文章的其中一篇.如果您有不错的原创或译文,欢迎提交给我们,更欢迎其他朋友加入我们的翻译小组(联系qq:2408167315 ...

  3. bigworld源码分析(4)——BaseAppMgr分析

    BaseAppMgr是用来管理BaseApp的,在整个bigworld中只有一个.本篇就BaseAppMgr的一些核心功能进行分析: (1) BaseAppMgr是如何通知BaseApp创建Entit ...

  4. iOS常用设计模式和机制之代理

    Delegate : 1 代理设计模式的使用我们首先需要明白三个要素 *委托方:委托别人去执行某些操作的人(对象) *代理方:被委托区执行某些操作的人(对象) *协议:(protocol)委托方需要代 ...

  5. 认识web前端

    对于一个只是浅尝辄止c语言.学过汇编语言的我,思考了半年终于在这一天入了坑,学习web前端. web前端,看着这个名字好高大上,其实我目前的理解就是写页面,是各种图片动画文字在一个页面上呈现,再一点能 ...

  6. Spring触发器配置Quartz

    参考文献:http://blog.csdn.net/liaq325/article/details/8269439 http://sundoctor.iteye.com/blog/441951 业务类 ...

  7. JavaWeb用Jdbc操作MySql数据库(一)

    一.添加开发包.在JavaWeb中用jdbc操作数据库,使用方法与java一样,但是在处理开发包的问题上有点差别.JavaWeb不能将mysql-connector-java-5.1.7-bin.ja ...

  8. Chrome渲染Transition时页面闪动Bug

    前段时间,有同事和会员反馈使用Chrome访问淘宝首页会出现画面闪动的现象,但是我在Mac和Win下面的Chrome都无法重现这个问题,后来重装了一遍Win7下的Chrome Beta版本,终于重现了 ...

  9. Apache Shiro系列教程之三:Shiro的结构

    Shiro的设计目标是简化应用的安全管理工作.软件通常是以用户为基础设计的.也就是说,我们经常是根据用户是怎样和我们的软件交互的来设计相关的用户接口.比如,你可能会说"如果是已经登录的用户与 ...

  10. nova boot instance call flow

    参考http://www.cnblogs.com/popsuper1982/p/3927390.html