LeetCode344:Reverse String

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

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

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

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

1 class Solution(object):
2     def reverseString(self, s):
3         """
4         :type s: str
5         :rtype: str
6         """
7         return s[::-1]

2.交换前后字母位置。

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

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

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

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

 1 from collections import deque
 2 class Solution(object):
 3     def reverseString(self, s):
 4         """
 5         :type s: str
 6         :rtype: str
 7         """
 8         d = deque()
 9         d.extendleft(s)
10         return ‘‘.join(d)

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

1 class Solution(object):
2     def reverseString(self, s):
3         """
4         :type s: str
5         :rtype: str
6         """
7         return ‘‘.join(s[i] for i in range(len(s)-1, -1, -1)) 

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

我在LeetCode上提交的是:

 1 class Solution(object):
 2     def reverseString(self, s):
 3         """
 4         :type s: str
 5         :rtype: str
 6         """
 7         l=len(s)
 8         if l>0:
 9             str=s[l-1]
10             while l!=1:
11                 l-=1
12                 str=str+s[l-1]
13         else:
14             return s
15         return str
时间: 2024-10-27 18:26:57

LeetCode344:Reverse String的相关文章

leetcode344 Reverse String Java

自己写法,不过不被leetcode接受,提示Time Limit Exceeded~~~ public String reverseString(String s) { String res = ""; Stack<Character> stack = new Stack<>(); char[] ch = s.toCharArray(); int count = 0; for(char c : ch) { count++; stack.push(c); } fo

leetcode344——Reverse String(C++)

Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 个人博客:http://www.cnblogs.com/wdfwolf3/. 这道题就是简单的字符串逆置,在C++中字符串类型可以作为数组方式处理,所以经典的数组逆置就可以完成.这里提供两种思路: 1.classic方法头尾交

[LeetCode] Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or eq

[CareerCup] 1.2 Reverse String 翻转字符串

1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string. 这道题让我们用C++或C语言来翻转一个字符串,不算一道难题,在之前那道Reverse Words in a String 翻转字符串中的单词中用到了这个函数,跟那道题比起来,这题算简单的了.C语言的版本要比C++的稍微复杂一些,应为string类集成了很多有用的功能,比如得到字符串的长度,用下标

[LeetCode] 344 Reverse String &amp; 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns

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 of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take t

344. Reverse String(C++)

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 题目大意: 字符串倒置. 解题方法: 第一个字符与最后一个非空字符对换. 注意事项: 1.字符串最后一个字符是空字符. C++代码: 1.不良代码: 1 class Solution

Leetcode刷题记录[python]——344 Reverse String

一.前言 不是计算机专业出身,却有一颗程序猿的心. 昨日开始leetcode第一次刷题,选择了菜鸟方式,从AC率最高且难度为Easy的题开始,不管题是简单还是难,都想做个记录,既是方便以后回顾,又是以此作为一个激励,督促自己每天都能有所进步. 二.题344 Reverse String Write a function that takes a string as input and returns the string reversed. class Solution(object): def

leetCode 344. Reverse String 字符串

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 思路1: 使用一个新的string来存放结果. class Solution { public:     string reverseString(string s) {