【LeetCode每天一题】Reverse String
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的更多相关文章
- 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) { ...
- LeetCode算法题-Reverse String II(Java实现)
这是悦乐书的第256次更新,第269篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第123题(顺位题号是541).给定一个字符串和一个整数k,你需要反转从字符串开头算起的 ...
- LeetCode算法题-Reverse String(Java实现)
这是悦乐书的第205次更新,第217篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第73题(顺位题号是344).编写一个以字符串作为输入并返回字符串的函数.例如: 输入: ...
- leetcode第七题--Reverse Integer
Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- 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 ...
- [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 ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)
Implement atoi which converts a string to an integer.The function first discards as many whitespace ...
- LeetCode Reverse String
原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
随机推荐
- day_6.7 py tcp
2018-6-7 09:20:34 #!/usr/bin/env python #!--*--coding:utf-8 --*-- #!@Time :2018/6/7 9:54 #!@Author T ...
- Centos下磁盘管理---分区
1.磁盘分区格式说明 linux分区不同于windows,linux下硬盘设备名为(IDE硬盘为hdx(x为从a—d)因为IDE硬盘最多四个,SCSI,SATA,USB硬盘为sdx(x为a—z ...
- 1.7Oob 静态变量静态方法
1)静态方法中可以直接调用同类中的静态成员,但不能直接调用非静态成员 2)如果希望在静态方法中调用非静态变量,可以通过创建类的对象,然后通过对象来访问非静态变量. 3)静态方法中不能直接调用非静态方法 ...
- 【单调栈】hdu 6319 杭电多校Problem A. Ascending Rating
http://acm.hdu.edu.cn/showproblem.php?pid=6319 从后往前更新,维护一个递减单调栈(队列) 最近很多题都是单调栈... #define _CRT_SECUR ...
- iOS知识点持续更新。。。
1.自动布局拉伸和压缩优先级 Autolayout中每个约束都有一个优先级,优先级的范围是1~1000.创建一个约束,默认的优先级最高是1000. Content Hugging Priority:该 ...
- SSH 结构中 不同角色登录,显示不同的菜单
关于这个功能,这里也就是提供一个思路,在做项目的时候因为要用到,肯定存在更好的方法,此思路仅供参考. 一.关于前台页面的接收方式 这里使用struts2的标签: <s:iterator valu ...
- User-Defined Variables
mysql> SET @w := SELECT COUNT(*) FROM course WHERE cteacher='程军'; ERROR (): You have an error in ...
- Java 输入/输出——处理流(BufferedStream、PrintStream、转换流、推回输入流)
关于使用处理流的优势,归纳起来就是两点:(1)对于开发人员来说,使用处理流进行输入/输出操作更简单:(2)使用处理流执行效率更高. 1.BufferedInputStream/BufferedOutp ...
- [skill][c][ld][gcc] 明确指定gcc在链接时明确使用静态库
通常,我们这样链接: gcc -o a.out -L/usr/lib/ -ltest a.o 这样它会链接动态库 libtest.so, 可是有时候我们想在静态库与动态库同时存在时,优先使用静态库. ...
- [administrative][CentOS] 新装系统时如何正确精准的选择基础环境和软件包
出于不同的目的,在进行全新CentOS安装的时候,我们到底应该如何作出选择. 是mininal,base server, base web server, 还是啥? 答案在这里: https://ac ...