双指针技巧主要分为两类:左右指针和快慢指针。
左右指针,就是两个指针相向而行或者相背而行;
快慢指针,就是两个指针同向而行,一快一慢。
数组相关的常见例题
/*
从中心向两端扩散的双指针技巧。
如果回文串的长度为奇数,则它有一个中心字符;如果回文串的长度为偶数,则可以认为它有两个中心字符。所以我们可以先实现这样一个函数:查找以 s[l] 和 s[r]为中心的最长回文子串
*/
char* Palindrome(char *s, int l, int r, int *subLen)
{
int len = strlen(s);
while (l >= 0 && r < len && s[l] == s[r]) {
l--;
r++;
}
char *res = malloc(sizeof(char) * (len + 1));
memset(res, 0, sizeof(char) * (len + 1));
// c语言截取字符中的一段
strncpy(res, s + l + 1, r - l - 1);
*subLen = r - l + 1;
return res;
}
char * longestPalindrome(char * s){
// 指针,从中间向两边扩展
int len = strlen(s);
char *res = malloc(sizeof(char) * (len + 1));
memset(res, 0, sizeof(char) * (len + 1));
int sizeRes = 0; // 当前最大回文子串的长度
for (int i = 0; i < len; ++i) {
// subLen1、subLen2 记录回文子串的长度
int subLen1 = 0;
int subLen2 = 0;
char *s1 = Palindrome(s, i, i, &subLen1); // 回文子串为奇数,只有一个中心点
char *s2 = Palindrome(s, i, i + 1, &subLen2); // 回文子串为偶数,有2个中心点
if (subLen1 > subLen2) {
if (subLen1 > sizeRes) {
sizeRes = subLen1;
strcpy(res, s1);
}
} else {
if (subLen2 > sizeRes) {
sizeRes = subLen2;
strcpy(res, s2);
}
}
}
return res;
}
class Solution {
public String Palindrome(String s, int l, int r) {
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
l--;
r++;
}
// 返回以 s[l] 和 s[r] 为中心的最长回文串
return s.substring(l + 1, r);
}
public String longestPalindrome(String s) {
String res = "";
for (int i = 0; i < s.length(); ++i) {
String s1 = Palindrome(s, i, i);
String s2 = Palindrome(s, i, i + 1);
res = res.length() < s1.length() ? s1 : res;
res = res.length() < s2.length() ? s2 : res;
}
return res;
}
}
int removeDuplicates(int* nums, int numsSize){
int left = 0; // 表示当前位置
int right = 1; // 表示下一个即将要处理的位置
// 如果nums[right] == nums[left] 表示当前的right为重复值,需要再向下找到第一个不等于nums[left]
// 的位置。因为数组有序,所以这一段单纯的right++遍历,即都是重复值
while (right < numsSize) {
if (nums[left] != nums[right]) {
left++;
nums[left] = nums[right];
}
right++;
}
return left + 1;
}
class Solution {
public int removeElement(int[] nums, int val) {
//右指针 right 指向当前将要处理的元素,左指针 left 指向下一个将要赋值的位置。
int left = 0;
int right = 0;
while (right < nums.length) {
if (nums[right] != val) {
nums[left] = nums[right];
left++;
}
right++;
}
return left;
}
}
int removeElement(int* nums, int numsSize, int val){
int left = 0;
int right = 0;
while(right < numsSize) {
if (nums[right] != val) {
nums[left] = nums[right];
left++;
}
right++;
}
return left;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize){
int left = 0;
int right = numbersSize - 1;
int *res = malloc(sizeof(int) * 2);
*returnSize = 2;
while (left < right) {
if (numbers[left] + numbers[right] < target) {
left++;
} else if (numbers[left] + numbers[right] > target){
right--;
} else {
res[0] = left + 1;
res[1] = right + 1;
return res;
}
}
return NULL;
}
class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
int left = 0;
int right = numbers.length - 1;
while (left < right) {
int tmp =numbers[left] + numbers[right];
if (tmp == target) {
res[0] = left + 1;
res[1] = right + 1;
return res;
} else if (tmp < target) {
left++;
} else {
right--;
}
}
return res;
}
}
class Solution {
public void moveZeroes(int[] nums) {
int left = 0; // 表示当前需要处理的位置
int right = 0; // 表示当前不为0的坐标
while (right < nums.length) {
if (nums[right] != 0) {
nums[left] = nums[right];
left++;
}
right++;
}
// 上述 while 循环结束之后,则 0—>left - 1 都是原数组中不为0的值,所以left->nums.length 都应该赋值为0
for (int i = left; i < nums.length; ++i) {
nums[i] = 0;
}
}
}
void moveZeroes(int* nums, int numsSize){
int left = 0;
int right = 0;
while (right < numsSize) {
if (nums[right] != 0) {
nums[left] = nums[right];
left++;
}
right++;
}
for (int i = left; i < numsSize; ++i) {
nums[i] = 0;
}
}