双指针:什么是双指针(对撞指针、快慢指针)
双指针,指的是在遍历对象的过程中,不是普通的使用单个指针进行访问,而是使用两个相同方向(快慢指针)或者相反方向(对撞指针)的指针进行扫描,从而达到相应的目的。
换言之,双指针法充分使用了数组有序这一特征,从而在某些情况下能够简化一些运算
进一步说明
-
对撞指针:对撞指针是指在有序数组中,将指向最左侧的索引定义为左指针(left),最右侧的定义为右指针(right),然后从两头向中间进行数组遍历。
对撞数组适用于有序数组,也就是说当你遇到题目给定有序数组时,应该第一时间想到用对撞指针解题。
图示:

-
快慢指针: 快慢指针也是双指针,但是两个指针从同一侧开始遍历数组,将这两个指针分别定义为快指针(fast) 和慢指针(slow) ,两个指针以不同的策略移动,直到两个指针的值相等(或其他特殊条件)为止,如fast每次增长两个,slow每次增长一个。
图示:

有一篇文章写得不错,https://zhuanlan.zhihu.com/p/71643340
双指针编程题:
- 2sum:
题目:两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
说明:
- 返回的下标值(index1 和 index2)不是从零开始的。
- 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
示例:
输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
图示:
代码:
class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return null;
}
int[] result = {-1, -1};
int start = 0;
int end = nums.length - 1;
while (start < end) {
if (nums[start] + nums[end] < target) {
start++;
} else if (nums[start] + nums[end] > target) {
end--;
} else {
result[0] = start + 1;
result[1] = end + 1;
break;
}
}
return result;
}
}
- 3sum:
题目:三数之和
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意: 答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
图示:
代码:
class Solution {
public List<List<Integer>> threeSum(int[] numbers) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (numbers == null || numbers.length == 0) {
return result;
}
//nlog(n)
Arrays.sort(numbers);
int len = numbers.length;
for (int i = 0; i < len - 2; i++) {
if (i > 0 && numbers[i] == numbers[i - 1]) {
continue;
}
int left = i + 1;
int right = len - 1;
while(left < right) {
if (numbers[i] + numbers[left] + numbers[right] == 0) {
List<Integer> list = new ArrayList<Integer>();
list.add(numbers[i]);
list.add(numbers[left]);
list.add(numbers[right]);
left++;
right--;
result.add(list);
while (left < right && numbers[left] == numbers[left - 1]) {
left++;
}
while (left < right && numbers[right] == numbers[right + 1]) {
right--;
}
} else if (numbers[i] + numbers[left] + numbers[right] > 0) {
right--;
} else {
left++;
}
}
}
return result;
}
}
- 验证三角形:
题目:有效三角形的个数
给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。
示例 1:
输入: [2,2,3,4]
输出: 3
解释:
有效的组合是:
2,3,4 (使用第一个 2)
2,3,4 (使用第二个 2)
2,2,3
注意:
- 数组长度不超过1000。
- 数组里整数的范围为 [0, 1000]。
图示:
代码:
class Solution {
public int triangleNumber(int[] nums) {
if (nums == null || nums.length < 3) {
return 0;
}
Arrays.sort(nums);
int total = 0;
for (int k = S.length - 1; k >= 2; k--) {
int start = 0;
int end = k - 1;
while (Start < end) {
if (nums[start] + nums[end] > nums[k]) {
total += (end - start);
end --;
} else {
start++;
}
}
}
return total;
}
}
- 存水问题:
题目:接雨水

图示:
代码:
class Solution {
public int trap(int[] height) {
if(height == null || height.length == 0) {
return 0;
}
int length = height.length;
int left = 0;
int right = length - 1;
int leftHeight = height[left];
int rightHeight = height[right];
int sum = 0;
while (left < right) {
if (leftHeight < rightHeight) {
if (leftHeight > height[left + 1]) {
sum += leftHeight - height[left + 1];
} else {
leftHeight = height[left +1];
}
left++;
} else {
if (rightHeight > height[right - 1]) {
sum += rightHeight - height[right - 1];
} else {
rightHeight = height[right - 1];
}
right--;
}
}
return sum;
}
}
本文介绍双指针算法,包括对撞指针与快慢指针的概念及应用,并通过实例讲解如何利用双指针解决2sum、3sum等问题,以及如何在存水问题中运用此技巧。
7073

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



