# leetcode刷题-双指针

## 1. 两数之和 II - 输入有序数组

### 题目

``````var twoSum = function(numbers, target) {
let left = 0, right = numbers.length - 1;
let result = [];

while(right > left) {
let sum = numbers[left] + numbers[right];
if (sum === target) {
result.push(left + 1, right + 1);
return result;
} else if (sum > target) {
right--;
} else {
left++;
}
}
return result;
};

## 2. 平方数之和

### 题目

``````/**
* @param {number} c
* @return {boolean}
*/
var judgeSquareSum = function(c) {
let left = 0, right = Math.ceil(Math.sqrt(c));
while(right >= left) {
let value = left * left + right * right;
if (value === c) {
return true;
} else if (value > c) {
right--;
} else {
left++;
}
}
return false;
};

## 3. 反转字符串中的元音字母

### 题目

``````/**
* @param {string} s
* @return {string}
*/
var reverseVowels = function(s) {
const sets = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);

let resArr = s.split('');
let left = 0, right = resArr.length - 1;

while(right > left) {
const leftFlag = sets.has(resArr[left]);
const rightFlag = sets.has(resArr[right]);

if (leftFlag && rightFlag) {
swap(resArr, left, right);
left++;
right--;
} else if (leftFlag) {
right--;
} else {
left++;
}
}
return resArr.join('');
};

function swap(arr, i, j) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

## 4. 验证回文字符串 Ⅱ

### 题目

``````/**
* @param {string} s
* @return {boolean}
*/
var validPalindrome = function(s) {
let left = 0, right = s.length - 1;
while(right > left) {
if (s.charAt(left) !== s.charAt(right)) {
return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1);
}
right--;
left++;
}
return true;
};

function isPalindrome(str, left, right) {
while(right > left) {
if (str.charAt(left) !== str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}

## 5. 合并两个有序数组

### 题目

``````// 注意要从数组后面往前遍历
var merge = function(nums1, m, nums2, n) {
let index1 = m - 1, index2 = n - 1, index = m + n - 1;

while(index1 !== -1 || index2 !== -1) {
if (index1 === -1) {
nums1[index--] = nums2[index2--];
} else if (index2 === -1) {
nums1[index--] = nums1[index1--];
} else if (nums2[index2] > nums1[index1]) {
nums1[index--] = nums2[index2--];
} else {
nums1[index--] = nums1[index1--];
}
}
};

## 6. 环形链表

### 题目

``````// 快慢指针

while(slow && fast) {
slow = slow.next;
if (!fast.next) {
return false;
}
fast = fast.next.next;
if (slow === fast) {
return true;
}
}

return false;
};

## 7. 通过删除字母匹配到字典里最长单词

### 题目

``````var findLongestWord = function(s, dictionary) {
let maxLength = 0, res = '';
dictionary.forEach(item => {
if (isSubString(s, item)) {
if (item.length > maxLength) {
maxLength = item.length;
res = item;
} else if (item.length === maxLength) {
res = item > res ? res : item;
}
}
})
return res;
};

function isSubString(str, subStr) {
let index = 0, subIndex = 0;
let strLength = str.length, subLength = subStr.length;

while(index < strLength) {
if (subIndex === subLength) {
return true;
}
if (str[index] === subStr[subIndex]) {
subIndex++;
}
index++;
}
return subIndex === subLength;
}