---
# System prepended metadata

title: '**Leetcode筆記(Longest Subarray of 1''s After Deleting One Element)**'
tags: [arrays, medium, leetcode]

---

# **Leetcode筆記(Longest Subarray of 1's After Deleting One Element)**


:::info
:information_source: 題目 : Longest Subarray of 1's After Deleting One Element, 類型 : arrays , 等級 : medium
日期 : 2024/09/22
:::


### 嘗試
從0開始往外擴
```python
class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        maxLen = 0
        allZero = True
        for i, n in enumerate(nums):
            if n == 0:
                allZero = False
                l, r = i - 1, i + 1
                while l >= 0 and nums[l] == 1:
                    l -= 1
                while r < len(nums) and nums[r] == 1:
                    r += 1
                maxLen = max(maxLen, r - l - 2)

        return maxLen if not allZero else len(nums) - 1
```

---
### **優化**

```python


```
---



**思路**


**講解連結**

Provided by. 
