LeetCode344:Reverse String@Python
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()类似于带有一个初始参数为None的map()。只有一个序列参数时,它返回一个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的更多相关文章
- 每天一道LeetCode--344. Reverse String
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- leetcode344——Reverse String(C++)
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- LeetCode Reverse String
原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...
- 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 ...
- [CareerCup] 1.2 Reverse String 翻转字符串
1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...
- 344. Reverse String(C++)
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
随机推荐
- UVALive 7338 (树链剖分+线段树)
Problem Toll Management IV 题目大意 给一张n个点m条边的无向图,有边权.数据保证前n-1条边构成了一棵最小生成树. 要求对于每条边求出其边权上下最多浮动范围,使得最小生成树 ...
- Canvas旋转元素
Canvas是HTML5的画布元素,有时需要按指定角度旋转某一个元素. var canvas = document.getElementById("mycanvas"); var ...
- Understanding Linux /proc/cpuinfo
http://www.richweb.com/cpu_info A hyperthreaded processor has the same number of function units as a ...
- phpstorm 激活
http://idea.lanyus.com/
- Erlang 102 Erlang并发编程
笔记系列 Erlang环境和顺序编程Erlang并发编程Erlang分布式编程YawsErlang/OTP 日期 变更说明 2014-11-02 A outline 2014 ...
- error :ld returned 1 exit status
额,被调用函数的名称与调用函数的名称写的不一致啊: 所以会出现 ld returned 1 exist status 好了, 问题解决了.
- 由于xrdp、gnome和unity之间的兼容性问题,在
由于xrdp.gnome和unity之间的兼容性问题,在Ubuntu 14.04版本中仍然无法使用xrdp登陆gnome或unity的远程桌面,现象是登录后只有黑白点为背景,无图标也无法操作.与13. ...
- Linux集群及LVS简介
一.什么是集群 通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系统中的单个计算机通常称为节点,通常通过局域网连接,但也有其它的 ...
- OS实验一实验报告
实验一.命令解释程序的编写实验 专业:商业软件工程 姓名:王泽锴 学号:201406114113 一.实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语 ...
- 关于 BCSCTL1 = CALBC1_12MHZ;DCOCTL = CALDCO_12MHZ; 的疑问
/************************************************************ * Calibration Data in Info Mem ******* ...