February——485. The maximum number of consecutive 1s I, II

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        
        #滑动窗口法
        res = 0
        i = 0
        windows = []
        tmp = 0
        while i<len(nums):
            
            if nums[i]!=0:
                windows.append(nums[i])

            else:
                tmp = len(windows)
                windows.clear()
            i+=1
            res = max(res,max(tmp,len(windows)))
            
        return res

        #记录有几个连续的1,不用额外空间存储
        index = -1
        res = 0
        for i,num in enumerate(nums):
            if num==0:
                index = i
            else:
                res = max(res,i-index)
        
        return res


        #使用位运算
        num = int(''.join([str(i) for i in nums]),base=2)
        res = 0
        while num>0:
            num&=num<<1
            res+=1
        
        return res
  •  Sliding window method
    • Add elements to the window if 0 is not encountered
    • If it is 0, calculate the current window length and clear the window
    • Update the maximum value. There is a detail in this is that the last element may be 1 instead of 0. All have to be compared
  •  Counting method
    • Initialized index is -1
    • If the current number is 0, then assign the 0 subscript to index
    • If the current number is not 0, then use the current coordinate i-index to update the maximum continuous number
  • Bitwise algorithm
    • Convert the list to a string, and then convert the string (only containing 0, 1) to an integer
    • Then use the bit operation to operate, the current number and the number are shifted to the left and then perform the AND operation until num is equal to 0, and then count
    • Return the result of the count

Summary: Bitwise operation is a very clever calculation of several consecutive 1s in an array. num&num<<1;res+=1

 

 

class Solution:
    def longestOnes(self, A: List[int], K: int) -> int:
        
        #题目的关键是转化为找到一个最长的字符串,该字符串含有最多不超过K个0
        size = len(A)
        right,left = 0,0
        zeros = 0
        res = 0
        while right<size:
            #对0的个数计数
            if A[right]==0:
                zeros+=1
            #如果0的个数超过K,那判断左指针是否为0,如果左指针为0,0的个数减去1,然后左指针向右移动
            while zeros>K:
                if A[left]==0:
                    zeros-=1
                left+=1
            #更新字符串的最长长度
            res = max(res,right-left+1)
            right+=1

        return res
  • The question intends to do a conversion, at most K 0s can be converted to 1, and then find the length of the longest continuous sub-array containing 1s. Converted to: Find a longest continuous string, the string contains no more than K 0
  • After the above conversion, just use the sliding window method directly .
    • Keep moving the right pointer, and then count the number of 0
    • If the number of 0 is greater than K, judge whether the left pointer is 0, if the left pointer is 0, then the number of 0 is subtracted by 1, and the left pointer is moved to the right
    • Update the longest continuous string length

 

Summary: For topics similar to replacement characters, we can convert it to calculate a longest continuous string, which contains several different characters.

Guess you like

Origin blog.csdn.net/weixin_37724529/article/details/113863709