【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_5.25py
作用域
- php reids 单机命令
一.Redis连接与认证 //连接参数:ip.端口.连接超时时间,连接成功返回true,否则返回false $ret = $redis->connect('127.0.0.1', 6379, 3 ...
- Python赋值与深浅拷贝
赋值: >> a = [1, 2, 3] >>> b = a >>> a = [4, 5, 6] //赋新的值给 a >>> a [4 ...
- MySQL语言 数据库练习题分解。
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- asp.net Ajax调用Aspx后台方法
Ajax调用的前提(以aspx文件为例:) 1.首先需要在aspx文件后台中引用using System.Web.Services; 2.需要调用的方法必须是公共的(public).静态的(stati ...
- atoi函数的用法
库函数原型: #inclue <stdlib.h> int atoi(const char *nptr); 用法:将字符串里的数字字符转化为整形数.返回整形值. 注意:转化时跳过前面的空格 ...
- layer开启与关闭加载层
// 开启加载层 layer.load(2, { shade: [0.6, '#fff'], content: '数据加载中...', success: function (layero) { lay ...
- python3写入csv文件时中文为乱码
今天修改李万的爬虫时把页面上的中文写入csv文件时,中文总是乱码.通过上网搜索得到解决.解决的办法是打开文件是需加参数 encoding='utf-8-sig' .感谢博客园的菜鸟Alex.他相关博客 ...
- php之函数
scope(空间) unpack (解压) Traversable (穿越) performance(性能) experiment (检验) properties (属性) trailing (尾随) ...
- MDK5如何新建一个工程
1.首先新建一个文件夹,然后在子文件夹下新建四个子文件,子文件分别为:CORE.HALLIB.OBJ.USER 2.打开MDK5,new一个工程,然后选择开发板芯片的型号 3.在这四个文件中分别添加相 ...