avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1004.Maximum continuity1Number III Maximum continuity1Number III

  2022-12-07        
字数统计: 297字   |   阅读时长: 1min

Today’s daily question is too difficult,So find the problem by yourself。Today’s question is a hash table+Sliding window algorithm,虽然感觉只用了Sliding window algorithm。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

class Solution:
def longestOnes(self, nums: List[int], k: int) -> int:
k_mean = k
# Used to record how many arrays there are now
flag = 0
# Used to record the maximum land number
max_flag = 0
for start in range(len(nums)):
tail = start
while k >= 0 and tail <= len(nums) - 1:
if nums[tail] == 1:
tail += 1
flag += 1
elif nums[tail] == 0 and k > 0:
tail += 1
k -= 1
flag += 1
elif nums[tail] == 0 and k == 0:
k = k_mean
max_flag = max(max_flag, flag)
flag = 0
break
if tail == len(nums):
max_flag = max(max_flag, flag)
flag = 0
break
return max_flag

This is my approach at the beginning,Although the double pointer is used,But there is no flexibility,Very empty feeling,Very dry slide。
the following@Lincoln@Lincoln Big practice,Just as one record of yourself,不作为我的answer发表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def longestOnes(self, nums: List[int], k: int) -> int:
"""
Thinking:1. k=0It can be understood as the problem of the maximum duplication sub -string
2. If the current window value-In the window1Number <= k: Then expand the window(right+1)
If the current window value-In the window1Number > k: Swipe the window to the right(left+1)
method:Hash table + Sliding window
"""
n = len(nums)
o_res = 0
left = right = 0
while right < n:
if nums[right]== 1: o_res += 1
if right-left+1- o_res > k:
if nums[left]== 1: o_res -= 1
left += 1
right += 1
return right - left

Look atLincolnBigThinking,very clearly,remember

  • Python
  • solved,answer

扫一扫,分享到微信

微信分享二维码
Frontend Development Learning Path
目录
ahoh, this article has no catalog.

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号