【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 ...
随机推荐
- wchar_t和char转化
char* WcharToChar(const wchar_t* wp) { char *m_char; int len = WideCharToMultiByte(CP_ACP, 0, wp, wc ...
- Solve error LNK2001 about pcl::io::vtkPolyDataToPointCloud
When use function 'pcl::io::vtkPolyDataToPointCloud' in PCL 1.6.0, one may have error as follows: &g ...
- [Asp.net]缓存简介
写在前面 针对一些经常访问而很少改变的数据,使用缓存,可以提高性能.缓存是一种用空间换取时间的技术,说的直白点就是,第一次访问从数据库中读取数据,然后将这些数据存在一个地方,比如内存,硬盘中,再次访问 ...
- 使用 git log、git diff 命令时出现 ESC[33 和 ESC[m 乱码的解决办法
经过搜索之后了解到,出现该问题的原因是 git 使用的默认分页程序是 less,而默认的直接运行 less 的话,会无法正确解析转义字符.但是如果以 -r 命令来运行 less 的话,就可以解决了.故 ...
- Centos7下使用mail发送邮件配置
参考文档:https://blog.csdn.net/lyf844692713/article/details/81479066 安装环境查看 查看服务是否安装 rpm -qa|grep mail 如 ...
- c语言笔记 数组2
15. c99以前一直使用 gets 和 puts来输入输出字符串,但是gets因为无法获知内存大小,容易出现内存溢出(对此c99对gets,采取保留态势,c11直接废除,但是某些编译器仍然默认可以使 ...
- [No000016F]高并发下线程安全的单例模式(最全最经典)
在所有的设计模式中,单例模式是我们在项目开发中最为常见的设计模式之一,而单例模式有很多种实现方式,你是否都了解呢?高并发下如何保证单例模式的线程安全性呢?如何保证序列化后的单例对象在反序列化后任然是单 ...
- Page8:对偶原理以及结构分解[Linear System Theory]
内容包含状态转移矩阵的对偶性.方块图的对偶性.时序的对偶性以及对偶性原理,能控能观标准型及其结构分解
- hdfs mapreduce hbase
参考资料:http://www.cnblogs.com/sharpxiajun/p/5585613.html 大数据时代的数据量是超大规模的,传统的关系数据库已经很难存储和管理这些数据了,为了存储海量 ...
- LeetCode 700 Search in a Binary Search Tree 解题报告
题目要求 Given the root node of a binary search tree (BST) and a value. You need to find the node in the ...