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 ...
随机推荐
- (转)深入理解JavaScript 模块模式
深入理解JavaScript 模块模式 (原文)http://www.cnblogs.com/starweb/archive/2013/02/17/2914023.html 英文:http://www ...
- Using Interface Builder记录
1.interface builder的作用主要是用来设计视图,文件的拓展名诶.storyboard或者.xib :这两文件的区别是,.xib文件只能设计一个视图控制器,也就只能显示一个视图,.sto ...
- PyCharm5.0.2最新版破解注册激活码(图文版)
下载PyCharm http://download-cf.jetbrains.com/python/pycharm-professional-5.0.2.exe 安装PyCharm 设置激活服务器 ...
- linux grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- AppStore 内购验证的方法
AppStore增加了验证内购(In App Purchasement)的方法, 就是苹果提供一个url地址, 开发测试用: https://sandbox.itunes.apple.com/veri ...
- Notepad++配置Python开发环境
1. 安装Python 1 下载 我选择了32位的2.7版本.https://www.python.org/ftp/python/2.7.8/python-2.7.8.msi 2. 安装 安装的时候可 ...
- 适配器模式(Adapter Pattern)
将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 模式中的角色 目标接口(Target):客户所期待的接口.目标可以是具体的或 ...
- (转)神经网络和深度学习简史(第一部分):从感知机到BP算法
深度|神经网络和深度学习简史(第一部分):从感知机到BP算法 2016-01-23 机器之心 来自Andrey Kurenkov 作者:Andrey Kurenkov 机器之心编译出品 参与:chen ...
- 通过前台选择输入用来计算圆,三角形以及长方形的面积(此题目主要是while以及if 的使用)
#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现计算圆面积,三角形面积,长方形面积'''flag=Truewhile flag: pi ...
- 使用swipecard实现卡片视图左右滑动监听以及点击监听
前言: 大家好,今天给大家介绍安卓一种特别实用有很酷炫的组件swipecard,当然这并不是安卓爸爸创造的,这是国内的一个我认为是大牛的一个人随便写着玩儿搞出来了,我看了他的代码介绍已经很清晰了,但 ...