auto commit

This commit is contained in:
CyC2018
2019-05-14 18:03:11 +08:00
parent 688398585f
commit 2dee14d978
4 changed files with 88 additions and 78 deletions

View File

@ -1,17 +1,17 @@
<!-- GFM-TOC -->
* [有序数组的 Two Sum](#有序数组的-two-sum)
* [两数平方和](#两数平方和)
* [反转字符串中的元音字符](#反转字符串中的元音字符)
* [回文字符串](#回文字符串)
* [归并两个有序数组](#归并两个有序数组)
* [判断链表是否存在环](#判断链表是否存在环)
* [最长子序列](#最长子序列)
* [1. 有序数组的 Two Sum](#1-有序数组的-two-sum)
* [2. 两数平方和](#2-两数平方和)
* [3. 反转字符串中的元音字符](#3-反转字符串中的元音字符)
* [4. 回文字符串](#4-回文字符串)
* [5. 归并两个有序数组](#5-归并两个有序数组)
* [6. 判断链表是否存在环](#6-判断链表是否存在环)
* [7. 最长子序列](#7-最长子序列)
<!-- GFM-TOC -->
双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。
# 有序数组的 Two Sum
# 1. 有序数组的 Two Sum
[Leetcode 167. Two Sum II - Input array is sorted (Easy)](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)
@ -45,7 +45,7 @@ public int[] twoSum(int[] numbers, int target) {
}
```
# 两数平方和
# 2. 两数平方和
[633. Sum of Square Numbers (Easy)](https://leetcode.com/problems/sum-of-square-numbers/description/)
@ -74,7 +74,7 @@ public boolean judgeSquareSum(int c) {
}
```
# 反转字符串中的元音字符
# 3. 反转字符串中的元音字符
[345. Reverse Vowels of a String (Easy)](https://leetcode.com/problems/reverse-vowels-of-a-string/description/)
@ -85,7 +85,8 @@ Given s = "leetcode", return "leotcede".
使用双指针指向待反转的两个元音字符一个指针从头向尾遍历一个指针从尾到头遍历
```java
private final static HashSet<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
private final static HashSet<Character> vowels = new HashSet<>(
Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
public String reverseVowels(String s) {
int i = 0, j = s.length() - 1;
@ -106,7 +107,7 @@ public String reverseVowels(String s) {
}
```
# 回文字符串
# 4. 回文字符串
[680. Valid Palindrome II (Easy)](https://leetcode.com/problems/valid-palindrome-ii/description/)
@ -120,8 +121,7 @@ Explanation: You could delete the character 'c'.
```java
public boolean validPalindrome(String s) {
int i = -1, j = s.length();
while (++i < --j) {
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) {
return isPalindrome(s, i, j - 1) || isPalindrome(s, i + 1, j);
}
@ -139,7 +139,7 @@ private boolean isPalindrome(String s, int i, int j) {
}
```
# 归并两个有序数组
# 5. 归并两个有序数组
[88. Merge Sorted Array (Easy)](https://leetcode.com/problems/merge-sorted-array/description/)
@ -173,7 +173,7 @@ public void merge(int[] nums1, int m, int[] nums2, int n) {
}
```
# 判断链表是否存在环
# 6. 判断链表是否存在环
[141. Linked List Cycle (Easy)](https://leetcode.com/problems/linked-list-cycle/description/)
@ -196,7 +196,7 @@ public boolean hasCycle(ListNode head) {
}
```
# 最长子序列
# 7. 最长子序列
[524. Longest Word in Dictionary through Deleting (Medium)](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/)
@ -210,6 +210,8 @@ Output:
题目描述删除 s 中的一些字符使得它构成字符串列表 d 中的一个字符串找出能构成的最长字符串如果有多个相同长度的结果返回字典序的最小字符串
通过删除字符串 s 中的一个字符能得到字符串 t可以认为 t s 的子序列我们可以使用双指针来判断一个字符串是否为另一个字符串的子序列
```java
public String findLongestWord(String s, List<String> d) {
String longestWord = "";
@ -218,14 +220,14 @@ public String findLongestWord(String s, List<String> d) {
if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
continue;
}
if (isValid(s, target)) {
if (isSubstr(s, target)) {
longestWord = target;
}
}
return longestWord;
}
private boolean isValid(String s, String target) {
private boolean isSubstr(String s, String target) {
int i = 0, j = 0;
while (i < s.length() && j < target.length()) {
if (s.charAt(i) == target.charAt(j)) {