Leetcode python 刷题笔记——字符串

2022/3/19 12:58:54

本文主要是介绍Leetcode python 刷题笔记——字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

字符串

344. 反转字符串

用双指针法轻松搞定!

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        left, right = 0, len(s)-1
        
        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1
        
        return 0

541. 反转字符串II

class Solution:
    def reverseStr(self, s: str, k: int) -> str:
        
        def reverse(text) :
            left, right = 0, len(text)-1
            while left < right:
                text[left], text[right] = text[right], text[left]
                left += 1
                right -= 1
            return text
        
        res = list(s)
        
        for i in range(0, len(s), 2*k):
            res[i:i+k] = reverse(res[i:i+k])
        
        return ''.join(res)

151. 翻转字符串里的单词

class Solution:
    def reverseWords(self, s: str) -> str:        
        s = s.strip().split()
        
        left, right = 0, len(s)-1
        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1
        
        s = ' '.join(s)
    
        return s
            

class Solution:
  #1.去除多余的空格
        def trim_spaces(self,s):     
            n=len(s)
            left=0
            right=n-1
        
            while left<=right and s[left]==' ':       #去除开头的空格
                left+=1
            while left<=right and s[right]==' ':      #去除结尾的空格
                right=right-1
            tmp=[]
            while left<=right:                        #去除单词中间多余的空格
                if s[left]!=' ':
                    tmp.append(s[left])
                elif tmp[-1]!=' ':                    #当前位置是空格,但是相邻的上一个位置不是空格,则该空格是合理的
                    tmp.append(s[left])
                left+=1
            return tmp
#2.翻转字符数组
        def reverse_string(self,nums,left,right):
            while left<right:
                nums[left], nums[right]=nums[right],nums[left]
                left+=1
                right-=1
            return None
#3.翻转每个单词
        def reverse_each_word(self, nums):
            start=0
            end=0
            n=len(nums)
            while start<n:
                while end<n and nums[end]!=' ':
                    end+=1
                self.reverse_string(nums,start,end-1)
                start=end+1
                end+=1
            return None

#4.翻转字符串里的单词
        def reverseWords(self, s): #测试用例:"the sky is blue"
            l = self.trim_spaces(s)                     #输出:['t', 'h', 'e', ' ', 's', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e'
            self.reverse_string( l,  0, len(l) - 1)   #输出:['e', 'u', 'l', 'b', ' ', 's', 'i', ' ', 'y', 'k', 's', ' ', 'e', 'h', 't']
            self.reverse_each_word(l)               #输出:['b', 'l', 'u', 'e', ' ', 'i', 's', ' ', 's', 'k', 'y', ' ', 't', 'h', 'e']
            return ''.join(l)                                 #输出:blue is sky the



这篇关于Leetcode python 刷题笔记——字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程