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. 从c开始,小小感悟

    c语言是众多编程小白进入编程大门的钥匙,不过许多人在学习一段时间以后就渐渐产生了困惑,枯燥的黑色界面渐渐让他们失去了"渴望",---我还不能制作出一款像样的软件,我还是只是在算数学 ...

  2. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  3. 认识web前端

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

  4. iOSapp的json告示

    看到这篇文章,要知道这篇文章告诉你什么,就是对json的解析的一个解释,解析的代码去百度就可以了,OC的.安卓的.JS的等等都很多,但是对于swift语言的小白来说,资料就少之又少,包括一些看不懂的, ...

  5. Codeforces Round #157 (Div. 2)

    A. Little Elephant and Chess 模拟. B. Little Elephant and Magic Square 枚举左上角,计算其余两个位置的值,在\(3\times 3\) ...

  6. H20的题——[noip2003]银河英雄传(并查集)

    公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶集团派宇 ...

  7. MongoDB学习笔记四:索引

    索引就是用来加速查询的.创建数据库索引就像确定如何组织书的索引一样.但是你的优势是知道今后做何种查询,以及哪些内容需要快速查找.比如:所有的查询都包括"date"键,那么很可能(至 ...

  8. PHP如何批量生成手机号-使用PHP 如何生成一组不重复的手机号码?

    <?php //匹配手机号的正则表达式 #^(13[0-9]|14[47]|15[0-35-9]|17[6-8]|18[0-9])([0-9]{8})$# $arr = array( 130,1 ...

  9. supersocket+controller+action

    public class MasterServer : SuperSocket.SocketBase.AppServer<MasterSession> { } public class M ...

  10. JQuery AJAX 解析获得的JSON数据

    下面的解析的Json是一个二级循环. <!DOCTYPE html> <html> <head> <script src="https://code ...