------
## 最终结论
```python
def longest_subarray(nums):
    left = 0
    right = 0
    zero_count = 0
    max_length = 0
    
    while right < len(nums):
        if nums[right] == 0:
            zero_count += 1
        
        while zero_count > 1:
            if nums[left] == 0:
                zero_count -= 1
            left += 1
        
        max_length = max(max_length, right - left)
        right += 1
    
    if all(num == 1 for num in nums):
        return len(nums) - 1
    return max_length

# Test cases
print(longest_subarray([1,1,0,1])) # Output: 3
print(longest_subarray([0,1,1,1,0,1,1,0,1])) # Output: 5
print(longest_subarray([1,1,1])) # Output: 2
print(longest_subarray([1,1,0,0,1,1,1,0,1])) # Output: 4
print(longest_subarray([0,0,0])) # Output: 0
```