一、反向
通用步骤:
- Initialize two pointer i = 0, j = array.length - 1
- while i <= j:
- Decide what you should do based on the value of array[i] and array[j]
- Move at least one pointer forward in its direction
二、同向

通用步骤:
- Initialize two pointers i and j, usually both equal to 0
- while i < array.length:
- if we need array[i], then we keep it by assigning array[i] = array[j], and move i forward, make it ready at the next position
- otherwise skip it. We do not need to move i since its spot is not fulfilled
本文探讨了两种常见的双指针算法技巧:反向遍历用于动态调整区间,适用于特定值比较;同向遍历则关注元素保留与替换策略。通过实例演示,理解这两种方法在解决数组问题中的关键作用。
1057

被折叠的 条评论
为什么被折叠?



