6

怎么用递归解决《344. 反转字符串》

 4 years ago
source link: https://sexywp.com/344-reverse-string.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

怎么用递归解决《344. 反转字符串》 – Becomin' Charles跳至内容

Becomin' Charles

Mac | Linux | 团队管理 | 架构

  • Charles : Sorry, 我测试了一下,有个语法特性在 PHP 7.0 下不支持,更新到 2.0.7 可以修复。...
  • 大盛 : 你好,我启用后报错,怎么解决啊,我的WP版本是5.6.1,PHP版本是7.0

    Parse er...

  • Wang : 现在大部分大学老师在照本宣科,学生在应付考试,计算机专业在大学毕业前没打开过GitHub大有人在。自...
  • Charles : 我上面插图里,有我家里门禁室内机的电路板。电路板正面右上角,有一个开关,那个就是开锁按钮,可以看到就...
  • 求教 : 您好,最近在研究类似方案,刚刚好搜到你的文章。虽然没有看到介绍,但是我看文章的情况,貌似您的门禁室内...

怎么用递归解决《344. 反转字符串》

这个题目太简单了,我就不贴题目的原文了。意思是这样的,给你一个字符数组(字符串),然后,你要原地把这个数组的元素顺序反转。这道题目是非常平凡的,常规解法是使用双指针,头尾指针向中间夹逼,交换头尾指针指向的字符。

class Solution:
def reverseString(self, s: List[str]) -> None:
l, r = 0, len(s) - 1
while l < r:
s[l], s[r] = s[r], s[l]
class Solution:
    def reverseString(self, s: List[str]) -> None:
        l, r = 0, len(s) - 1
        while l < r:
            s[l], s[r] = s[r], s[l]
            l += 1
            r -= 

用 Python 写,会非常短小。时间复杂度是 O(n),空间复杂度是 O(1)。

假如,让你为这个题目设计一个递归算法,你会怎么写呢?

方案一:假设我们掌握了一个把下标 [i, j] 范围内的字符顺序反转的方法。

那么对于这个题目来说,我们只要交换头尾两个字符,然后,用递归方法去计算中间剩余部分即可。

class Solution:
def reverseString(self, s: List[str]) -> None:
def reverse(i: int, j: int) -> None:
if i < j:
s[i], s[j] = s[j], s[i]
reverse(i + 1, j - 1)
reverse(0, len(s) - 1
class Solution:
    def reverseString(self, s: List[str]) -> None:
        def reverse(i: int, j: int) -> None:
            if i < j:
                s[i], s[j] = s[j], s[i]
                reverse(i + 1, j - 1)
        reverse(0, len(s) - 1

这个方法和上面双指针其实如出一辙。

方案二:深度优先搜索,从当前位置探索最后一个字符,找到后,把最后一个字符放到正确位置,然后回溯。

class Solution:
def reverseString(self, s: List[str]) -> None:
n = len(s)
def dfs(i: int):
if i == n:
return
c = s[i]
dfs(i + 1)
s[n - 1 - i] = c
dfs(0
class Solution:
    def reverseString(self, s: List[str]) -> None:
        n = len(s)
        def dfs(i: int):
            if i == n:
                return
            c = s[i]
            dfs(i + 1)
            s[n - 1 - i] = c
        dfs(0

这里很有意思的一点,就是这个变量 c 可不可以不要?为什么?

你还能想出其他的递归设计方法么?欢迎给我留言。

发布于 2021/04/26作者 Charles分类 算  法标签 algorithmdouble pointerrecursion

发表评论 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

评论

显示名称 *

电子邮箱地址 *

网站网址

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据


Recommend

  • 7
    • this-week-in-rust.org 4 years ago
    • Cache

    This Week in Rust 344

    Hello and welcome to another issue of This Week in Rust ! Rust is a systems language pursuing the trifecta: safety, concurrency, and speed. This is a weekly summary of its pro...

  • 8
    • segmentfault.com 4 years ago
    • Cache

    递归反转链表的一部分

    递归反转链表的一部分

  • 5
    • itimetraveler.github.io 4 years ago
    • Cache

    【算法】反转字符串

    研究算法能提高我们的编程功底,更好地编写出高效稳健的代码。今天,我们研究的是 —— 反转字符串。 //输入一个字符串,输出它的倒序字符串input: Hellooutput: olleH反转字符串,确实没什么难度,但是我无意间搜索了一下...

  • 6
    • www.cxyxiaowu.com 3 years ago
    • Cache

    LeetCode 第 344 号问题:反转字符串

    LeetCode 第 344 号问题:反转字符串-程序员小吴 当前位置:程序员小吴 > LeetCodeAnimation > LeetCode 第 344 号问题:反转字符串 ...

  • 6
    • www.relay.fm 3 years ago
    • Cache

    Rocket #344: Viral News - Relay FM

    Rocket #344: Viral News Rocket Accelerated geek conversation #344: Viral News

  • 4
    • segmentfault.com 3 years ago
    • Cache

    LeetCode-344-反转字符串

    LeetCode-344-反转字符串发布于 9 月 19 日反转字符串题目描述:编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[...

  • 4
    • www.youtube.com 3 years ago
    • Cache

    FreeCodeSession - Episode 344

    FreeCodeSession - Episode 3448 viewsFeb 22, 20220DislikeShareSave

  • 5
    • www.bikeshed.fm 2 years ago
    • Cache

    344: Spinner Armageddon

    About this Episode Steph has an update and a question wrapped into one about the work that is being done to migrate the Test::Unit test over to RSpec. Chris got to do something ex...

  • 6
    • labuladong.github.io 2 years ago
    • Cache

    递归魔法:反转单链表

    递归魔法:反转单链表

  • 3
    • studygolang.com 2 years ago
    • Cache

    golang 递归判断回文字符串

    golang 递归判断回文字符串 guonaihong · 2015-05-25 23:00:01 · 3501 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览    

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK