LeetCode 题解工作台
使数组元素等于零
给你一个整数数组 nums 。 开始时,选择一个满足 nums[curr] == 0 的起始位置 curr ,并选择一个移动 方向 :向左或者向右。 此后,你需要重复下面的过程: 如果 curr 超过范围 [0, n - 1] ,过程结束。 如果 nums[curr] == 0 ,沿当前方向继续移动…
3
题型
7
代码语言
3
相关题
当前训练重点
简单 · 数组·模拟
答案摘要
假设我们初始向左移动,遇到了一个非零元素,那么我们就需要将这个元素减一,然后改变移动方向,继续移动。 因此,我们可以维护每个零值元素左侧的元素和 ,右侧元素的和 $s - l$。如果 $l = s - l$,即左侧元素和等于右侧元素和,那么我们可以选择当前零值元素,向左或向右移动,答案加 ;如果 $|l - (s - l)| = 1$,此时如果左侧元素和更大,那么我们可以选择当前零值元素,向左移动…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·模拟 题型思路
题目描述
给你一个整数数组 nums 。
开始时,选择一个满足 nums[curr] == 0 的起始位置 curr ,并选择一个移动 方向 :向左或者向右。
此后,你需要重复下面的过程:
- 如果
curr超过范围[0, n - 1],过程结束。 - 如果
nums[curr] == 0,沿当前方向继续移动:如果向右移,则 递增curr;如果向左移,则 递减curr。 - 如果
nums[curr] > 0:- 将
nums[curr]减 1 。 - 反转 移动方向(向左变向右,反之亦然)。
- 沿新方向移动一步。
- 将
如果在结束整个过程后,nums 中的所有元素都变为 0 ,则认为选出的初始位置和移动方向 有效 。
返回可能的有效选择方案数目。
示例 1:
输入:nums = [1,0,2,0,3]
输出:2
解释:
可能的有效选择方案如下:
- 选择
curr = 3并向左移动。[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0].
- 选择
curr = 3并向右移动。[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].
示例 2:
输入:nums = [2,3,4,0,4,1,0]
输出:0
解释:
不存在有效的选择方案。
提示:
1 <= nums.length <= 1000 <= nums[i] <= 100- 至少存在一个元素
i满足nums[i] == 0。
解题思路
方法一:枚举 + 前缀和
假设我们初始向左移动,遇到了一个非零元素,那么我们就需要将这个元素减一,然后改变移动方向,继续移动。
因此,我们可以维护每个零值元素左侧的元素和 ,右侧元素的和 。如果 ,即左侧元素和等于右侧元素和,那么我们可以选择当前零值元素,向左或向右移动,答案加 ;如果 ,此时如果左侧元素和更大,那么我们可以选择当前零值元素,向左移动,答案加 ,如果右侧元素和更大,那么我们可以选择当前零值元素,向右移动,答案加 。
时间复杂度 ,其中 为数组长度。空间复杂度 。
class Solution:
def countValidSelections(self, nums: List[int]) -> int:
s = sum(nums)
ans = l = 0
for x in nums:
if x:
l += x
elif l * 2 == s:
ans += 2
elif abs(l * 2 - s) == 1:
ans += 1
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n^2) in the worst case since each zero element is tried in both directions over the array. Space complexity is O(n) for temporary array copies during simulation. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Noticing the small input size implies a simulation-based solution is acceptable.
- question_mark
Expecting candidates to handle edge cases where no valid selection exists.
- question_mark
Looking for recognition that directional moves must be tested from each zero position.
常见陷阱
外企场景- error
Overwriting the original array during simulation, corrupting other trials.
- error
Failing to check both left and right directions for each zero.
- error
Miscounting valid selections when multiple zeros interact in overlapping paths.
进阶变体
外企场景- arrow_right_alt
Count the minimum steps to reduce all elements to zero rather than the number of valid selections.
- arrow_right_alt
Allow subtracting variable values instead of fixed ones during each move.
- arrow_right_alt
Extend the array length or element range to test simulation efficiency under higher constraints.