有序数组的 Two Sum
Leetcode :167. Two Sum II - Input array is sorted (Easy)
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
题目描述:在有序数组中找出两个数,使它们的和为 target。
使用双指针,一个指针指向值较小的元素,一个指针指向值较大的元素。指向较小元素的指针从头向尾遍历,指向较大元素的指针从尾向头遍历。
- 如果两个指针指向元素的和 sum == target,那么得到要求的结果;
- 如果 sum > target,移动较大的元素,使 sum 变小一些;
- 如果 sum < target,移动较小的元素,使 sum 变大一些。
如果是无序的数组two sum则考虑使用hashmap做优化。
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;
}
两数平方和
633. Sum of Square Numbers (Easy)
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
题目描述:判断一个数是否为两个数的平方和。
public boolean judgeSquareSum(int c) {
//双指针,i表示左边从0开始的指针,j表示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;
}
反转字符串中的元音字符
345. Reverse Vowels of a String (Easy)
Given s = "leetcode", return "leotcede".
使用双指针指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。
class Solution {
//用hashset判断字符是否是元音字符
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) {
char[] sc = s.toCharArray();
int left = 0;
int right = sc.length-1;
while(left<=right){
while(left<=right&&!vowels.contains(sc[left])){
left++;
}
while(left<=right&&!vowels.contains(sc[right])){
right--;
}
if(left<=right){
char tmp = sc[left];
sc[left] = sc[right];
sc[right] = tmp;
left++;
right--;
}
}
return new String(sc);
}
}
回文字符串
680. Valid Palindrome II (Easy)
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
题目描述:可以删除一个字符,判断是否能构成回文字符串。
public boolean validPalindrome(String s) {
//判断是否是回文
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
//如果是回文,这一步不会进入,如果出现中间比较不相等的情况,考虑两种结果
//一种是删除左指针(i)指向的,一种是删除右指针(j)指向的
if (s.charAt(i) != s.charAt(j)) {
return isPalindrome(s, i, j - 1) || isPalindrome(s, i + 1, j);
}
}
return true;
}
//判断s字符串从i到j是否是回文
private boolean isPalindrome(String s, int i, int j) {
while (i < j) {
if (s.charAt(i++) != s.charAt(j--)) {
return false;
}
}
return true;
}
归并两个有序数组
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
题目描述:把归并结果存到第一个数组上。
需要从尾开始遍历,否则在 nums1 上归并得到的值会覆盖还未进行归并比较的值。
public void merge(int[] nums1, int m, int[] nums2, int n) {
//主要思路是从nums1后面往前面依次加入数据
//index1是当前nums1最后正在操作的数
//index2是当前nums2最后正在操作的数
int index1 = m - 1, index2 = n - 1;
//indexMerge是合并后的数组最后的index
int indexMerge = m + n - 1;
while (index1 >= 0 || index2 >= 0) {
//nums1中的数全部算完了,把所有nums2剩下的数加入nums1
if (index1 < 0) {
nums1[indexMerge--] = nums2[index2--];
//nums2中的数全部算完了,把所有nums1剩下的数加入nums1
} else if (index2 < 0) {
nums1[indexMerge--] = nums1[index1--];
} else if (nums1[index1] > nums2[index2]) {
nums1[indexMerge--] = nums1[index1--];
} else {
nums1[indexMerge--] = nums2[index2--];
}
}
}
判断链表是否存在环
使用双指针,一个指针每次移动一个节点,一个指针每次移动两个节点,如果存在环,那么这两个指针一定会相遇。
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
//快指针
ListNode fast = head;
//慢指针
ListNode slow = head;
while(fast.next!=null && fast.next.next!=null && slow.next!=null){
fast = fast.next.next;
slow = slow.next;
if(slow == fast)
return true;
}
return false;
}
}
最长子序列
524. Longest Word in Dictionary through Deleting (Medium)
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"
题目描述:删除 s 中的一些字符,使得它构成字符串列表 d 中的一个字符串,找出能构成的最长字符串。如果有多个相同长度的结果,返回字典序的最小字符串。
通过删除字符串 s 中的一个字符能得到字符串 t,可以认为 t 是 s 的子序列,我们可以使用双指针来判断一个字符串是否为另一个字符串的子序列。
class Solution {
public String findLongestWord(String s, List<String> d) {
String res = "";
for(String target: d){
if(target.length()<res.length() || (target.length()==res.length() && res.compareTo(target)<0)){
continue;
}
if(isSubstr(s,target)){
res = target;
}
}
return res;
}
//判断s中是否有目标字串,主要思想就是双指针
private boolean isSubstr(String s, String target) {
int i=0;
int j=0;
while(i<s.length() && j< target.length()){
if(s.charAt(i) == target.charAt(j)){
j++;
}
i++;
}
return j==target.length();
}
}