双指针题目
1. 和为S的连续正数序列
思想: 设计前后两个指针以及一个用于保存当前和的变量
import java.util.ArrayList;
public class Solution {
public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) {
int l = 1;
int r = 2;
int totalSum = 3;
int mid = (sum+1)/2;
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
while(l<mid){
if(sum==totalSum){
ArrayList<Integer> subList = new ArrayList<>();
for(int i=l; i<=r; i++){
subList.add(i);
}
ret.add(subList);
}
if(sum<totalSum){
totalSum -= l;
l++;
}
else{
r++;
totalSum += r;
}
}
return ret;
}
}
2.Two Sum II - Input array is sorted (Easy)
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
class Solution {
public int[] twoSum(int[] numbers, int target) {
int l = 0;
int r = numbers.length-1;
while(l<r){
if(target==numbers[l]+numbers[r]) return new int[]{l+1, r+1};
else if(target < (numbers[l]+numbers[r])) r--;
else l++;
}
return null;
}
}
3. Reverse Vowels of a String
**Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = “hello”, return “holle”.**
class Solution {
private HashSet<Character> vowels = new HashSet<>(Arrays.asList('a','e','i','o','u','A','E','I','O','U'));
public String reverseVowels(String s) {
if(s.length()==0) return s;
int l = 0, r = s.length() - 1;
char[] my_char = s.toCharArray();
while(l<=r){
while(!vowels.contains(my_char[l])&&l<r){
l++;
}
while(!vowels.contains(my_char[r])&&l<r){
r--;
}
char temp = my_char[l];
my_char[l] = my_char[r];
my_char[r] = temp;
l++;
r--;
}
return new String(my_char);
}
}
4. Sum of Square Numbers
Given a non-negative integer c, your task is to decide whether there’re two integers a and b such that a2 + b2 = c.
思路:可以使用双指针,也可以用哈希表
class Solution {
public boolean judgeSquareSum(int c) {
int l = 0;
int r = (int)Math.sqrt(c);
while(l<=r){
int sum = l*l + r*r;
if(sum==c) return true;
else if(sum<c){
l++;
}
else{
r--;
}
}
return false;
}
}
public class Solution {
public boolean judgeSquareSum(int c) {
HashSet<Integer> set = new HashSet<> ();
for (int i = 0; i <= Math.sqrt(c); i++) {
set.add(i * i);
if (set.contains(c - i * i)) {
return true;
}
}
return false;
}
}
5. Valid Palindrome II
思路:前后指针去搜索,找到第一对不同的,然后选择删掉左指针或者删掉右指针所指的数。只要其中一个里面满足回文字符串即可。
class Solution {
public boolean validPalindrome(String s) {
int l = 0;
int r = s.length()-1;
while(l<=r){
if(s.charAt(l)!=s.charAt(r)){
return isPalindrome(s, l+1, r)||isPalindrome(s, l, r-1);
}
else{
l++;
r--;
}
}
return true;
}
private boolean isPalindrome(String s, int l, int r){
while(l<r){
if(s.charAt(l)!=s.charAt(r)){
return false;
}
l++;
r--;
}
return true;
}
}
6. Merge Sorted Array(合并两个有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
思路:两指针都从后面开始比较
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1;
int j = n - 1;
int idx = m + n -1;
while(i>=0||j>=0){
if(i<0) {nums1[idx] = nums2[j]; j--;}
else if(j<0) {nums1[idx] = nums1[i]; i--;}
else if(nums1[i]>nums2[j]) {nums1[idx] = nums1[i]; i--;}
else {nums1[idx] = nums2[j]; j--;}
idx--;
}
}
}
7. Linked List Cycle (Easy) 判断链表是否有环
public boolean hasCycle(ListNode head) {
if(head == null) return false;
ListNode l1 = head, l2 = head.next;
while(l1 != null && l2.next != null){
if(l1 == l2) return true;
l1 = l1.next;
l2 = l2.next.next;
}
return false;
}
8. 最长子序列
思路:对每个数组中的字符串进行统计。首先找出重合的元素的个数,然后判断个数是否符合字符串的长度。满足上述条件后,我们要分两种情况,一种是长度相等,一种情况是大于。
class Solution {
public String findLongestWord(String s, List<String> d) {
int max = 0;
String ret = "";
for(String cur_str : d){
int i=0;
int k = 0;
int count = 0;
\\找出重合的个数
while(i<s.length()&&k<cur_str.length()){
if(s.charAt(i)==cur_str.charAt(k)) {k++; count++;}
i++;
}
if(count==cur_str.length()){
\\ 如果ret大于cur_str.降序用
if((count==max&&ret.compareTo(cur_str) > 0)||count>max){
ret=cur_str; max = count;
}
}
}
return ret;
}
}