当前位置:网站首页>[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"])
边栏推荐
猜你喜欢
SimpleITK使用——4. 奇怪的问题
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Mathematical modeling -- graph and network models and methods (I)
540. Single element in ordered array
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
UE4 游戏架构 学习笔记
Simpleitk use - 3 Common operations
SimpleITK使用——3. 常见操作
C language, to achieve three chess games
对象与对象变量
随机推荐
Pointer array parameter passing, pointer parameter passing
[QT] QT multithreading development - reentrancy and thread safety
SimpleITK使用——4. 奇怪的問題
Oracle-游标
Market Research - current market situation and future development trend of handheld wound imaging equipment
NC24325 [USACO 2012 Mar S]Flowerpot
540. Single element in ordered array
Ransack combined condition search implementation
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Socket套接字C/S端流程
UE4 UI自适应屏幕
Using rendertext() to output multiple lines of text with rendertext() in R shiny
New feature of go1.18: introduce new netip Network Library
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
UE4 UI adaptive screen
U++ learning notes - relaxation
影视随摘