2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-05-14 18:03:11 +08:00
|
|
|
|
* [1. 有序数组的 Two Sum](#1-有序数组的-two-sum)
|
|
|
|
|
* [2. 两数平方和](#2-两数平方和)
|
|
|
|
|
* [3. 反转字符串中的元音字符](#3-反转字符串中的元音字符)
|
|
|
|
|
* [4. 回文字符串](#4-回文字符串)
|
|
|
|
|
* [5. 归并两个有序数组](#5-归并两个有序数组)
|
|
|
|
|
* [6. 判断链表是否存在环](#6-判断链表是否存在环)
|
|
|
|
|
* [7. 最长子序列](#7-最长子序列)
|
2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-08 20:31:07 +08:00
|
|
|
|
双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
# 1. 有序数组的 Two Sum
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[Leetcode :167. Two Sum II - Input array is sorted (Easy)](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Input: numbers={2, 7, 11, 15}, target=9
|
|
|
|
|
Output: index1=1, index2=2
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
题目描述:在有序数组中找出两个数,使它们的和为 target。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
使用双指针,一个指针指向值较小的元素,一个指针指向值较大的元素。指向较小元素的指针从头向尾遍历,指向较大元素的指针从尾向头遍历。
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
- 如果两个指针指向元素的和 sum == target,那么得到要求的结果;
|
|
|
|
|
- 如果 sum > target,移动较大的元素,使 sum 变小一些;
|
|
|
|
|
- 如果 sum < target,移动较小的元素,使 sum 变大一些。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public int[] twoSum(int[] numbers, int target) {
|
|
|
|
|
int i = 0, j = numbers.length - 1;
|
|
|
|
|
while (i < j) {
|
|
|
|
|
int sum = numbers[i] + numbers[j];
|
|
|
|
|
if (sum == target) {
|
|
|
|
|
return new int[]{i + 1, j + 1};
|
|
|
|
|
} else if (sum < target) {
|
|
|
|
|
i++;
|
|
|
|
|
} else {
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
# 2. 两数平方和
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[633. Sum of Square Numbers (Easy)](https://leetcode.com/problems/sum-of-square-numbers/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Input: 5
|
|
|
|
|
Output: True
|
|
|
|
|
Explanation: 1 * 1 + 2 * 2 = 5
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
题目描述:判断一个数是否为两个数的平方和。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public boolean judgeSquareSum(int c) {
|
|
|
|
|
int i = 0, j = (int) Math.sqrt(c);
|
|
|
|
|
while (i <= j) {
|
|
|
|
|
int powSum = i * i + j * j;
|
|
|
|
|
if (powSum == c) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (powSum > c) {
|
|
|
|
|
j--;
|
|
|
|
|
} else {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
# 3. 反转字符串中的元音字符
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[345. Reverse Vowels of a String (Easy)](https://leetcode.com/problems/reverse-vowels-of-a-string/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Given s = "leetcode", return "leotcede".
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
使用双指针指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-05-14 18:03:11 +08:00
|
|
|
|
private final static HashSet<Character> vowels = new HashSet<>(
|
|
|
|
|
Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
public String reverseVowels(String s) {
|
|
|
|
|
int i = 0, j = s.length() - 1;
|
|
|
|
|
char[] result = new char[s.length()];
|
|
|
|
|
while (i <= j) {
|
|
|
|
|
char ci = s.charAt(i);
|
|
|
|
|
char cj = s.charAt(j);
|
|
|
|
|
if (!vowels.contains(ci)) {
|
|
|
|
|
result[i++] = ci;
|
|
|
|
|
} else if (!vowels.contains(cj)) {
|
|
|
|
|
result[j--] = cj;
|
|
|
|
|
} else {
|
|
|
|
|
result[i++] = cj;
|
|
|
|
|
result[j--] = ci;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new String(result);
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
# 4. 回文字符串
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[680. Valid Palindrome II (Easy)](https://leetcode.com/problems/valid-palindrome-ii/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Input: "abca"
|
|
|
|
|
Output: True
|
|
|
|
|
Explanation: You could delete the character 'c'.
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
题目描述:可以删除一个字符,判断是否能构成回文字符串。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public boolean validPalindrome(String s) {
|
2019-05-14 18:03:11 +08:00
|
|
|
|
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
|
2019-03-27 20:57:37 +08:00
|
|
|
|
if (s.charAt(i) != s.charAt(j)) {
|
|
|
|
|
return isPalindrome(s, i, j - 1) || isPalindrome(s, i + 1, j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private boolean isPalindrome(String s, int i, int j) {
|
|
|
|
|
while (i < j) {
|
|
|
|
|
if (s.charAt(i++) != s.charAt(j--)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
# 5. 归并两个有序数组
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[88. Merge Sorted Array (Easy)](https://leetcode.com/problems/merge-sorted-array/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input:
|
2019-03-27 20:57:37 +08:00
|
|
|
|
nums1 = [1,2,3,0,0,0], m = 3
|
|
|
|
|
nums2 = [2,5,6], n = 3
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Output: [1,2,2,3,5,6]
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
题目描述:把归并结果存到第一个数组上。
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
需要从尾开始遍历,否则在 nums1 上归并得到的值会覆盖还未进行归并比较的值。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public void merge(int[] nums1, int m, int[] nums2, int n) {
|
|
|
|
|
int index1 = m - 1, index2 = n - 1;
|
|
|
|
|
int indexMerge = m + n - 1;
|
|
|
|
|
while (index1 >= 0 || index2 >= 0) {
|
|
|
|
|
if (index1 < 0) {
|
|
|
|
|
nums1[indexMerge--] = nums2[index2--];
|
|
|
|
|
} else if (index2 < 0) {
|
|
|
|
|
nums1[indexMerge--] = nums1[index1--];
|
|
|
|
|
} else if (nums1[index1] > nums2[index2]) {
|
|
|
|
|
nums1[indexMerge--] = nums1[index1--];
|
|
|
|
|
} else {
|
|
|
|
|
nums1[indexMerge--] = nums2[index2--];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
# 6. 判断链表是否存在环
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[141. Linked List Cycle (Easy)](https://leetcode.com/problems/linked-list-cycle/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
使用双指针,一个指针每次移动一个节点,一个指针每次移动两个节点,如果存在环,那么这两个指针一定会相遇。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public boolean hasCycle(ListNode head) {
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
ListNode l1 = head, l2 = head.next;
|
|
|
|
|
while (l1 != null && l2 != null && l2.next != null) {
|
|
|
|
|
if (l1 == l2) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
l1 = l1.next;
|
|
|
|
|
l2 = l2.next.next;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
# 7. 最长子序列
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[524. Longest Word in Dictionary through Deleting (Medium)](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
Input:
|
2019-03-27 20:57:37 +08:00
|
|
|
|
s = "abpcplea", d = ["ale","apple","monkey","plea"]
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
Output:
|
|
|
|
|
"apple"
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
题目描述:删除 s 中的一些字符,使得它构成字符串列表 d 中的一个字符串,找出能构成的最长字符串。如果有多个相同长度的结果,返回字典序的最小字符串。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
通过删除字符串 s 中的一个字符能得到字符串 t,可以认为 t 是 s 的子序列,我们可以使用双指针来判断一个字符串是否为另一个字符串的子序列。
|
|
|
|
|
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
public String findLongestWord(String s, List<String> d) {
|
|
|
|
|
String longestWord = "";
|
|
|
|
|
for (String target : d) {
|
|
|
|
|
int l1 = longestWord.length(), l2 = target.length();
|
|
|
|
|
if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-14 18:03:11 +08:00
|
|
|
|
if (isSubstr(s, target)) {
|
2019-03-27 20:57:37 +08:00
|
|
|
|
longestWord = target;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return longestWord;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 18:03:11 +08:00
|
|
|
|
private boolean isSubstr(String s, String target) {
|
2019-03-27 20:57:37 +08:00
|
|
|
|
int i = 0, j = 0;
|
|
|
|
|
while (i < s.length() && j < target.length()) {
|
|
|
|
|
if (s.charAt(i) == target.charAt(j)) {
|
|
|
|
|
j++;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return j == target.length();
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-10 11:23:18 +08:00
|
|
|
|
# 微信公众号
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
微信公众号 CyC2018 提供了该项目的离线阅读版本,后台回复 "下载" 即可领取。也提供了一份技术面试复习大纲,不仅系统整理了面试知识点,而且标注了各个知识点的重要程度,从而帮你理清多而杂的面试知识点,后台回复 "大纲" 即可领取。我基本是按照这个大纲来进行复习的,对我拿到了 BAT 头条等 Offer 起到很大的帮助。你们完全可以和我一样根据大纲上列的知识点来进行复习,就不用看很多不重要的内容,也可以知道哪些内容很重要从而多安排一些复习时间。
|
|
|
|
|
|
|
|
|
|
|
2019-06-09 22:59:34 +08:00
|
|
|
|
<img width="580px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/other/公众号海报2.png"></img>
|