Leetcode刷题笔记—two pointers (17)

文章包含一系列编程问题的解决方案,主要涉及字符串的回文验证、数组元素移除、去重以及排序数组的操作。此外,还涵盖了链表的去重、查找环以及求最长递增子序列等算法问题。
摘要由CSDN通过智能技术生成

680. Valid Palindrome II  

class Solution {
    public boolean validPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        while(i < j) {
            if(s.charAt(i) != s.charAt(j)) {
                return(check(s, i, j - 1) || check(s, i + 1, j));
            }
            i++;
            j--;
        }
        return true;
    }
    public boolean check(String s, int i, int j) {
        while(i < j) {
            if(s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

27. Remove Element

class Solution {
    public int removeElement(int[] nums, int val) {
        int index = 0;
        for(int num : nums) {
            if(num != val) {
                nums[index] = num;
                index++;
            }
        }
        return index;
    }
}

26. Remove Duplicates from Sorted Array

class Solution {
    public int removeDuplicates(int[] nums) {
       int index = 1;
       for(int i = 1; i < nums.length; i++) {
           if(nums[i] != nums[i - 1]) {
               nums[index] = nums[i];
               index++;
           }
       }
       return index;
    }
}



class Solution {
    public int removeDuplicates(int[] nums) {
       int index = 0;
       for(int i = 1; i < nums.length; i++) {
           if(nums[index] != nums[i]) {
               index++;
               nums[index] = nums[i];
           }
       }
       return index +1;
    }
}

80. Remove Duplicates from Sorted Array II

class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length <= 1) {
            return nums.length;
        }
        int i = 0, j = 1, sum = 1;
        while(j < nums.length) {
            if(nums[i] == nums[j] && sum < 2) {
                nums[++i] = nums[j];
                sum++;
            } else if(nums[i] != nums[j]) {
                sum = 1;
                nums[++i] = nums[j];
            }
            j++;
        }
        return ++i;
    }
}

83. Remove Duplicates from Sorted List

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null) {
          return head;
        }
        ListNode p = head;
        ListNode nex = head.next;
        while(nex != null) {
            if(p.val != nex.val) {
                p.next = nex;
                p = nex;
            } 
            nex = nex.next;
        }
        p.next = null;
        return head;
    }
}

82. Remove Duplicates from Sorted List II

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode fake = new ListNode();
        fake.next = head;
        ListNode first = fake;
        while(first.next != null && first.next.next != null) {
            if(first.next.val == first.next.next.val) {
                int duplic = first.next.val;
                while(first.next != null && first.next.val == duplic) {
                    first.next = first.next.next;
                }
            } else {
                first = first.next;
            }
        }
        return fake.next;
    }
}

643. Maximum Average Subarray I

class Solution {
    public double findMaxAverage(int[] nums, int k) {
        int sum = 0;
        if(nums.length <= 1) {
            return nums[0];
        }
        for(int i = 0; i < k; i++) {
            sum += nums[i];
        }
        int maxSum = sum;
        for(int i = k; i < nums.length; i++) {
            sum += nums[i];
            sum -= nums[i-k];
            maxSum = maxSum > sum ? maxSum : sum;
        }
        return  (double)maxSum / (double)k;
    }
}

674. Longest Continuous Increasing Subsequence

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int count = 1;
        int max = 1;
        for(int i = 1; i < nums.length; i++) {
            if(nums[i] > nums[i - 1]) {
                count++;
                max = Math.max(max, count);
            } else {
                count = 1;
            }
        }
        return max;
    }
}

160. Intersection of Two Linked Lists

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
       ListNode a = headA, b = headB;
       while(a != b) {
           a = a == null ? headB : a.next;
           b = b == null ? headA : b.next;
       } 
       return b;
    }
}

88. Merge Sorted Array

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int index1 = m - 1, index2 = n - 1, index3 = m + n - 1;
        while(index1 >= 0 && index2 >= 0) {
            if(nums1[index1] > nums2[index2]) {
                nums1[index3--] = nums1[index1--];
            } else {
                nums1[index3--] = nums2[index2--];
            }
        }
        while(index2 >= 0) {
            nums1[index3--] = nums2[index2--];
        }
    }
}

141. Linked List Cycle

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast) {
                return true;
            }
        }
        return false;
    }
}

142. Linked List Cycle II 

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

287. Find the Duplicate Number

class Solution {
    public int findDuplicate(int[] nums) {
        int slow = nums[0];
        int fast = nums[nums[0]];
        while(slow != fast) {
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        int entry = 0;
        while(entry != slow) {
            entry = nums[entry];
            slow = nums[slow];
        }
        return entry;
    }
}

234. Palindrome Linked List

class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head.next == null){
            return true;
        }      
        ListNode middleNode = getMiddleNode(head);
        ListNode head1 = head;
        ListNode head2 = middleNode.next; 
        middleNode.next = null; 
        head2 = reverseLinkedList(head2); 
        while(head1!=null && head2!=null){
            if(head1.val != head2.val){
                return false;
            }
            head1 = head1.next;
            head2 = head2.next;
        }
        return true;
    }
    public ListNode getMiddleNode(ListNode node){
        ListNode slow = node;
        ListNode fast = node.next;
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    public ListNode reverseLinkedList(ListNode node){
        if(node==null || node.next == null){
            return node;
        }
        ListNode newHead = reverseLinkedList(node.next);
        node.next.next = node;
        node.next = null;
        return newHead;
    }
}

2540. Minimum Common Value

class Solution {
    public int getCommon(int[] nums1, int[] nums2) {
        int n1 = 0, n2 = 0;
        int len1 = nums1.length, len2 = nums2.length;
        while(n1 < len1 && n2 < len2) {
            if(nums1[n1] == nums2[n2]) {
                return nums1[n1];
            } else if(nums1[n1] > nums2[n2]){
                n2++;
            } else {
                n1++;
            }
        }
        return -1;
    }
}

11. Container With Most Water

class Solution {
    public int maxArea(int[] height) {
       int begin = 0, end = height.length - 1;
       int res = 0;
       while(begin < end) {
           res = Math.max(res, (end - begin) * Math.min(height[begin], height[end]));
           if(height[begin] < height[end]) {
               begin++;
           } else {
               end--;
           }
       } 
       return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值