当前位置:网站首页>[LeetCode] 反转字符串【344】
[LeetCode] 反转字符串【344】
2022-07-02 22:06:00 【山茶花开时。】
问题: 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组s的形式给出
示例1
输入: s = ["h","e","l","l","o"]
输出: ["o","l","l","e","h"]
示例2
输入: s = ["H","a","n","n","a","h"]
输出: ["h","a","n","n","a","H"]
Python3解题
# 解法1:双指针
def reverseString(s):
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left = left + 1
right = right - 1
return s
# 解法2
def reverseString(s):
s.reverse()
return s
# ['o', 'l', 'l', 'e', 'h']
reverseString(["h","e","l","l","o"])
# ['h', 'a', 'n', 'n', 'a', 'H']
reverseString(["H","a","n","n","a","h"])
边栏推荐
- UE4 game architecture learning notes
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- 影视随摘
- 杰理之修改不需要长按开机功能【篇】
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- 傑理之修改不需要長按開機功能【篇】
- Market Research - current situation and future development trend of sickle cell therapy Market
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
- Jerry's modification does not require long press the boot function [chapter]
- Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
猜你喜欢
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
PMP项目整合管理
NC24325 [USACO 2012 Mar S]Flowerpot
Share how to make professional hand drawn electronic maps
大话云原生之负载均衡篇-小饭馆客流量变大了
SimpleITK使用——3. 常见操作
kubernetes 使用主机名将 pod 分配在指定节点上
【外刊】睡眠与减肥
Oracle PL / SQL programming
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
随机推荐
C language, to achieve three chess games
LxC terminal login method
I admire that someone explained such an obscure subject as advanced mathematics so easily
UE4 游戏架构 学习笔记
傑理之修改不需要長按開機功能【篇】
Radis:Linux上安装Redis(步骤)
Pointer - function pointer
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
#include errors detected. Please update your includePath.
Struct, bit segment, enumeration, union
phpcms实现订单直接支付宝支付功能
Film and television excerpts
Service visibility and observability
影视随摘
Unity发布WebGL播放声音的一种方法
How should programmers write logs
U++ 原始内存 学习笔记
Source code analysis - lightweight asynchronous crawler framework Ruia
Jerry's modification does not require long press the boot function [chapter]
杰理之内置短按再长按,不管长按多长时间都是短按【篇】