Write a function that reverses a string. The input string is given as an array of characters char[].Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.You may assume all the characters consist of printable ascii characters.

Example 1:  Input: ["h","e","l","l","o"]                        Output: ["o","l","l","e","h"]

Example 2:   Input: ["H","a","n","n","a","h"]                Output: ["h","a","n","n","a","H"]



反转字符串:
方法一:主要的思路就是通过两个指针,一个指到开头,一个指到尾。然后交换位置。最终可以得到反转之后的字符串。时间复杂度为O(n), 空间复杂度为0(1)。     
 class Solution(object):
def reverseString(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
if len(s) < 2:
return s
start, end = 0, len(s)-1
while start < end:
s[start], s[end] = s[end], s[start]
start += 1
end -= 1
  方法二:可以利用栈的特性,从头遍历到尾,依此将数据存进栈中,然后在将数据弹出组成一个新的字符串就是反正之后的字符串。时间复杂度为O(n), 空间复杂度为O(n)。(当然这道题要求原地改变,所以这是不符合思路的)
  
方法三: 因为是使用的是Python, 所以可以直接使用一种办法就是 s[ : :-1]这样会直接将字符串进行反转,但是利用内置的模型,这不是这道题需要考察的东西。       

【LeetCode每天一题】Reverse String的更多相关文章

  1. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  2. LeetCode算法题-Reverse String II(Java实现)

    这是悦乐书的第256次更新,第269篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第123题(顺位题号是541).给定一个字符串和一个整数k,你需要反转从字符串开头算起的 ...

  3. LeetCode算法题-Reverse String(Java实现)

    这是悦乐书的第205次更新,第217篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第73题(顺位题号是344).编写一个以字符串作为输入并返回字符串的函数.例如: 输入: ...

  4. leetcode第七题--Reverse Integer

    Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  5. leetcode第24题--Reverse Nodes in k-Group

    problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...

  6. [LeetCode&Python] Problem 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  7. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  8. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  9. LeetCode Reverse String

    原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...

  10. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

随机推荐

  1. 基础知识——CentOS7操作系统的安装图文教程

    学习了很久的Linux操作系统,也看了不少的资料,对于操作系统的安装,相对来说都在不断的改进,安装的难度也在不断的降低,操作步骤也变得非常的简单了. 有很多CentOS系统的安装教程,但是比较不全面或 ...

  2. B - Calculation 2

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  3. python3 的 mysql 简单操作

    一.python 提供的 db 接口 pymysql 两个基本对象: connection.cursor 连接示例 # connect_demo.py import pymysql db = pymy ...

  4. CodeForces - 748E (枚举+脑洞)

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  5. Ubuntu下eclipse中运行Hadoop时所需要的JRE与JDK的搭配

    第一组: Eclise 版本:Indigo,Service Release 1 Build id:20110916-0149 Window-->Preferences -->Compile ...

  6. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  7. 模板倍增LCA 求树上两点距离 hdu2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...

  8. DACLs and ACEs

    [Windows]Windows的访问控制模型 - Zplutor - 博客园 https://www.cnblogs.com/zplutor/archive/2010/01/05/1639892.h ...

  9. Page12:镇定条件、镇定与极点配置的关系,解耦控制的概念等[Linear System Theory]

    内容包含镇定条件.镇定与极点配置的关系,解耦控制的概念.形式.分类以及各种解耦方法特点,系统能否解耦判断.

  10. 图->存储结构->邻接多重表

    文字描述 邻接多重表是无向图的另一种链式存储结构. 虽然邻接表是无向图的一种很有效的存储结构,在邻接表中容易求得顶点和边的各种信息. 但是,在邻接表中每一条边(vi,vj)有两个结点,分别在第i个和第 ...