代码随想录算法训练营第八天| 344.反转字符串、541.反转字符串II、剑指Offer 05.替换空格、151.翻转字符串里的单词、剑指Offer58-II.左旋转字符串

一、今日任务

二、反转字符串

2.1 题目:344. 反转字符串

2.2 解题过程

输入:字符数组

要求进行的操作:将原数组内容反转,且空间复杂度为O(1)

要求输出:无

思路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) > 1:
            for index in range(1, len(s)):
                temp = s[index]
                for i in range(index, 0, -1):
                    s[i] = s[i-1]
                s[0] = temp

思路2:头尾双向指针会成功吗?试一下。成功啦!

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) > 1:
            left = 0
            right = len(s)-1
            while left < right:
                temp = s[left]
                s[left] = s[right]
                s[right] = temp
                left += 1
                right -= 1

2.3 阅读材料改进

思路一致。

三、反转字符串 II

3.1 题目:541. 反转字符串 II

3.2 解题过程

整个思路很混乱,最后才胡乱理清楚。

class Solution(object):
    def reverseStr(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: str
        """
        news = ""
        index = 0
        while index < len(s):
            left = index
            right = min(len(s)-1, index + k - 1)
            while right >= left:
                news += s[right]
                right -= 1
            if index+k -1 < len(s)-1:
                if index + 2*k <= len(s) -1 :
                    news += s[index + k : index + 2*k]
                    index += 2*k
                elif index + 2*k > len(s) -1 :
                    news += s[index + k: len(s)]
                    break
            else:
                break
        return news

3.3 阅读材料改进

随想录在python语言方面用了蛮多字符串类的函数的。

四、剑指 Offer 05. 替换空格

4.1 题目:剑指 Offer 05. 替换空格

4.2 解题过程

非常简单的题目。

class Solution(object):
    def replaceSpace(self, s):
        """
        :type s: str
        :rtype: str
        """
        news = ""
        for i in s:
            if i == " ":
                news += "%20"
            else:
                news += i
        return news

4.3 阅读材料改进

随想录的要求的不另外使用另外的数组就是原字符串上进行。

以下是复现代码:

class Solution:
    def replaceSpace(self, s: str) -> str:
        counter = s.count(' ')
        res = list(s)
        res.extend([' '] * counter * 2)
        left, right = len(s) - 1, len(res) - 1      
        while left >= 0:
            if res[left] != ' ':
                res[right] = res[left]
                right -= 1
            else:
                res[right - 2: right + 1] = '%20'
                right -= 3
            left -= 1
        return ''.join(res)

五、反转字符串中的单词

5.1 题目:151.反转字符串中的单词

5.2 解题过程

这题在大一大二真的非常常见。

按照以前的思路:

class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        slist = list(s)
        wordlist = []
        word = ''
        for i in slist:
            if i != ' ':
                word += i
            if i == ' ':
                if word != '':
                    wordlist.append(word)
                word = ''
        if word != '':
            wordlist.append(word)
        news = wordlist[len(wordlist)-1]
        for index in range(len(wordlist)-2, -1, -1):
            if wordlist[index] !=' ' :
                news += (' '+wordlist[index])
        return news

进阶依旧要求空间复杂度为O(1)

没什么思路。

5.3 阅读材料改进

主要的步骤是:

去除多余的空格。

字符串反转。

单词反转。

class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        slist = list(s)
        remove_slist = []
        lindex = 0
        while slist[lindex]==' ':
            lindex += 1
        rindex = len(slist)-1
        while slist[rindex] == ' ':
            rindex -= 1
        remove_slist = slist[lindex:rindex+1]
        length = len(remove_slist)
        index = 0
        while index < length:
            if remove_slist[index]==' ' and remove_slist[index-1] == ' ':
                remove_slist = remove_slist[: index]+remove_slist[index+1:]
                length -= 1
                index -= 1
            index += 1
        # 反转整个字符串
        left = 0
        right = len(remove_slist) - 1
        while left < right:
            remove_slist[left], remove_slist[right] = remove_slist[right], remove_slist[left]
            left += 1
            right -= 1
        # 反转单词
        left = 0
        for index in range(0, len(remove_slist)):
            if remove_slist[index] == ' ':
                right = index - 1
                while left < right:
                    remove_slist[left], remove_slist[right] = remove_slist[right], remove_slist[left]
                    left += 1
                    right -= 1
                left = index + 1
        right = len(remove_slist)-1
        while left < right:
            remove_slist[left], remove_slist[right] = remove_slist[right], remove_slist[left]
            left += 1
            right -= 1
        return ''.join(remove_slist)

六、剑指 Offer 58 - II. 左旋转字符串

6.1 题目:剑指 Offer 58 - II. 左旋转字符串

6.2 解题过程

很简单的思路:

class Solution(object):
    def reverseLeftWords(self, s, n):
        """
        :type s: str
        :type n: int
        :rtype: str
        """
        slist = list(s)
        if n < len(s):
            changelist = slist[0:n]
            rlist = slist[n:]+changelist
        else:
            rlist = slist
        return ''.join(rlist)

进阶依旧要求空间复杂度为O(1),那就只能时间换空间。

class Solution(object):
    def reverseLeftWords(self, s, n):
        """
        :type s: str
        :type n: int
        :rtype: str
        """
        slist = list(s)
        if n < len(s):
            for index in range(0, n):
                temp = slist[0]
                for i in range(0, len(slist)-1):
                    slist[i] = slist[i+1]
                slist[len(slist)-1] = temp
        return ''.join(slist)

会超时。

6.3 阅读材料改进

主要的步骤是:

反转前n个。

反转全部。

反转前len(s)-n个。

class Solution(object):
    def reverseLeftWords(self, s, n):
        """
        :type s: str
        :type n: int
        :rtype: str
        """
        slist = list(s)
        if n < len(s):
            # 反转前n个。
            left = 0
            right = n-1
            while left < right:
                slist[left], slist[right] = slist[right], slist[left]
                left += 1
                right -= 1
            # 反转整个字符串
            left = 0
            right = len(slist) - 1
            while left < right:
                slist[left], slist[right] = slist[right], slist[left]
                left += 1
                right -= 1
            #反转前 len(slist)-n 个
            left = 0
            right = len(slist) - n - 1
            while left < right:
                slist[left], slist[right] = slist[right], slist[left]
                left += 1
                right -= 1
        return ''.join(slist)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值