Leetcode DAY8: 反转字符串 and 反转字符串II and 替换空格 and 翻转字符串里的单词 and 左旋转字符串

344.反转字符串

原本打算直接reverse解决 然后发现reverse()函数的时间复杂度是O(n),题目要求是O(1)

class Solution:
    def reverseString(self, s: List[str]) -> None:
        s.reverse()

所以采用双指针的方法 也非常简单:

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

541. 反转字符串II

class Solution:
    def reverseStr(self, s: str, k: int) -> str:

        def reverseSubstr(substr: List[str]) -> List[str]:
            left, right = 0, len(substr) - 1
            while left < right:
                substr[left], substr[right] = substr[right], substr[left]
                left += 1
                right -= 1
            return substr

        S = list(s)
        n = len(S)
        for i in range(0, n, 2 * k):
            S[i: i + k] = reverseSubstr(S[i: i + k])
            #用切片的方式避免了边界条件的处理  如果切片超过剩余长度 会返回剩余所有字符

        return "".join(S)
            
        

该题由于题目理解错误 花费了大量的时间  

实际上 只需要 把每2k段内的前k个字符反转 最后不足k的全部反转就行

用list切片的方式不需要对边界进行处理 比较方便

剑指Offer 05.替换空格

普通方法 遍历字符串  直接替换

class Solution:
    def replaceSpace(self, s: str) -> str:
        res = list(s)
        for i in range(len(res)):
            if res[i] == " ":
                res[i] = "%20"
        return "".join(res)

151.翻转字符串里的单词

class Solution:
    def reverseWords(self, s: str) -> str:
        def reverseStr(subs: List[str],start: int, end: int) -> List[str]:
            while start < end:
                subs[start], subs[end] = subs[end], subs[start]
                start += 1
                end -= 1
            return subs

        ss = list(s)
        # 去掉头尾空格
        left = 0
        right = len(ss) - 1
        while left <= right and s[left] == " ":
            left += 1
        while left <= right and s[right] == " ":
            right -= 1

        ss = ss[left:right + 1]
        # 去掉多余空格
        for i in range(0, len(ss) - 1):
            if ss[i] == ss[i + 1] and ss[i] == " ":
                ss[i] = ""          

        #再反转整个字符串
        res = reverseStr(ss, 0, len(ss) - 1)

        #再反转字符串内的子串
        st = 0
        for i in range(len(res)):
            if res[i] == " ":
                reverseStr(res, st, i - 1)
                st = i + 1
            elif i == len(res) - 1:
                reverseStr(res, st, i)

        return "".join(res)

主要逻辑: 1)去除头尾空格 ;2)去除多余空格 ; 3)反转整个字符串  4) 反转子串

剑指Offer58-II.左旋转字符串

1、用切片的方式 (左闭右开)

class Solution:
    def reverseLeftWords(self, s: str, n: int) -> str:
        s1 = s[0:n]
        s2 = s[n:len(s)]
        return s2 + s1

2、如果不能申请额外空间,只能在本串上操作(不用切片 不用reverse函数)

(用反转字符串的方式 先反转前一段 再反转后一段 再反转全部)

class Solution:
    def reverseLeftWords(self, s: str, n: int) -> str:
        def reversestr(s: list[str], left, right):
            while left < right:
                s[left], s[right] = s[right], s[left]
                left += 1
                right -= 1

        ss = list(s)
        reversestr(ss, 0, n - 1)
        reversestr(ss, n, len(ss) - 1)
        reversestr(ss, 0, len(ss) - 1)

        return "".join(ss)

你可能感兴趣的:(leetcode,算法,职场和发展)